How do you query for WMI namespaces?
So I know about WMI namespaces because I read that they exits and I know I can connect to say:
root\cimv2
My question is what if I didn't know what namespaces were there, how would I go about querying for the available namespaces?
I just sort of want to go exploring the WMI and not have to look up each namespace.
I'm using WBEMtest, but I'll take anything, .NET, winapi.h, what have you.
To enumerate all the namespaces, you must first connect to the root
namespace, query for all the __NAMESPACE
instances, and for each instance recursively repeat this process.
check these samples
Delphi
procedure GetListWMINameSpaces(const RootNameSpace:String;const List :TStrings;ReportException:Boolean=True);//recursive function
var
objSWbemLocator : OleVariant;
objWMIService : OleVariant;
colItems : OLEVariant;
colItem : OLEVariant;
oEnum : IEnumvariant;
iValue : LongWord;
sValue : string;
begin
try
objSWbemLocator := CreateOleObject('WbemScripting.SWbemLocator');
objWMIService := objSWbemLocator.ConnectServer(wbemLocalhost, RootNameSpace, '', '');
colItems := objWMIService.InstancesOf('__NAMESPACE');
oEnum := IUnknown(colItems._NewEnum) as IEnumVariant;
while oEnum.Next(1, colItem, iValue) = 0 do
begin
sValue:=VarStrNull(colItem.Name);
colItem:=Unassigned;
List.Add(RootNameSpace+'\'+sValue);
GetListWMINameSpaces(RootNameSpace+'\'+sValue,List);//recursive
end;
except
if ReportException then
raise;
end;
end;
VbScript
strComputer = "."
Call EnumNameSpaces("root")
Sub EnumNameSpaces(strNameSpace)
WScript.Echo strNameSpace
Set objWMIService = GetObject("winmgmts:\\" & strComputer & _
"\" & strNameSpace)
Set colNameSpaces = objWMIService.InstancesOf("__NAMESPACE")
For Each objNameSpace In colNameSpaces
Call EnumNameSpaces(strNameSpace & "\" & objNameSpace.Name)
Next
End Sub
I understand that you got your answer but wanted to show how easy it is in PowerShell to get a list of namespaces:
Get-WMIObject -namespace "root" -class "__Namespace" | Select Name
To list all the namespace Name
property values for all (root) namespaces for a server named server in C# (in LINQPad):
ManagementClass nsClass =
new ManagementClass(
new ManagementScope(@"\\server\root"),
new ManagementPath("__namespace"),
null);
foreach(ManagementObject ns in nsClass.GetInstances())
{
ns["Name"].Dump();
}
Note that the Dump
method just outputs a value; if you're not using LINQPad you'd want to do something else with the Name
property value.
For Python (with the requisite WMI and pyWin32 extensions installed):
import wmi
c = wmi.WMI (moniker='//./root')
wql = "SELECT * FROM __NAMESPACE"
for __NAMESPACE in c.query(wql):
print __NAMESPACE.Name
Use Get-CimInstance -Query "SELECT * FROM __NAMESPACE" -Namespace "root"
from powershell.
This also works for Windows Nano Server
You can use VBScript to get all Namespaces
from root
.
Here is the sample script
On Error Resume Next
strComputer = "."
Wscript.Echo "-----------------------------------"
Wscript.Echo "Namespaces"
Wscript.Echo "-----------------------------------"
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root")
Set colItems = objWMIService.ExecQuery( _
"SELECT * FROM __NAMESPACE",,48)
For Each objItem in colItems
Wscript.Echo "Name: " & objItem.Name
Next
Expected output
Microsoft (R) Windows Script Host Version 5.8
Copyright (C) Microsoft Corporation. All rights reserved.
-----------------------------------
Namespaces
-----------------------------------
Name: subscription
Name: DEFAULT
Name: MicrosoftWmiNet
Name: CIMV2
Name: Cli
Name: nap
Name: MicrosoftIISv2
Name: SECURITY
Name: SecurityCenter2
Name: RSOP
Name: WebAdministration
Name: WMI
Name: OpenHardwareMonitor
Name: directory
Name: Policy
Name: Interop
Name: ServiceModel
Name: SecurityCenter
Name: Microsoft
Name: aspnet