Wednesday, August 2, 2023

Immutable ID, ms-ds-consistencyGUID, and object GUID conversions

It seems like I'm constantly need to convert immutable IDs and GUIDs as part of my M365 migrations. To simplify this I finally got around to writing some functions that simplify the work instead of looking them up all the time.

I created the following:

  • Convert-ImmutableIDToGUID
  • Convert-ImmutableIDtoHexString
  • Convert-ImmutableIDtoByteArray
  • Convert-ByteArrayToGUID
  • Convert-GUIDToImmutableID
  • Convert-HexStringToGUID
  • Convert-HexStringToImmutableID
  • Convert-ByteArrayToImmutableID

To make these available at a powershell prompt, you can load them as part of your powershell profile or dot source a script that contains.

Example of dot sourcing:

. c:\scripts\convertfunctions.ps1

Code for the functions:

# Example immutable ID to play with
# $ImmutableID = "GJo33fsMIUKvmIIyTOSjzg=="

function Convert-ImmutableIDToGUID {
    param ($ImmutableID)
    $guid=[Guid]([Convert]::FromBase64String($ImmutableID))
    return $guid
}


function Convert-ImmutableIDtoHexString {
    param ($ImmutableID)
    $hexstring=([Convert]::FromBase64String($ImmutableID) | ForEach-Object ToString X2) -join ' '
    return $hexstring
}


function Convert-ImmutableIDtoByteArray {
    param ($ImmutableID)
    $bytearray=[Convert]::FromBase64String($ImmutableID)
    return $bytearray
}


#When you retrieve ms-ds-consistencyGUID from AD it is a byte array
#to avoid this conversion use [guid]$user.'ms-ds-consistencyGUID'
function Convert-ByteArrayToGUID {
    param ($bytearray)
    $guid=[Guid]([Convert]::FromBase64String([system.convert]::ToBase64String($bytearray)))
    return $guid
}

#works with GUID object or string in GUID format
function Convert-GUIDToImmutableID {
    param ($guid)
    $immutableID = [system.convert]::ToBase64String(([GUID]$guid).ToByteArray())
    return $immutableID
}


function Convert-HexStringToGUID {
    param ($hexstring)
    $guid = [GUID]([byte[]] (-split (($hexstring -replace " ", "") -replace '..', '0x$& ')))
    return $guid
}


function Convert-HexStringToImmutableID {
    param ($hexstring)
    $ImmutableID = [system.convert]::ToBase64String([byte[]] (-split (($hexstring -replace " ", "") -replace '..', '0x$& ')))
    return $ImmutableID
}


function Convert-ByteArrayToImmutableID {
    param ($bytearray)
    $ImmutableID = [system.convert]::ToBase64String($bytearray)
    return $ImmutableID
}

<# Use Example

$msdsconsistencyGUID = (Get-ADUser Byron -properties *).ms-ds-consistencyGUID
$ImmutableID = Convert-ByteArrayToImmutableID -bytearray $msdsconsistencyGUID
Set-AzureADUser byron@domain.com -ImmutableID $ImmutableID

#>

8 comments:

  1. "Great list of functions! This will save a lot of time during migrations."
    Franchise Show
    Franchise Show Mumbai

    ReplyDelete
  2. "Can you explain the difference between ImmutableID and GUID in more detail?"
    Franchise Show Bangaluru
    Tubular Screw Conveyor delhi

    ReplyDelete
  3. "It would be helpful if you could share some examples of using these functions."
    Loss in weight Feeder delhi
    pneumatic vibrator delhi

    ReplyDelete
  4. "This is exactly what I needed for my current migration project—thanks!"
    Warehouse Storage rack Delhi
    mezzanine floor Delhi

    ReplyDelete
  5. "Do these functions work with older versions of PowerShell as well?"
    mobile compactor India
    fifo flow rack delhi

    ReplyDelete
  6. "Including a downloadable script file for these functions would be amazing!"
    heavy duty rack
    Multi tier rack

    ReplyDelete
  7. "Could you add some information on error handling in these functions?"
    Slotted angle rack manufacturer
    pallet rack supplier

    ReplyDelete
  8. "How would you recommend integrating these with automated scripts?"
    Dust Collector
    Best Business opportunities

    ReplyDelete