如何查询 WMI 命名空间?

发布于 2024-10-23 15:47:14 字数 234 浏览 5 评论 0 原文

如何查询 WMI 命名空间?

所以我了解 WMI 命名空间,因为我读到它们存在,并且我知道我可以连接说:

root\cimv2

我的问题是,如果我不知道那里有哪些命名空间,我将如何查询可用的命名空间?

我只是想探索 WMI,而不必查找每个名称空间。

我正在使用 WBEMtest,但我会使用任何东西,.NET、winapi.h,你有什么。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

忘东忘西忘不掉你 2024-10-30 15:47:14

我知道您已经得到了答案,但想要展示在 PowerShell 中获取命名空间列表是多么容易:

Get-WMIObject -namespace "root" -class "__Namespace" | Select Name

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
冷︶言冷语的世界 2024-10-30 15:47:14

要枚举所有命名空间,您必须首先连接到 root 命名空间,查询所有 __NAMESPACE 实例,并对每个实例递归地重复此过程。

检查这些示例

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

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
酒绊 2024-10-30 15:47:14

对于 Python(安装了必需的 WMI 和 pyWin32 扩展):

import wmi
c = wmi.WMI (moniker='//./root')
wql = "SELECT * FROM __NAMESPACE"

for __NAMESPACE in c.query(wql):
    print __NAMESPACE.Name

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
神爱温柔 2024-10-30 15:47:14

列出 C# 中名为 server 的服务器的所有(根)命名空间的所有命名空间 Name 属性值(在 LINQPad):

ManagementClass nsClass =
    new ManagementClass(
        new ManagementScope(@"\\server\root"),
        new ManagementPath("__namespace"),
        null);

foreach(ManagementObject ns in nsClass.GetInstances())
{
    ns["Name"].Dump();
}

请注意,Dump 方法仅输出一个值;如果您不使用 LINQPad,则需要对 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.

颜漓半夏 2024-10-30 15:47:14

从 powershell 中使用 Get-CimInstance -Query "SELECT * FROM __NAMESPACE" -Namespace "root"

这也适用于 Windows Nano Server

Use Get-CimInstance -Query "SELECT * FROM __NAMESPACE" -Namespace "root" from powershell.

This also works for Windows Nano Server

欢烬 2024-10-30 15:47:14

您可以使用VBScriptroot获取所有命名空间

这是示例脚本

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

预期输出

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文