How do I get SID for the group using vbscript?

前端 未结 2 1485
忘掉有多难
忘掉有多难 2021-01-27 10:02

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

2条回答
  •  不要未来只要你来
    2021-01-27 11:03

    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.

提交回复
热议问题