gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 06:33:39 25 4
gpt4 key购买 nike

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

最佳答案

以下脚本甚至可以在域 Controller 上运行:

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.

关于vbscript - 如何使用 VBScript 和 WMI 列出本地管理员组的所有成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7770817/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com