问题描述
如何查询 WMI 命名空间?
How do you query for WMI namespaces?
所以我知道 WMI 命名空间,因为我读到它们退出并且我知道我可以连接说:
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?
我只是想探索 WMI 而不必查找每个命名空间.
I just sort of want to go exploring the WMI and not have to look up each namespace.
我正在使用 WBEMtest,但我会接受任何东西,.NET、winapi.h,你有什么.
I'm using WBEMtest, but I'll take anything, .NET, winapi.h, what have you.
推荐答案
要枚举所有的命名空间,首先要连接到root
命名空间,查询所有的__NAMESPACE
实例,并为每个实例递归重复此过程.
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.
检查这些样本
德尔福
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
这篇关于如何查询 WMI 命名空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!