WMI 任务:帐户和域

帐户和域管理任务获取计算机域或当前登录用户等信息。 其中的许多任务都是用 ADSI 脚本来执行的。 有关详细信息和其他示例,请参阅 TechNet ScriptCenter 脚本存储库。

本主题中所示的脚本示例仅从本地计算机获取数据。 有关如何使用脚本从远程计算机获取数据的详细信息,请参阅 连接到远程计算机上的 WMI

下面的过程介绍如何运行脚本。

运行脚本

  1. 复制代码并将其保存到扩展名为 .vbs 的文件中,如 filename.vbs。 确保文本编辑器不会向该文件添加 .txt 扩展名。
  2. 打开命令提示符窗口并导航到保存该文件的目录。
  3. 在命令提示符下键入 cscript filename.vbs
  4. 如果无法访问事件日志,请检查是否正在从提升权限的命令提示符运行。 某些事件日志,例如安全事件日志,可能会受到 (UAC) 的用户访问控制的保护。

备注

默认情况下,cscript 会在命令提示符窗口中显示脚本的输出。 由于 WMI 脚本可能生成大量输出,因此可能需要将输出重定向到文件。 在命令提示符下键入 cscript filename.vbs > outfile.txt ,将 filename.vbs 脚本的输出重定向到 outfile.txt

下表列出了可用于从本地计算机获取各种类型的数据的脚本示例。

如何实现... WMI 类或方法
...确定计算机所属的域? 使用 Win32_ComputerSystem 类并检查 Domain 属性的值。 你还可以使用Win32_NetworkAdapterConfiguration中的DNSDomain属性。

VB
strComputer = "."
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colSettings = objWMIService.ExecQuery("Select * from Win32_ComputerSystem")

For Each objComputer in colSettings Wscript.Echo "System Name: " & objComputer.Name Wscript.Echo "Domain: " & objComputer.Domain Next

PowerShell
$computer = Get-WmiObject -Class Win32_ComputerSystem
"System Name: {0}" -f $computer.name
"Domain : {0}" -f $computer.domain

C#
using Microsoft.Management.Infrastructure;
...
CimSession session = CimSession.Create("localHost");
IEnumerable

foreach (CimInstance cimObj in queryInstance) { Console.WriteLine(cimObj.CimInstanceProperties["Name"].ToString()); Console.WriteLine(cimObj.CimInstanceProperties["Domain"].ToString()); }

...确定计算机是服务器还是工作站?

使用 Win32_ComputerSystem 类和 DomainRole 属性。

VB
strComputer = "."
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colComputers = objWMIService.ExecQuery("Select DomainRole from Win32_ComputerSystem")
For Each objComputer in colComputers
    Select Case objComputer.DomainRole 
        Case 0 
            strComputerRole = "Standalone Workstation"
        Case 1        
            strComputerRole = "Member Workstation"
        Case 2
            strComputerRole = "Standalone Server"
        Case 3
            strComputerRole = "Member Server"
        Case 4
            strComputerRole = "Backup Domain Controller"
        Case 5
            strComputerRole = "Primary Domain Controller"
    End Select
    Wscript.Echo strComputerRole
Next

PowerShell
$Computer = Get-WmiObject -Class Win32_ComputerSystem 

"Computer "{0}.{1}" is a: "-f $Computer.Name,$computer.domain

switch ($computer.DomainRole) { 0 {"Standalone Workstation"} 1 {"Member Workstation"} 2 {"Standalone Server"} 3 {"Member Server"} 4 {"Backup Domain Controller"} 5 {"Primary Domain Controller"} }

...确定计算机名称?

使用 Win32_ComputerSystem 类和 Name 属性。 你还可以使用Win32_NetworkAdapterConfiguration中的DNSHostName属性。

VB
strComputer = "."
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_ComputerSystem")
For Each objItem in colItems
    Wscript.Echo "Computer Name: " & objItem.Name
Next
PowerShell
$Computer = Get-WmiObject -Class Win32_ComputerSystem
"Computer Name is: {0}" -f $Computer.Name

C#
using Microsoft.Management.Infrastructure;
...
CimSession session = CimSession.Create("localHost");
IEnumerable

foreach (CimInstance cimObj in queryInstance) { Console.WriteLine(cimObj.CimInstanceProperties["Name"].ToString()); }

...查找当前登录到计算机的人员的姓名?

使用 Win32_ComputerSystem 类和 UserName 属性。

VB
strComputer = "."
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 
Set colComputer = objWMIService.ExecQuery("Select * from Win32_ComputerSystem")
 
For Each objComputer in colComputer
    Wscript.Echo "User Name = " & objComputer.UserName & VBNewLine & "Computer Name = " & objComputer.Name
WScript.Echo objComputer.UserName
Next
PowerShell
$computers = Get-WmiObject -Class Win32_ComputerSystem 
"Logged on user(s):"
foreach($computer in $computers) {
   "User: {0}" -f $computer.UserName
}

C#
using Microsoft.Management.Infrastructure;
...
CimSession session = CimSession.Create("localHost");
IEnumerable

foreach (CimInstance cimObj in queryInstance) { Console.WriteLine("User Name: " + cimObj.CimInstanceProperties["UserName"].ToString()); }

...重命名计算机?

使用 Win32_ComputerSystem 类和 重命名 方法。

VB
strComputer = "."
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colComputers = objWMIService.ExecQuery("Select * from Win32_ComputerSystem")
For Each objComputer in colComputers
    errReturn = ObjComputer.Rename("NewName")
    WScript.Echo "Computer name is now " & objComputer.Name
Next

PowerShell
param (
[$String] $NewName = 'NewName',
[$string] $Comp = "."
}

<# Get computer object #> $Computer = Get-WmiObject -Class Win32_ComputerSystem -ComputerName $comp

<# Rename the Computer #> $Return = $Computer.Rename($NewName)

if ($return.ReturnValue -eq 0) { "Computer name is now: $NewName" " but you need to reboot first" } else { " RenameFailed, return code: {0}" -f $return.ReturnValue }

...是否仅检索使用 WMI 的本地组?

使用Win32_Group类,并在WQL查询中包含以下WHERE子句。

Where LocalAccount = True

VB
strComputer = "."
Set objWMIService = GetObject( _
    "winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery _
    ("Select * from Win32_Group  Where LocalAccount = True")
For Each objItem in colItems
    Wscript.Echo "Local Account: " & objItem.LocalAccount & VBNewLine _
        & "Name: " & objItem.Name & VBNewLine _
        & "SID: " & objItem.SID & VBNewLine _
        & "SID Type: " & objItem.SIDType & VBNewLine _
        & "Status: " & objItem.Status & VBNewLine
Next
PowerShell
$Accts=Get-WMIObjectWin32_Group|where {$_.LocalAccount}
$accts |ftName, Sid, SidType, Status-autosize

脚本和应用程序的 WMI 任务

WMI c + + 应用程序示例

TechNet ScriptCenter