I want to get the SID of a group by input the group name and it will return the SID. Is there any way to do this using vbscript ? Any help would be great
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
Use WMI for resolving the SID:
Set wmi = GetObject("winmgmts://./root/cimv2")
groupname = "..."
qry = "SELECT * FROM Win32_Group WHERE Name='" & groupname & "'"
For Each group In wmi.ExecQuery(qry)
sid = group.SID
Next
If Not IsEmpty(sid) Then
WScript.Echo "Group " & groupname & " has SID " & sid & "."
Else
WScript.Echo "SID For group " & groupname & " could not be resolved."
End If
If you need to convert the binary SIDs used in AD I'd suggest you do something like this:
Function DecodeSID(binSID)
ReDim octets(LenB(binSID))
'convert binary string to octet array
For i = 1 To LenB(binSID)
octets(i-1) = AscB(MidB(binSID, i, 1))
Next
'convert octet array to SID string
sid = "S-" & CStr(octets(0)) & "-" & Octet2Str(Array(octets(2), octets(3), _
octets(4), octets(5), octets(6), octets(7)))
For i = 8 To (4 * octets(1) + 4) Step 4
sid = sid & "-" & OctetArrayToString(Array(octets(i+3), octets(i+2), _
octets(i+1), octets(i)))
Next
DecodeSID = sid
End Function
Function OctetArrayToString(arr)
v = 0
For i = 0 To UBound(arr)
v = v * 256 + arr(i)
Next
OctetArrayToString = CStr(v)
End Function
For more information on binary SIDs see here.
回答2:
I forgot where I got this but here it is:
Set oAD = GetObject(sLDAP) ' sLDAP starts with "LDAP://"
wscript.echo "SID: " & HexSIDToDec(OctetToHexStr(oAD.Get("objectSid"))))
Function HexSIDToDec(strSID)
' Function to convert most hex SID values to decimal format.
Dim arrbytSID, lngTemp, j
ReDim arrbytSID(Len(strSID)/2 - 1)
For j = 0 To UBound(arrbytSID)
arrbytSID(j) = CInt("&H" & Mid(strSID, 2*j + 1, 2))
Next
If (UBound(arrbytSID) = 11) Then
HexSIDToDec = "S-" & arrbytSID(0) & "-" & arrbytSID(1) & "-" & arrbytSID(8)
Exit Function
End If
If (UBound(arrbytSID) = 15) Then
HexSIDToDec = "S-" & arrbytSID(0) & "-" & arrbytSID(1) & "-" & arrbytSID(8)
lngTemp = arrbytSID(15)
lngTemp = lngTemp * 256 + arrbytSID(14)
lngTemp = lngTemp * 256 + arrbytSID(13)
lngTemp = lngTemp * 256 + arrbytSID(12)
HexSIDToDec = HexSIDToDec & "-" & CStr(lngTemp)
Exit Function
End If
HexSIDToDec = "S-" & arrbytSID(0) & "-" & arrbytSID(1) & "-" & arrbytSID(8)
lngTemp = arrbytSID(15)
lngTemp = lngTemp * 256 + arrbytSID(14)
lngTemp = lngTemp * 256 + arrbytSID(13)
lngTemp = lngTemp * 256 + arrbytSID(12)
HexSIDToDec = HexSIDToDec & "-" & CStr(lngTemp)
lngTemp = arrbytSID(19)
lngTemp = lngTemp * 256 + arrbytSID(18)
lngTemp = lngTemp * 256 + arrbytSID(17)
lngTemp = lngTemp * 256 + arrbytSID(16)
HexSIDToDec = HexSIDToDec & "-" & CStr(lngTemp)
lngTemp = arrbytSID(23)
lngTemp = lngTemp * 256 + arrbytSID(22)
lngTemp = lngTemp * 256 + arrbytSID(21)
lngTemp = lngTemp * 256 + arrbytSID(20)
HexSIDToDec = HexSIDToDec & "-" & CStr(lngTemp)
If (UBound(arrbytSID) > 23) Then
lngTemp = arrbytSID(27)
lngTemp = lngTemp * 256 + arrbytSID(26)
lngTemp = lngTemp * 256 + arrbytSID(25)
lngTemp = lngTemp * 256 + arrbytSID(24)
HexSIDToDec = HexSIDToDec & "-" & CStr(lngTemp)
End If
End Function
Function OctetToHexStr(ByVal arrbytOctet)
' Function to convert OctetString (byte array) to Hex string.
Dim k
OctetToHexStr = ""
For k = 1 To Lenb(arrbytOctet)
OctetToHexStr = OctetToHexStr & Right("0" & Hex(Ascb(Midb(arrbytOctet, k, 1))), 2)
Next
End Function