作成 2010.01.07
更新 2010.01.07
更新 2010.01.07
VBScript で Active Directory のオブジェクトSIDを取得する
このサンプルではstrDNに設定されたオブジェクトのSIDを表示します。
Option Explicit
Const adTypeBinary = 1
Const adTypeText = 2
Const strDN = "CN=DC1,OU=Domain Controllers,DC=example,DC=lan"
' Const strDN = "CN=Administrator,CN=Users,DC=example,DC=lan"
Dim adObject, rawSID
On Error Resume Next
Set adObject = GetObject("LDAP://" & strDN )
If Err.Number <> 0 Then
WScript.Echo strDN & " に接続できません"
WScript.Quit
End If
On Error Goto 0
WScript.Echo GetStringSID( adObject )
Function GetStringSID( objAccount )
Dim retStr, rawSID, arrSID
Dim adStream, i, tmp, j
retStr = "S-"
rawSID = objAccount.Get( "objectSid" )
Set adStream = WScript.CreateObject("ADODB.Stream")
adStream.Type = adTypeText
adStream.Charset = "UTF-16"
adStream.Open
adStream.WriteText rawSID
adStream.Position = 0
adStream.Type = adTypeBinary
adStream.Position = 2
ReDim arrSID(UBound(rawSID))
For i=0 to UBound(rawSID)
tmp = adStream.Read(1)
If IsNull(tmp) Then Exit For
arrSID(i) = AscB(tmp)
Next
adStream.Close
retStr = retStr & arrSID(0) & "-" & arrSID(1)
j = 3
Do While UBound(arrSID) >= j * 4 - 1
tmp = arrSID(j*4-1) * 256^3 + arrSID(j*4-2) * 256^2 + arrSID(j*4-3) * 256 + arrSID(j*4-4)
j = j + 1
retStr = retStr & "-" & tmp
Loop
GetStringSID = retStr
End Function