Skip to content

Instantly share code, notes, and snippets.

@JohnLBevan
Created October 28, 2024 11:20
Show Gist options
  • Save JohnLBevan/da8ca7df37b5987191f0c0495409ce23 to your computer and use it in GitHub Desktop.
Save JohnLBevan/da8ca7df37b5987191f0c0495409ce23 to your computer and use it in GitHub Desktop.
Convert an Azure Active Directory / MS Entra / AAD SID to Object Id. Entra doesn't store the SID natively (the OnPremiseSecurityIdentifier is the AD SID in hybrid environments, but has no comptable link to the Entra SID), so if given a SID we need to convert it to an ObjectId before we can use that to find the correspnding Entra object (could be…
# Thanks to https://github.com/okieselbach/Intune/blob/master/Convert-AzureAdSidToObjectId.ps1
Function Convert-EntraSidToObjectId {
[CmdletBinding()]
Param(
[Parameter(ValueFromPipeline)]
[String]$Sid
)
Process {
$text = $sid.Replace('S-1-12-1-', '')
$array = [UInt32[]]$text.Split('-')
$bytes = New-Object 'Byte[]' 16
[Buffer]::BlockCopy($array, 0, $bytes, 0, 16)
([PSCustomObject]@{
SID = $Sid
ObjectId = [Guid]$bytes
})
}
}
$sid = 'S-1-12-1-#########-##########-##########-##########'
$x = Convert-EntraSidToObjectId $sid
$x | Format-Table -AutoSize
Get-AzAdUser -ObjectId $x.ObjectId -ErrorAction SilentlyContinue
Get-AzAdGroup -ObjectId $x.ObjectId -ErrorAction SilentlyContinue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment