-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathConvert-GuidFormat.ps1
More file actions
105 lines (84 loc) · 3.38 KB
/
Copy pathConvert-GuidFormat.ps1
File metadata and controls
105 lines (84 loc) · 3.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
function Convert-GuidFormat {
<#
.SYNOPSIS
Converts between standard GUID, ImmutableId (Base64), and hex byte representations.
.DESCRIPTION
Automatically detects the input format and returns all three representations of the same
GUID. Useful when working with Entra Connect, Entra ID, and Active Directory, where
the same object identifier appears in different encodings depending on the tool.
Supported formats:
- Standard GUID string 25b5dd02-494c-452f-bbc2-54ff54bb861c
- ImmutableId (Base64) At21JUxJL0W7wlT/VLuGHA==
- Hex bytes with spaces 02 DD B5 25 4C 49 2F 45 BB C2 54 FF 54 BB 86 1C
.PARAMETER InputValue
A string containing a GUID in any of the three supported formats.
Accepts pipeline input.
.EXAMPLE
Convert-GuidFormat "25b5dd02-494c-452f-bbc2-54ff54bb861c"
GuidString : 25b5dd02-494c-452f-bbc2-54ff54bb861c
ImmutableId : At21JUxJL0W7wlT/VLuGHA==
GuidHex : 02 DD B5 25 4C 49 2F 45 BB C2 54 FF 54 BB 86 1C
.EXAMPLE
Convert-GuidFormat "At21JUxJL0W7wlT/VLuGHA=="
Converts an ImmutableId (Base64) value to all three formats.
.EXAMPLE
Convert-GuidFormat "02 DD B5 25 4C 49 2F 45 BB C2 54 FF 54 BB 86 1C"
Converts a hex byte string (as seen in LDAP editors) to all three formats.
.EXAMPLE
"At21JUxJL0W7wlT/VLuGHA==", "25b5dd02-494c-452f-bbc2-54ff54bb861c" | Convert-GuidFormat
Accepts multiple values from the pipeline.
.INPUTS
System.String
.OUTPUTS
PSCustomObject with properties: GuidString, ImmutableId, GuidHex
.NOTES
Author: Mike Crowley
https://mikecrowley.us
.LINK
https://learn.microsoft.com/en-us/entra/identity/hybrid/connect/plan-connect-design-concepts#sourceanchor
#>
[CmdletBinding()]
[OutputType([PSCustomObject])]
param(
[Parameter(Mandatory, Position = 0, ValueFromPipeline)]
[ValidateNotNullOrEmpty()]
[string]$InputValue
)
process {
$Guid = $null
# Method 1: Standard GUID string
try { $Guid = [Guid]$InputValue } catch { }
# Method 2: ImmutableId (Base64)
if ($null -eq $Guid) {
try {
$bytes = [Convert]::FromBase64String($InputValue)
if ($bytes.Length -eq 16) { $Guid = [Guid]::new($bytes) }
}
catch { }
}
# Method 3: Hex bytes with spaces
if ($null -eq $Guid) {
try {
$hexValues = $InputValue.Split(' ', [StringSplitOptions]::RemoveEmptyEntries)
if ($hexValues.Count -eq 16) {
$bytes = [byte[]]::new(16)
for ($i = 0; $i -lt 16; $i++) {
$bytes[$i] = [Convert]::ToByte($hexValues[$i], 16)
}
$Guid = [Guid]::new($bytes)
}
}
catch { }
}
if ($null -ne $Guid) {
[PSCustomObject]@{
GuidString = $Guid.ToString()
ImmutableId = [Convert]::ToBase64String($Guid.ToByteArray())
GuidHex = [BitConverter]::ToString($Guid.ToByteArray()).Replace("-", " ")
}
}
else {
Write-Error "Cannot parse '$InputValue' as a GUID, ImmutableId, or hex byte string."
}
}
}