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
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