PoshCode Archive  Artifact [7d6630a64e]

Artifact 7d6630a64e4200e3b0378641467907aab6d789bb72f5332bea5469b4c79002ae:

  • File Get-WmiNamespace.ps1 — part of check-in [4d6dcfab3b] at 2018-06-10 12:56:23 on branch trunk — In order to enumerate all the WMI namespaces, you must first connect to the “root” namespace, query for all the “__NAMESPACE” instances, and for each instance recursively repeat this process. You can use the computerName parameter of Get-WmiNamespace to list the WMI namespaces on the remote computer. (user: Aleksandar size: 1640)

# encoding: ascii
# api: powershell
# title: Get-WmiNamespace
# description: In order to enumerate all the WMI namespaces, you must first connect to the “root” namespace, query for all the “__NAMESPACE” instances, and for each instance recursively repeat this process. You can use the computerName parameter of Get-WmiNamespace to list the WMI namespaces on the remote computer.
# version: 0.1
# type: function
# author: Aleksandar
# license: CC0
# function: Get-WmiNamespace
# x-poshcode-id: 1079
# x-archived: 2016-03-06T01:17:00
# x-published: 2010-05-04T14:23:00
#
#
# In order to enumerate all the WMI namespaces, you must first connect to the "root" namespace,
# query for all the "__NAMESPACE" instances, and for each instance recursively repeat this process.
# You can use the computerName parameter of Get-WmiNamespace to list the WMI namespaces on the remote computer.

function Get-WmiNamespace {
	param (
	[string]$rootns = "root",
	[string]$computerName = ".",
	$credential
	)

	if ($credential -is [String] ) {
		$credential = Get-Credential $credential
	}

	if ($credential -eq $null) {
		gwmi -class __namespace -namespace $rootns -computerName $computerName |
		where {$_.name} | foreach {
			$ns = "{0}\{1}" -f $rootns,$_.name
			$ns
			Get-WmiNamespace -rootns $ns -computer $computerName
		}
	}
	else {
		gwmi -class __namespace -namespace $rootns -computerName $computerName -credential $credential |
		where {$_.name} | foreach {
			$ns = "{0}\{1}" -f $rootns,$_.name
			$ns
			Get-WmiNamespace -rootns $ns -computer $computerName -credential $credential
		}
	}
}