-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGet-AzureADDeviceID.ps1
More file actions
63 lines (52 loc) · 3.06 KB
/
Get-AzureADDeviceID.ps1
File metadata and controls
63 lines (52 loc) · 3.06 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
function Get-AzureADDeviceID {
<#
.SYNOPSIS
Get the Azure AD device ID from the local device.
.DESCRIPTION
Get the Azure AD device ID from the local device.
.NOTES
Author: Nickolaj Andersen
Contact: @NickolajA
Created: 2021-05-26
Updated: 2023-06-20
Version history:
1.0.0 - (2021-05-26) Function created
1.0.1 - (2022-10-20) @AzureToTheMax - Fixed issue pertaining to Cloud PCs (Windows 365) devices ability to locate their AzureADDeviceID.
1.0.2 - (2023-06-20) @AzureToTheMax - Fixed issue pertaining to Cloud PCs (Windows 365) devices where the reported AzureADDeviceID was in all capitals, breaking signature creation.
#>
Process {
# Define Cloud Domain Join information registry path
$AzureADJoinInfoRegistryKeyPath = "HKLM:\SYSTEM\CurrentControlSet\Control\CloudDomainJoin\JoinInfo"
# Retrieve the child key name that is the thumbprint of the machine certificate containing the device identifier guid
$AzureADJoinInfoThumbprint = Get-ChildItem -Path $AzureADJoinInfoRegistryKeyPath | Select-Object -ExpandProperty "PSChildName"
if ($AzureADJoinInfoThumbprint -ne $null) {
# Retrieve the machine certificate based on thumbprint from registry key
$AzureADJoinCertificate = Get-ChildItem -Path "Cert:\LocalMachine\My" -Recurse | Where-Object { $PSItem.Thumbprint -eq $AzureADJoinInfoThumbprint }
if ($AzureADJoinCertificate -ne $null) {
# Determine the device identifier from the subject name
$AzureADDeviceID = ($AzureADJoinCertificate | Select-Object -ExpandProperty "Subject") -replace "CN=", ""
# Convert upper to lowercase.
$AzureADDeviceID = "$($AzureADDeviceID)".ToLower()
# Handle return value
return $AzureADDeviceID
} else {
#If no certificate was found, locate it by Common Name instead of Thumbprint. This is likely a CPC or similar.
$AzureADJoinCertificate = Get-ChildItem -Path "Cert:\LocalMachine\My" -Recurse | Where-Object { $PSItem.Subject -like "CN=($AzureADJoinInfoThumbprint)" }
if ($AzureADJoinCertificate -ne $null){
# Cert is now found, extract Device ID from Common Name
$AzureADDeviceID = ($AzureADJoinCertificate | Select-Object -ExpandProperty "Subject") -replace "CN=", ""
# Convert upper to lowercase.
$AzureADDeviceID = "$($AzureADDeviceID)".ToLower()
# Handle return value
return $AzureADDeviceID
} else {
# Last ditch effort, try and use the ThumbPrint (reg key) itself.
$AzureADDeviceID=$AzureADJoinInfoThumbprint
# Convert upper to lowercase.
$AzureADDeviceID = "$($AzureADDeviceID)".ToLower()
return $AzureADDeviceID
}
}
}
}
}