Search code examples
powershellpowershell-cmdlet

test-connection that supports wildcard? workaround?


trying to see if anyone has a known workaround for using the test-connection cmdlet in powershell to ping wildcard entries in DNS.

I'm trying to clean out our DNS db and exported a list from our BIND server and am in the process of just pinging through the 600+ machines to see if anything responds. I made my own simple script but have also found one that works slightly better on this forum. The script works but the cmdlet help files state that the -computername parameter does not support wildcards and sure enough, when i run the script all CNAME records are reporting down/false when they actually should be responding. The code I'm using is below and is kind of messy but I just needed something quick and it works, but I've included it below for reference:

Get-Content -path C:\Work\testy.txt  | ForEach-Object { Test-Connection -ComputerName $_ -Count 1 -AsJob } | Get-Job | Receive-Job -Wait | Select-Object @{Name='ComputerName';Expression={$_.Address}},@{Name='Reachable';Expression={if ($_.StatusCode -eq 0) { $true } else { $false }}} |out-file -FilePath c:\work\TEST.txt

Solution

  • As pointed out by briantist, any non-existing record name will do. You could generate a GUID to substitute the * in your record name:

    "subdomain.domain.tld","*.domain.tld" |ForEach-Object {
        Test-Connection -ComputerName $($_ -replace '\*',"$([guid]::NewGuid())")
    } 
    

    Your expression for whether it's "Reachable" or not can be simplified as well:

    @{Name='Reachable'; Expression={[bool]($_.StatusCode -eq 0)}}