Quantcast
Channel: WMI – PowerShell for Windows Admins
Viewing all articles
Browse latest Browse all 140

Discovering namespaces part 2

$
0
0

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


Viewing all articles
Browse latest Browse all 140

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>