PowerShell for Windows Admins

Apr 4 2014   12:18PM GMT

Discovering namespaces part 2

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

I recently showed how to use Get-CimInstance to discover the namespaces present in a particular CIM namespace. I’m going to try to use CIM instaed of WMI but expect the old terminology to creep in occasionally.

The function I showed last time will only find the first level of namespaces in a namespace – what if those namespaces contain namespaces.

This is where you get to meet the concept of recursion. In this case all it means is that we’re going to call our function from within the function. Its easier to show with code.

function get-cimnamespace {

param (

[string]$namespace = ‘root/cimv2′,

[switch]$nobase

)

if (-not $nobase)

{

New-Object -TypeName psobject -Property @{

Name = $namespace

}

}

Get-CimInstance -ClassName __NameSpace -Namespace $namespace |

foreach {

$ns = New-Object -TypeName psobject -Property @{

Name = “$($psitem.CimSystemProperties.NameSpace)/$($psitem.Name)”

}

Write-Output $ns

get-cimnamespace -namespace $ns.Name -nobase

}

}

The function has 2 parameters – the startign namespace parameter and a switch parameter. The switch controls if the namespace used as a parameter is output.

Get-CimInstance is used to find each instance of the __Namespace class. Foreach instance the namespace name is output and then used to call get-cimnamespace with the new namespace as a paramter. Its already been output so the –nobase switch is used to prevent duplicate output. And that’s recursion.

On my test machine I get this

£> get-cimnamespace

Name

—-

root/cimv2

ROOT/cimv2/mdm

ROOT/cimv2/mdm/MS_40c

ROOT/cimv2/mdm/MS_809

ROOT/cimv2/mdm/MS_413

ROOT/cimv2/mdm/MS_409

ROOT/cimv2/mdm/MS_407

ROOT/cimv2/ms_40c

ROOT/cimv2/Security

ROOT/cimv2/Security/MicrosoftTpm

ROOT/cimv2/Security/MicrosoftVolumeEncryption

ROOT/cimv2/ms_809

ROOT/cimv2/power

ROOT/cimv2/power/MS_40c

ROOT/cimv2/power/ms_809

ROOT/cimv2/power/MS_413

ROOT/cimv2/power/ms_409

ROOT/cimv2/power/MS_407

ROOT/cimv2/ms_413

ROOT/cimv2/ms_409

ROOT/cimv2/TerminalServices

ROOT/cimv2/TerminalServices/ms_40c

ROOT/cimv2/TerminalServices/ms_809

ROOT/cimv2/TerminalServices/ms_413

ROOT/cimv2/TerminalServices/ms_407

ROOT/cimv2/ms_407

ROOT/cimv2/Applications

ROOT/cimv2/Applications/WindowsParentalControls

ROOT/cimv2/Applications/WindowsParentalControls/Secured

ROOT/cimv2/Applications/Games

ROOT/cimv2/Applications/Games/ms_40c

ROOT/cimv2/Applications/Games/ms_809

ROOT/cimv2/Applications/Games/ms_413

ROOT/cimv2/Applications/Games/ms_409

ROOT/cimv2/Applications/Games/ms_407

I’m loading the function as part of a CimInvetsigation module which now has 2 cmdlets:

Get-CimMethod

Get-CimNamespace

 Comment on this Post

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to: