|
# Inspired by https://stackoverflow.com/questions/41267553/powershell-test-connection-failed-due-to-lack-of-resources |
|
|
|
# See PowerShell version |
|
Get-Host | Select-Object Version |
|
|
|
$destinationIpOrComputerName = "MyDestinationIpOrComputerName" |
|
$port = 7834 # destination TCP port |
|
|
|
# Test-Connection (easy to use, often not reliable) |
|
# checks DNS resolution |
|
# gives IP of destination |
|
Test-Connection $destinationIpOrComputerName -IPv4 |
|
|
|
# Test-Connection (easy to use, often not reliable) |
|
# deterimines if can connect to destination TCP port |
|
Test-Connection $destinationIpOrComputerName -TCPPort $port |
|
|
|
# Use .NET TcpClient to verify port (reliable) |
|
# if error mentions "No such host is known" |
|
# did not resolve the IP or hostname on the network |
|
# if error mentions "...because the target machine actively refused it...", |
|
# resolved the IP or hostname but port is blocked or non-existent |
|
(New-Object System.Net.Sockets.TcpClient($destinationIpOrComputerName, $port)).Connected |
|
|
|
# Verify DNS resolving (will only look at DNS table). NSLOOKUP is more accurate |
|
# than PING since it only looks at DNS while PING looks at DNS, NetBIOS, and WINS. |
|
# If DNS ultimately will not resolve, it is a mis-configuration on the part of |
|
# DNS server. Local IT will need to get involved. Much of this came from the site |
|
# https://social.technet.microsoft.com/Forums/en-US/f82b747e-6711-418b-9b6f-317223daa2dc/access-computers-by-ip-addresses-but-not-by-computer-name?forum=w7itpronetworking |
|
nslookup <pcname> |
|
ping <pcname> |
|
|
|
# Handy Commands |
|
ipconfig /all |
|
ipconfig /release |
|
ipconfig /renew |
|
ipconfig /flushdns |
|
ipconfig /registerdns |
|
|
|
# If nothing above works, try these lower-level calls |
|
$ping1 = ` |
|
Get-WmiObject ` |
|
-Class Win32_PingStatus ` |
|
-Filter "Address='$destinationIpOrComputerName' AND Timeout=1000" |
|
$ping2 = ` |
|
Get-CimInstance ` |
|
-ClassName Win32_PingStatus ` |
|
-Filter "Address='$destinationIpOrComputerName' AND Timeout=1000" |
|
$statusCodes = @{ |
|
[uint32]0 = 'Success'; |
|
[uint32]11001 = 'Buffer Too Small'; |
|
[uint32]11002 = 'Destination Net Unreachable'; |
|
[uint32]11003 = 'Destination Host Unreachable'; |
|
[uint32]11004 = 'Destination Protocol Unreachable'; |
|
[uint32]11005 = 'Destination Port Unreachable'; |
|
[uint32]11006 = 'No Resources'; |
|
[uint32]11007 = 'Bad Option'; |
|
[uint32]11008 = 'Hardware Error'; |
|
[uint32]11009 = 'Packet Too Big'; |
|
[uint32]11010 = 'Request Timed Out'; |
|
[uint32]11011 = 'Bad Request'; |
|
[uint32]11012 = 'Bad Route'; |
|
[uint32]11013 = 'TimeToLive Expired Transit'; |
|
[uint32]11014 = 'TimeToLive Expired Reassembly'; |
|
[uint32]11015 = 'Parameter Problem'; |
|
[uint32]11016 = 'Source Quench'; |
|
[uint32]11017 = 'Option Too Big'; |
|
[uint32]11018 = 'Bad Destination'; |
|
[uint32]11032 = 'Negotiating IPSEC'; |
|
[uint32]11050 = 'General Failure' |
|
}; |
|
$statusCodes[$ping1.StatusCode]; |
|
$statusCodes[$ping2.StatusCode]; |