How to Use PowerShell to Retrieve an Object's SID from Active Directory Domain Service

How to Use PowerShell to Retrieve an Object's SID from Active Directory Domain Service

FIM ScriptBox Item

Summary

This script retrieves the user-friendly string representation of an object's SID from Active Directory Domain Service.

Script Code

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
#----------------------------------------------------------------------------------------------------------
 set-variable -name URI -value "http://localhost:5725/resourcemanagementservice"     -option constant 
 set-variable -name DN -value "LDAP://CN=Britta Simon,OU=FIMObjects,DC=Fabrikam,DC=Com" -option constant 
#----------------------------------------------------------------------------------------------------------
 If(@(get-pssnapin | where-object {$_.Name -eq "FIMAutomation"} ).count -eq 0) {add-pssnapin FIMAutomation}
#----------------------------------------------------------------------------------------------------------
 $AdUser = [ADSI]($DN)
 If($AdUser.objectGuid -eq $null) {Throw "Object not found in Active Directory"}
 $UserSid  = New-Object System.Security.Principal.SecurityIdentifier($AdUser.objectSid[0], 0)
 $Nt4Name  = $UserSid.Translate([System.Security.Principal.NTAccount])
 $Nt4Domain = ($Nt4Name.Value.Split("\"))[0]
 $Nt4Account = ($Nt4Name.Value.Split("\"))[1]
#----------------------------------------------------------------------------------------------------------
 Clear-Host
 Write-Host "User Data"
 Write-Host "========="
 $DataRecord = New-Object PSObject
 $DataRecord | Add-Member NoteProperty "DN" $DN
 $DataRecord | Add-Member NoteProperty "SamAccountName" ($Nt4Name.Value.Split("\"))[1]
 $DataRecord | Add-Member NoteProperty "Domain" ($Nt4Name.Value.Split("\"))[0]
 $DataRecord | Add-Member NoteProperty "SID" $($UserSid.ToString())
 $DataRecord | Format-List
#----------------------------------------------------------------------------------------------------------
 Trap 
 { 
  Write-Host "`nError: $($_.Exception.Message)`n" -foregroundcolor white -backgroundcolor darkred
  Exit 1
 }
#----------------------------------------------------------------------------------------------------------

 

 

note Note
To provide feedback about this article, create a post on the FIM TechNet Forum.
For more FIM related Windows PowerShell scripts, see the  FIM ScriptBox

 

Leave a Comment
  • Please add 3 and 6 and type the answer here:
  • Post
Wiki - Revision Comment List(Revision Comment)
Sort by: Published Date | Most Recent | Most Useful
Comments
  • Richard Mueller edited Revision 5. Comment: Removed (en-US) from title

  • Craig Lussier edited Revision 4. Comment: added en-US to tags and title

  • Ed Price MSFT edited Revision 3. Comment: Added tags. Our title standard is to use "How to" and no gerunds.

Page 1 of 1 (3 items)
Wikis - Comment List
Sort by: Published Date | Most Recent | Most Useful
Posting comments is temporarily disabled until 10:00am PST on Saturday, December 14th. Thank you for your patience.
Comments
  • Ed Price MSFT edited Revision 3. Comment: Added tags. Our title standard is to use "How to" and no gerunds.

  • Craig Lussier edited Revision 4. Comment: added en-US to tags and title

  • Richard Mueller edited Revision 5. Comment: Removed (en-US) from title

Page 1 of 1 (3 items)