如何使用VBScript和WMI列出本地管理员组的所有成员

时间:2011-10-14 16:42:15

标签: vbscript wmi

我需要编写一个脚本来获取Windows服务器的Administrators组中的成员列表以进行审计。我还需要显示用户/组的域名(如果它不是本地的)。

1 个答案:

答案 0 :(得分:4)

以下脚本甚至可以在域控制器上运行:

Sub GetAdministrators(strComputerName)
    Dim objWMIService, strQuery, colItems, Path, strMembers
    Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
    strQuery = "select * from Win32_GroupUser where GroupComponent = " & chr(34) & "Win32_Group.Domain='" & strComputerName & "',Name='Administrators'" & Chr(34)
    Set ColItems = objWMIService.ExecQuery(strQuery)
    strMembers = ""
    For Each Path In ColItems
        Dim strMemberName, NamesArray, strDomainName, DomainNameArray
        NamesArray = Split(Path.PartComponent,",")
        strMemberName = Replace(Replace(NamesArray(1),Chr(34),""),"Name=","")
        DomainNameArray = Split(NamesArray(0),"=")
        strDomainName = Replace(DomainNameArray(1),Chr(34),"")
        If strDomainName <> strComputerName Then
            strMemberName = strDomainName & "\" & strMemberName
        End If
        WScript.Echo strMemberName
    Next
End Sub

Function GetComputerName()
    Set objWMISvc = GetObject( "winmgmts:\\.\root\cimv2" )
    Set colItems = objWMISvc.ExecQuery( "Select * from Win32_ComputerSystem", , 48 )
    For Each objItem in colItems
        strComputerName = objItem.Name
        GetComputerName = strComputerName
    Next
End Function

GetAdministrators GetComputerName

要执行它,只需运行命令提示符窗口:

cscript //nologo script.vbs.