Skip to content

Instantly share code, notes, and snippets.

@msmorul
Created November 5, 2015 17:06
Show Gist options
  • Save msmorul/d9680acc4f64d422d378 to your computer and use it in GitHub Desktop.
Save msmorul/d9680acc4f64d422d378 to your computer and use it in GitHub Desktop.
Example script showing how to create the aut-generated unix uid for centrify
param (
[string]$username = $(throw "-username is required.")
)
#
# Load Account Details
#
$user = Get-ADObject -Filter {(sAMAccountName -eq $username)} -Properties 'ObjectSID','uidNumber'
$sidStr = $user.ObjectSID
"Found ObjectSID: {0} for {1}" -f $sidStr,$username
#
# Extract SID and RID for an account
#
$match = $sidStr.Value -match '^S-1-5-\d{2}-\d+-(?<sid>\d{4,10})-\d+-(?<rid>\d+)$'
$sid = [uint32]$matches['sid']
$rid = [uint32]$matches['rid']
"Found SID {0} and RID {1}, now see how the binary version lines up `n`r SID: {2,32}`r`n RID: {3,32}" -f $sid,$rid,[System.Convert]::ToString($sid,2),[System.Convert]::ToString($rid,2)
#
# only bottom 9 bits from sid to use as a prefix.
#
$sid = $sid -shl 22
#
# Remove top 9 bits of rid
#
$rid = ($rid -shl 9) -shr 9
#
# Add them together for final uid
#
$uidNumber = $sid + $rid
"Now grab the bottom 9 bits of the sid and bottom 22 of the rid `r`n SID: {0,32}`r`n RID: {1,32}`r`n UID: {2,32}`r`n`r`n UID: {3}" -f [System.Convert]::ToString($sid,2),[System.Convert]::ToString($rid,2),[System.Convert]::ToString($uidNumber,2),$uidNumber
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment