# Define a helper function to extract the domain part of an email address function Get-Domain { param([string]$email) return $email.Split("@")[1] } # Define the base path for JoinInfo and TenantInfo $joinInfoBasePath = "Registry::HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\CloudDomainJoin\JoinInfo" $tenantInfoBasePath = "Registry::HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\CloudDomainJoin\TenantInfo" # Get all GUID-based subkeys under JoinInfo if (Test-Path $joinInfoBasePath) { $joinInfoGuids = Get-ChildItem -Path $joinInfoBasePath foreach ($joinInfoGuid in $joinInfoGuids) { $joinInfoPath = "$joinInfoBasePath\$($joinInfoGuid.PSChildName)" $joinInfoValues = Get-ItemProperty -Path $joinInfoPath # Check if the UserEmail key exists in JoinInfo if ($joinInfoValues.PSObject.Properties['UserEmail']) { $userEmail = $joinInfoValues.UserEmail $joinDomain = Get-Domain -email $userEmail Write-Host "Found UserEmail: $userEmail in JoinInfo GUID: $($joinInfoGuid.PSChildName)" # Now check the TenantInfo path for matching TenantId if (Test-Path $tenantInfoBasePath) { $tenantInfoGuids = Get-ChildItem -Path $tenantInfoBasePath foreach ($tenantInfoGuid in $tenantInfoGuids) { $tenantInfoPath = "$tenantInfoBasePath\$($tenantInfoGuid.PSChildName)" $tenantInfoValues = Get-ItemProperty -Path $tenantInfoPath # Check if the TenantId key exists in TenantInfo and matches the TenantId from JoinInfo if ($tenantInfoValues.TenantId -eq $joinInfoValues.TenantId) { Write-Host "Match found for TenantId in TenantInfo GUID: $($tenantInfoGuid.PSChildName)" Write-Host "Processing additional checks if required..." # Add additional checks or processing here if needed } else { Write-Host "No matching TenantId found for JoinInfo GUID: $($joinInfoGuid.PSChildName) in TenantInfo GUID: $($tenantInfoGuid.PSChildName)" } } } else { Write-Host "TenantInfo path does not exist." } } else { Write-Host "No UserEmail found in JoinInfo GUID: $($joinInfoGuid.PSChildName)" } } } # If no mismatching domains are found, exit with success (0) Write-Host "Script completed." exit 0