#requires -version 2.0 # ----------------------------------------------------------------------------- # Script: Get-WMINamespace-v2.ps1 # Author: Jeffery Hicks # http://jdhitsolutions.com/blog # http://twitter.com/JeffHicks # http://www.scriptinggeek.com # Date: 6/15/2011 # Keywords: WMI, Namespace, Invoke-Expression # Comments: # # ----------------------------------------------------------------------------- Function Get-WmiNamespace { <# .SYNOPSIS Get WMI Namespaces .DESCRIPTION This command uses WMI to query a machine and return namespaces. Without any parameters it will return the top level namespaces. Or you can use -Recurse to list all namespaces. .PARAMETER Computername THe name of the computer to query. The default is the localhost. The parameter has an alias of 'Name'. .PARAMETER Credential A saved PSCredential object or a username which will be used to create a PSCredential. .PARAMETER Recurse Recurse through all namespaces .EXAMPLE PS C:\> Get-WMINamespace Computername Namespace Name ------------ --------- ---- SERENITY ROOT\Policy Policy SERENITY ROOT\Interop Interop SERENITY ROOT\WMI WMI SERENITY ROOT\directory directory SERENITY ROOT\Microsoft Microsoft SERENITY ROOT\aspnet aspnet SERENITY ROOT\ServiceModel ServiceModel SERENITY ROOT\SecurityCenter SecurityCenter SERENITY ROOT\RSOP RSOP SERENITY ROOT\CIMV2 CIMV2 SERENITY ROOT\Cli Cli SERENITY ROOT\subscription subscription SERENITY ROOT\DEFAULT DEFAULT SERENITY ROOT\snmp snmp SERENITY ROOT\SecurityCenter2 SecurityCenter2 SERENITY ROOT\nap nap SERENITY ROOT\SECURITY SECURITY Get top level namespaces on the local host. .EXAMPLE PS C:\> Get-WMINamespace -computername "JDHIT-DC01" -cred $jdhit -recurse | Select Namespace Namespace --------- ROOT ROOT\aspnet ROOT\CIMV2 ROOT\CIMV2\Applications ROOT\CIMV2\Applications\MicrosoftIE ROOT\Cli ROOT\DEFAULT ROOT\directory ROOT\directory\LDAP ROOT\Microsoft ROOT\Microsoft\HomeNet ROOT\MicrosoftActiveDirectory ROOT\MicrosoftDNS ROOT\MicrosoftIISv2 ROOT\MicrosoftNLB ROOT\MSCluster ROOT\perfmon ROOT\Policy ROOT\RSOP ROOT\RSOP\Computer ROOT\RSOP\User ROOT\RSOP\User\S_1_5_21_805063240_3875113082_2769008284_500 ROOT\SECURITY ROOT\ServiceModel ROOT\snmp ROOT\snmp\localhost ROOT\subscription ROOT\WMI Connect to JDHIT-DC01 using the saved credential and get all namespaces .NOTES NAME : Get-WmiNamespace VERSION : 2.0 LAST UPDATED: 6/16/2011 AUTHOR : Jeffery Hicks .LINK http://jdhitsolutions.com/blog/2011/06/get-wmi-namespace/ .LINK Get-WmiObject Invoke-Expression .INPUTS String .OUTPUTS Custom Object #> [cmdletBinding()] Param( [Parameter(Position=0,Mandatory=$False,ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$True)] [ValidateNotNullorEmpty()] [Alias("name")] [string]$Computername=$env:computername, [object]$Credential, [switch]$Recurse ) Begin { Write-Verbose -Message "$(Get-Date) Starting $($myinvocation.mycommand)" #convert credential to a PSCredential if a string was passed. if ( $Credential -is [system.management.automation.psCredential]) { Write-Verbose "$(Get-Date) Using PSCredential for $($credential.username)" } ElseIf ($Credential) { Write-Verbose "$(Get-Date) Getting PSCredential for $credential" $Credential=Get-Credential $credential } } #close Begin Process { #create a command string. PacketPrivacy is required for some namespaces #like MicrosoftTPM $cmd="Get-WMIObject -computername $computername -Namespace 'Root' -class '__Namespace' -authentication 'PacketPrivacy' -errorAction 'Stop'" if ($recurse) { $cmd+=" -List -Recurse" } if ($Credential) { $cmd+=" -credential `$Credential" } Try { Write-Verbose -Message "$(Get-Date) $cmd" $data=Invoke-Expression $cmd } Catch { Write-Warning "Failed to retrieve namespace information from $Computername" Write-Error $_ } if ($data) { Write-Verbose "$(Get-Date) Processing $(($data | measure-object).count) items" if ($Recurse) { #format the data to accomodate nested namespaces $data | Sort __NAMESPACE | Select @{Name="Computername";Expression={$_.__SERVER}}, @{Name="Namespace";Expression={$_.__NAMESPACE}}, @{Name="Name";Expression={Split-Path $_.__Namespace -Leaf }} } else { $data | Sort __NAMESPACE | Select @{Name="Computername";Expression={$_.__SERVER}}, @{Name="Namespace";Expression={Join-path -Path $_.__NAMESPACE -ChildPath $_.Name }},Name } } } #close process End { Write-Verbose -Message "$(Get-Date) Ending $($myinvocation.mycommand)" } #close End } #end Function