Skip to content

combining 2 in 1 #9

Description

@droushd

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

}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions