# 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
		}
	}
}
