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<CimInstance> queryInstance = session.QueryInstances(@"root\cimv2", "WQL", "SELECT * FROM Win32_ComputerSystem"); 

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 &quot;{0}.{1}”为 "-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<CimInstance> queryInstance = session.QueryInstances(@"root\cimv2", "WQL", "SELECT * FROM Win32_ComputerSystem"); 

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<CimInstance> queryInstance = session.QueryInstances(@"root\cimv2", "WQL", "SELECT * FROM Win32_ComputerSystem"); 

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

...重命名计算机?

使用 Win32_ComputerSystem 类和 Rename 方法。

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