Skip to content

Commit d4e3ac4

Browse files
committed
FEAT: Add Get-ComputerIDP.ps1 script to retrieve identity provider information for endpoints (#24)
1 parent eedb826 commit d4e3ac4

1 file changed

Lines changed: 113 additions & 0 deletions

File tree

Inventory/Get-ComputerIDP.ps1

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
<#
2+
Summary: Determines the identity provider(s) the endpoint is joined to (AD DS and/or Entra ID) and emits a JSON report with the relevant identifiers.
3+
Script Type: Device Inventory-Metascript
4+
Dependencies: Invoke-ImmyCommand
5+
Author: GitHub Copilot
6+
#>
7+
8+
function Get-DomainJoinData {
9+
<# Retrieves domain membership info from the endpoint via CIM. #>
10+
Invoke-ImmyCommand {
11+
try {
12+
$system = Get-CimInstance -ClassName Win32_ComputerSystem -ErrorAction Stop
13+
[hashtable]@{
14+
PartOfDomain = [bool]$system.PartOfDomain
15+
Domain = $system.Domain
16+
}
17+
} catch {
18+
$null
19+
}
20+
}
21+
}
22+
23+
function Get-EntraJoinData {
24+
<# Parses dsregcmd output on the endpoint to capture Entra ID join metadata. #>
25+
Invoke-ImmyCommand {
26+
$exe = Join-Path $env:SystemRoot 'System32\dsregcmd.exe'
27+
if (-not (Test-Path $exe)) {
28+
return $null
29+
}
30+
31+
$statusLines = & $exe /status 2>$null
32+
if (-not $statusLines) {
33+
return $null
34+
}
35+
36+
$data = [hashtable]@{
37+
AzureAdJoined = $null
38+
TenantId = $null
39+
TenantName = $null
40+
DeviceId = $null
41+
}
42+
43+
foreach ($line in $statusLines) {
44+
if (-not $data.AzureAdJoined -and $line -match 'AzureAdJoined\s*:\s*(\w+)') {
45+
$data.AzureAdJoined = $matches[1].Trim().ToUpperInvariant()
46+
continue
47+
}
48+
49+
if (-not $data.TenantId -and $line -match 'TenantId\s*:\s*([0-9a-fA-F-]+)') {
50+
$data.TenantId = $matches[1].Trim()
51+
continue
52+
}
53+
54+
if (-not $data.TenantName -and $line -match 'TenantName\s*:\s*(.+)$') {
55+
$data.TenantName = $matches[1].Trim()
56+
continue
57+
}
58+
59+
if (-not $data.DeviceId -and $line -match 'DeviceId\s*:\s*([0-9a-fA-F-]+)') {
60+
$data.DeviceId = $matches[1].Trim()
61+
}
62+
}
63+
64+
$data
65+
}
66+
}
67+
68+
$domainInfo = Get-DomainJoinData
69+
$entraInfo = Get-EntraJoinData
70+
71+
$hasAdDomain = $false
72+
$adDomainName = $null
73+
$adPartOfDomain = $false
74+
75+
if ($domainInfo -and $domainInfo.PartOfDomain -and $domainInfo.Domain) {
76+
$hasAdDomain = $true
77+
$adDomainName = $domainInfo.Domain
78+
$adPartOfDomain = $domainInfo.PartOfDomain
79+
}
80+
81+
$hasEntraJoin = $false
82+
$entraTenantId = $null
83+
$entraTenantName = $null
84+
$entraDeviceId = $null
85+
86+
if ($entraInfo -and $entraInfo.AzureAdJoined -eq 'YES' -and $entraInfo.TenantId) {
87+
$hasEntraJoin = $true
88+
$entraTenantId = $entraInfo.TenantId
89+
$entraTenantName = $entraInfo.TenantName
90+
$entraDeviceId = $entraInfo.DeviceId
91+
}
92+
93+
$idpType = 'Unknown'
94+
if ($hasAdDomain -and $hasEntraJoin) {
95+
$idpType = 'Hybrid (AD DS + Entra ID)'
96+
} elseif ($hasAdDomain) {
97+
$idpType = 'AD DS'
98+
} elseif ($hasEntraJoin) {
99+
$idpType = 'Entra ID'
100+
}
101+
102+
$result = [hashtable]@{
103+
ComputerName = $ComputerName
104+
IdentityProvider = $idpType
105+
AdDomainName = $adDomainName
106+
AdPartOfDomain = $adPartOfDomain
107+
EntraTenantId = $entraTenantId
108+
EntraTenantName = $entraTenantName
109+
EntraDeviceId = $entraDeviceId
110+
GeneratedOnUtc = (Get-Date).ToUniversalTime().ToString('o')
111+
}
112+
113+
$result

0 commit comments

Comments
 (0)