The below script to check the list of website connectivity is not working when one of the domain controllers is not contactable, and when the destination IP address is not responding?
How can this script be adjusted to always move on or On Error Resume Next, and then put the column "Exception Error".
Try
{
$Array = @()
$Servers = Get-ADDomainController -filter * | Select-Object -ExpandProperty Name
$PortNumber = '443'
$Array = Invoke-Command -cn $Servers {
param ($PortNumber)
$Destinations = "168.62.20.37", "www.pingdom.com","168.168.21.21","cloudmasters.com"
$Object = New-Object PSCustomObject
$Object | Add-Member -MemberType NoteProperty -Name "Servername" -Value "$env:computername - $((Resolve-DnsName -Name $env:computername -Type A).IPAddress)"
$Object | Add-Member -MemberType NoteProperty -Name "Port" -Value $PortNumber
Foreach ($Item in $Destinations)
{
$Result = Test-NetConnection -Port $PortNumber -cn $Item
$Delay = $Result.'PingReplyDetails'.Roundtriptime | % { ("$_" + " ms") }
$Object | Add-Member -Type NoteProperty -Name "Destination" -Value $Item -Force
$Object | Add-Member -Type NoteProperty -Name "Ping Time" -Value $Delay -Force
$Object | Add-Member -Type NoteProperty -Name "Ping" -Value $Result.PingSucceeded -Force
$Object | Add-Member -Type NoteProperty -Name "TCP" -Value $Result.TcpTestSucceeded -Force
$Object
}
} -ArgumentList $PortNumber -ErrorAction stop | Select-Object * -ExcludeProperty runspaceid, pscomputername, PSShowComputerName
}
Catch [System.Exception]{
Write-host "Error" -backgroundcolor red -foregroundcolor yellow
$_.Exception.Message
}
$Array | Out-GridView -Title "Results"
The script is a bit old since there is a lot of Add-Member Object in use. It is working, but not sure what's the suggested best practice to optimize for speed.