When working with PowerShell's Test-Connection
cmdlet, you might encounter this particularly unhelpful error message:
System.Net.NetworkInformation.PingException: Testing connection to computer
'COMPUTER01' failed: A non-recoverable error occurred during a database
lookup ---> System.ComponentModel.Win32Exception: A non-recoverable error
occurred during a database lookup.
This occurs when trying to ping hosts that are offline or unreachable across subnets, while traditional ping
gives the more expected "Destination host unreachable" message.
Standard ICMP tools show clearer network status:
C:\\> ping COMPUTER01
Pinging COMPUTER01.domain.com [192.168.2.153] with 32 bytes of data:
Reply from 192.168.0.220: Destination host unreachable.
C:\\> tracert -d 192.168.2.153
7 40 ms 40 ms 40 ms 192.168.0.220
8 192.168.0.220 reports: Destination host unreachable.
The misleading error occurs because Test-Connection
performs additional host verification steps before ICMP:
- DNS resolution (confirmed working via nslookup)
- WINS lookup (if configured)
- NetBIOS name resolution
- Local hosts file parsing
Wireshark analysis shows the root cause is actually an ICMP Type 3 Code 1 response:
Type: 3 (Destination unreachable)
Code: 1 (Host unreachable)
The .NET framework's System.Net.NetworkInformation.Ping
class translates this into the misleading database error due to legacy Windows networking architecture.
For more reliable network testing in scripts, consider these alternatives:
# Option 1: Raw .NET Ping with proper error handling
$ping = New-Object System.Net.NetworkInformation.Ping
try {
$result = $ping.Send("192.168.2.153", 1000)
$result.Status.ToString()
} catch [System.Net.NetworkInformation.PingException] {
$_.Exception.InnerException.Message
}
# Option 2: Test-NetConnection (PowerShell 4.0+)
Test-NetConnection -ComputerName COMPUTER01 -InformationLevel Detailed
# Option 3: Custom function with fallback logic
function Test-HostConnection {
param([string]$ComputerName)
$result = Test-Connection -ComputerName $ComputerName -Count 1 -ErrorAction SilentlyContinue
if (-not $result) {
try {
$ip = [System.Net.Dns]::GetHostAddresses($ComputerName)[0].IPAddressToString
$ping = New-Object System.Net.NetworkInformation.Ping
$ping.Send($ip, 1000) | Select-Object Status,Address
} catch {
Write-Output "Host $ComputerName is unreachable (ICMP blocked or host down)"
}
}
}
When building deployment scripts or monitoring systems, proper error handling is crucial. The default Test-Connection
behavior can:
- Mask real network issues with misleading errors
- Cause false positives in monitoring systems
- Make troubleshooting more difficult
The solutions provided above give more accurate network diagnostics for scripting scenarios.
When working with PowerShell's Test-Connection
cmdlet, you might encounter this particularly confusing error:
System.Net.NetworkInformation.PingException: Testing connection to computer
'COMPUTER01' failed: A non-recoverable error occurred during a database
lookup ---> System.ComponentModel.Win32Exception: A non-recoverable error
occurred during a database lookup.
This message appears when pinging an offline host on a different subnet, while traditional ping
commands give more straightforward "Destination host unreachable" messages.
The root issue stems from how Test-Connection
handles DNS resolution differently than the standard ping utility. Here's the technical breakdown:
Test-Connection
performs additional verification steps after initial DNS resolution- The cmdlet attempts to validate reverse DNS records (PTR)
- When the target host is offline, these secondary lookups fail catastrophically
Network traces reveal the actual sequence:
1. DNS query for COMPUTER01 (successful)
2. ICMP echo request to resolved IP (192.168.2.153)
3. Router responds with ICMP Type 3 Code 1 (Host Unreachable)
4. Test-Connection attempts reverse DNS lookup (fails)
5. Error bubbles up with misleading message
Here are several approaches to handle this scenario in your PowerShell scripts:
Option 1: Wrap in Try-Catch with Custom Handling
try {
$result = Test-Connection -ComputerName $Computer -Count 1 -ErrorAction Stop
return $true
}
catch [System.Net.NetworkInformation.PingException] {
if ($_.Exception.InnerException.Message -match "database lookup") {
# Handle as unreachable host
return $false
}
throw # Re-throw other PingExceptions
}
Option 2: Use .NET's Ping Class Directly
function Test-HostReachability {
param([string]$ComputerName)
$ping = New-Object System.Net.NetworkInformation.Ping
try {
$reply = $ping.Send($ComputerName, 1000)
return $reply.Status -eq 'Success'
}
catch {
return $false
}
}
Option 3: Combine DNS Check with Ping
function Robust-TestConnection {
param([string]$ComputerName)
# First verify DNS resolution
try {
$null = [System.Net.Dns]::GetHostEntry($ComputerName)
}
catch {
return $false
}
# Then test connectivity
try {
$ping = New-Object System.Net.NetworkInformation.Ping
$reply = $ping.Send($ComputerName, 1000)
return $reply.Status -eq 'Success'
}
catch {
return $false
}
}
Understanding this behavior is crucial when:
- Building network inventory scripts
- Creating monitoring solutions
- Developing deployment automation
- Writing infrastructure tests
The standard Test-Connection
cmdlet might report false negatives in these scenarios unless properly handled. The solutions above provide more reliable alternatives for production scripts.
The "database lookup" reference in the error message specifically relates to:
- DNS resolver cache operations
- Reverse DNS (PTR) record lookups
- Underlying Windows socket API behavior
When a host is offline, Windows may fail these secondary checks even when initial forward resolution succeeds. This explains why simple ping
works differently - it doesn't perform these additional verifications.