combining 2 in 1
droushd opened this issue · 0 comments
droushd commented
function Convert-SIDGUID {
<#
.SYNOPSIS
This will help translate SIDS into GUIDS and vice versa. Checks if input is SID or GUID and returns the other.
.DESCRIPTION
Based on code from https://tech.nicolonsky.ch/validating-a-guid-with-powershell/ and https://oliverkieselbach.com/2020/05/13/powershell-helpers-to-convert-azure-ad-object-ids-and-sids/
.PARAMETER InputObject
The SID or GUID to convert
#>
param
(
[Parameter(Mandatory = $true, Position = 0, ValueFromPipelineByPropertyName = $true)]
[AllowEmptyString()]
[string]$InputObject
)
$tryguid = [guid]::TryParse($InputObject, $([ref][guid]::Empty))
if ($tryguid) {
$bytes = [Guid]::Parse($ObjectId).ToByteArray()
$array = New-Object 'UInt32[]' 4
[Buffer]::BlockCopy($bytes, 0, $array, 0, 16)
$sid = "S-1-12-1-$array".Replace(' ', '-')
return $sid
}
try {
$sid = New-Object System.Security.Principal.SecurityIdentifier($InputObject) | foreach {$_.Value}
$index = 'S-1-12-1-'.Length
$length = $sid.length - $index
$text = $sid.Substring($index,$length)
# $text = $sid.Replace('S-1-12-1-', '')
$array = [UInt32[]]$text.Split('-')
$bytes = New-Object 'Byte[]' 16
[Buffer]::BlockCopy($array, 0, $bytes, 0, 16)
[Guid]$guid = $bytes
return $guid
}
catch {
Return "No valid SID or GUID found"
}
}