使用SID从Active Directory中检索用户详细信息

当我有他/她的SID时,如何在我的AD中find一个用户? 我不想依赖其他属性,因为我试图检测这些变化。 例如:我收到一条关于用户logging更改的消息,其中包含:

Message: User Account Changed: Target Account Name: test12 Target Domain: DOMAIN Target Account ID: %{S-1-5-21-3968247570-3627839482-368725868-1110} Caller User Name: Administrator Caller Domain: DOMAIN Caller Logon ID: (0x0,0x62AB1) Privileges: - 

我想通知用户有关更改。 所以我需要他们的AD帐户信息。

启动Windows PowerShell并运行:

 $strSID="S-1-5-21-500000003-1000000000-1000000003-1001" $uSid = [ADSI]"LDAP://<SID=$strSID>" echo $uSid 

输出应该看起来像这样,

 distinguishedName : {CN=John Doe,OU=Domain Admins,OU=People,OU=xxx,DC=xxx} Path : LDAP://<SID=S-1-5-21-500000003-1000000000-1000000003-1001> 

执行此操作的“LDAP方法”是使用GUID(或SID)检索基础对象,该GUID(或SID)将只检索基础对象,不附加附加的类数据。 但是,从此基础对象中,您可以检索用户对象的实际“distinguishedName”。 使用“distinguishedName”属性检索用户对象将返回具有完整类数据的DirectoryEntry对象(.Net / C#/ PowerShell)或iadsUser对象(VBScript),并允许您获取所需的任何其他属性数据。

问题是检索与GUID(或SID)的初始对象。 有些来源会说你必须将string格式GUID(即{28c67c50-9778-47a4-a77a-bf56f238a0c4})转换成字节数组的string表示forms(即“\ 50 \ 7c \ c6 \ 28 \ 78 \ 97 \ a4 \ 47 \ 7a \ a7 \ bf \ 56 \ f2 \ 38 \ a0 \ c4“)传递给LDAP。 根据微软的文档 ,情况并非如此。 GUID / SID的简单string表示就足够了。

以下是如何通过GUID绑定到对象的示例,然后使用完整的类数据检索实际的用户对象。 如果你使用GUID进行绑定,Powershell实际上会拉动完整的对象。 如果您使用VBScript,那么您将需要执行两个步骤的过程。

另外,请注意,尽pipeMicrosoft文档说可以接受多个GUIDstring格式,但我已经能够成功使用的唯一一个是删除{}字符。 此外 ,请注意这不是一个正确的“字节数组”string,但只是GUIDstring剥离的特殊字符。

 $strGUID = "{28c67c50-9778-47a4-a77a-bf56f238a0c4}" -replace '-|{|}','' $guid = [ADSI]"LDAP://<GUID=$strGUID>" $user = [ADSI]$guid.distinguishedName 

SID绑定可以使用相同的过程。 描述这个MSDN页面说有几个可用的fstring格式,但最常见的将是s-1-5 -…-…-…-…格式。

 #Powershell $strSID="S-1-5-21-500000003-1000000000-1000000003-1001" $uSid = [ADSI]"LDAP://<SID=$uSid>" $user = [ADSI]$user.distinguishedName 

*查询*

如果要执行LDAP查询来查找对象(例如,通过将“objectGUID”与字节数组或“objectSID”与字节数组进行比较),也就是当您需要执行“正确的”字节数时,数组转换。 请注意,字节数组的顺序与string表示forms不同,因为它以GUID的DWORD-WORD-WORD-WORD-BYTESforms存储,并考虑到端序。 转换SID的字节数组也有类似的结果。

有几种不同的方法来完成转换,Technet有一个简单的VBScriptalgorithm 。 你也可以使用System.Guid,或者通过PowerShell中的一个简单的脚本,用C#/ VB.Net做一些更好的事情(需要爱PowerShell!):

 #Powershell # Creates a new System.GUID object from the supplied string. # Only need for this example. $guid = [system.guid]"{28c67c50-9778-47a4-a77a-bf56f238a0c4}" $out="" #Formats the array of integers as a backslash-delimited string of Hex values $guid.ToByteArray() | %{ $out += $("\{0:x2}" -f $_) } 

然后,您应该能够使用标准的LDAPfilter查询对象:

 (&(objectClass=User)(objectGUID=\50\7c\c6\28\78\97\a4\47\a7\7a\bf\56\f2\38\a0\c4)) 

…或者其他任何您可能要查询的内容。 这也适用于SID。

好。 我find了一个通过Active Directory来做到这一点的方法。 这里的代码是:

 REM Converts the SID into a format, that can be processed by ADSI or WMI Function NormalizeSid(strSidToNormalize) Dim regEx,strReplace strReplace="" ' Create regular expression. Set regEx = New RegExp regEx.Global = True regEx.Pattern = "(%|{|})" regEx.IgnoreCase = True ' Make replacement. NormalizeSid = regEx.Replace(strSidToNormalize, strReplace) End Function REM Searches for a SID the in the Message that was passed as argument REM SID returned will be of the form %{S-1-5-21-3968247570-3627839482-368725868-1110} REM NOTE: Neither WMI nor ADSI will accept this. Use NormalizeSid like in FindUser Function FindSidInMessage(Message) Dim strAccountRegex Dim objRegex Dim objMatch Dim strSID strAccountRegex = "(\%\{S\-[,0-9,\-]*\})" Set objRegex = new RegExp objRegex.Pattern= strAccountRegex for each objMatch in objRegex.Execute(Message) REM Wscript.StdOut.writeLine "Found an Account ID: " & objMatch.value strSID=objMatch.value next FindSidInMessage=strSID End Function REM Searches Directory for the User matching the SID passed as parameter Function FindUser(userSID) Dim normalizedSID Dim objUser normalizedSID=NormalizeSid(userSID) Wscript.Echo "SID after escaping: " & normalizedSID Wscript.StdOut.writeLine "Querying AD to retrieve user-data" Set objUser = GetObject("LDAP://<SID="& normalizedSID & ">") FindUser=objUser.EmailAddress End Function 

希望这对其他人有用。

使用PS:

 $SID = "SXX-XX-XXXXXXXXXX-XXXXXXXXXX-XXXXXXXXXX-XXXX" Get-ADObject -IncludeDeletedObjects -Filter * -Properties * | where{$_.objectSid -eq $SID}