Posted On May 26, 2020

PowerShell: Detect Whether Computer is Connected To Domain

kimconnect 0 comments
blog.KimConnect.com >> Codes >> PowerShell: Detect Whether Computer is Connected To Domain
# The easy method
$domainConnected=.{
            try {
                [void]::([System.DirectoryServices.ActiveDirectory.Domain]::GetComputerDomain())
                return $true
                }
            catch{
                return $false
                }
}
# The hard way
$otherKnownDnsServers='192.168.500.9000','192.168.500.9001'
$domainConnected=.{
            $regexIpv4 = "\b(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b"       
            $netdom=netdom query /d:$env:USERDNSDOMAIN DC|?{$_.trim() -notlike "" -and $_ -notmatch '\s'}
            $dcIps=$netdom|%{([System.Net.Dns]::GetHostAddresses($_)).ipaddresstostring}|?{$_ -match $regexIpv4 -and $_ -notmatch "^169."}|sort
            $dcIps+=$otherKnownDnsServers
            $x=Get-WmiObject Win32_NetworkAdapterConfiguration | select DNSServerSearchOrder|out-string
            $x=$x -split '\r?\n'
            $dns=[array]($x | Select-String -Pattern $regexIpv4 -AllMatches).matches.value
	        foreach ($ip in $dns){if ($ip -in $dcIps){return $true}}
            }

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Post

PowerShell: Create Mailbox Report on Office 365

# connect-to-exchange-online.ps1 # How to connect to Office 365 Cloud Services using PowerShell # Office…

Hyper-V: Creating a New Virtual Machine

# Compulsory variables $hyperVHost='HYPERV007' $vmName='WindowsGoldenImage' $parentDirectory='C:\ClusterStorage\Volume5' $disk1Size='100GB' $memoryAllocation='8GB' $networkSwitch='PublicZone' $vlan='1005' $clusterName='DEV-CLUSTER05' # Optional variables $disk2Size=$false…

PowerShell: Get Windows Resource Utilization

# getWindowsResourceUtilization.ps1 # version 0.02 # Gather information on a list of Windows Machines #…