Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

combining 2 in 1 #9

Open
droushd opened this issue Mar 29, 2021 · 0 comments
Open

combining 2 in 1 #9

droushd opened this issue Mar 29, 2021 · 0 comments

Comments

@droushd
Copy link

droushd commented Mar 29, 2021

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

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant