Public/OS/Get-DomainInfo.ps1
<#
Copyright © 2024 Integris. For internal company use only. All rights reserved. #> FUNCTION Get-DomainInfo { <# .SYNOPSIS Retrieves and displays domain information for the current system. .DESCRIPTION This function collects various details about the system's domain status, including whether it is joined to an Active Directory (AD) domain, Azure AD (AAD), or a workgroup. It also checks the health of the domain join and resolves domain controllers if applicable. .PARAMETER DomainServicesRegistration The output of the dsregcmd /status command, used to determine the domain join status. .EXAMPLE Get-DomainInfo This command retrieves and displays domain information for the current system, including domain type, FQDN, and domain join health. .NOTES This function uses CIM instances and DNS resolution to gather domain information and may require appropriate permissions to execute. #> [CmdletBinding()] PARAM ( ) ### Declare Variables $Results = @() $DomainFQDN = $Null $DomainResolves = $False $DomainType = $Null $DomainServicesRegistration = dsregcmd /status $ResolvedIPs = @() $DomainControllers = @() $DomainJoinHealth = $Null ### Begin Script IF ($DomainServicesRegistration -like "*DomainJoined : YES*") { $DomainType = "AD" } ELSEIF ($DomainServicesRegistration -like "*AzureAdJoined : YES*" -and $DomainServicesRegistration -like "*DomainJoined : NO*") { $DomainType = "AAD" } ELSEIF ($DomainServicesRegistration -like "*AzureAdJoined : NO*" -and $DomainServicesRegistration -like "*DomainJoined : NO*") { $DomainType = "Workgroup" } ELSE { RETURN $False } IF ($DomainType -eq "AD") { $DomainFQDN = (Get-CIMInstance Win32_ComputerSystem).Domain $Records = Resolve-DnsName $DomainFQDN -ErrorAction SilentlyContinue FOREACH ($Record in $Records) { IF ((Test-IsPrivateIP $Record.IPAddress) -or $Record.Name -like "*.local*" ) { $DomainResolves = $True; $ResolvedIPs += $Record.IPAddress }} FOREACH ($IP in $ResolvedIPs) { TRY { $DomainControllers += (((Resolve-DNSName $IP).NameHost).Replace(".$($DomainFQDN)","").ToUpper()) } CATCH {"[Name Not Available]" }} IF ($ResolvedIPs.Count -ge 1) { TRY { IF (Test-IsDomainController) { $DomainJoinHealth = "Domain Controller" } ELSEIF (Test-ComputerSecureChannel) { $DomainJoinHealth = "Healthy" } ELSE { $DomainJoinHealth = "Unhealthy" } } CATCH { $DomainJoinHealth = "Unhealthy" } } ELSE { $DomainJoinHealth = "Domain Unavailable" } $Results += New-Object PSObject -WarningAction SilentlyContinue -Property @{ Hostname = $env:COMPUTERNAME DomainType = $DomainType FQDN = $DomainFQDN TenantName = $Null TenantID = $Null ActiveDomainConnection = $DomainResolves ResolvedDomainControllerIP = $ResolvedIPs ResolvedDomainControllerName = $DomainControllers DomainJoinHealth = $DomainJoinHealth } RETURN $Results | Select-Object Hostname, DomainType, FQDN, ActiveDomainConnection, ResolvedDomainControllerIP, ResolvedDomainControllerName, DomainJoinHealth } ELSEIF ($DomainType -eq "AAD") { IF ((($DomainServicesRegistration | Select-String -Pattern "DeviceAuthStatus :").ToString().Replace("DeviceAuthStatus :","").Replace(" ","")) -eq "SUCCESS") { $DomainJoinHealth = "Healthy" } ELSE { $DomainJoinHealth = "Unhealthy" } $Results += New-Object PSObject -WarningAction SilentlyContinue -Property @{ Hostname = $env:COMPUTERNAME DomainType = $DomainType FQDN = "Login.Windows.Net" TenantName = ($DomainServicesRegistration | Select-String -Pattern " TenantName :").ToString().Replace("TenantName :","").Replace(" ","") TenantID = ($DomainServicesRegistration | Select-String -Pattern " TenantId :").ToString().Replace("TenantId :","").Replace(" ","") ActiveDomainConnection = $Null ResolvedDomainControllerIP = $Null ResolvedDomainControllerName = $Null DomainJoinHealth = $DomainJoinHealth } RETURN $Results | Select-Object Hostname, DomainType, FQDN, TenantName, TenantID, DomainJoinHealth } ELSEIF ($DomainType -eq "Workgroup") { $Results += New-Object PSObject -WarningAction SilentlyContinue -Property @{ Hostname = $env:COMPUTERNAME DomainType = $DomainType FQDN = $Null TenantName = $Null TenantID = $Null ActiveDomainConnection = $Null ResolvedDomainControllerIP = $Null ResolvedDomainControllerName = $Null DomainJoinHealth = $Null } RETURN $Results | Select-Object Hostname, DomainType } ELSE { $Results += New-Object PSObject -WarningAction SilentlyContinue -Property @{ Hostname = $env:COMPUTERNAME DomainType = "Error" FQDN = $Null TenantName = $Null TenantID = $Null ActiveDomainConnection = $Null ResolvedDomainControllerIP = $Null ResolvedDomainControllerName = $Null DomainJoinHealth = $Null } RETURN $Results | Select-Object Hostname, DomainType } } New-Alias -Name Get-DomainHealth -Value Get-DomainInfo |