0

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.

2
  • Move your errorhandling into your loop. If you get an error in the loop it will go to the catch then continue to loop. Commented Aug 25, 2020 at 13:09
  • @DanielBjörk can you show me the example on the above script ? Commented Aug 25, 2020 at 13:34

3 Answers 3

1

The try..catch should be inside the loop. Also, you could create a PsCustomObject for each item in the loop and fill in the details in the try and catch

Something like:

$Servers    = Get-ADDomainController -Filter * | Select-Object -ExpandProperty Name
$PortNumber = 443

$Array = Invoke-Command -ComputerName $Servers -ScriptBlock {
    param ($PortNumber)

    $Servername   = "$env:COMPUTERNAME - $((Resolve-DnsName -Name $env:COMPUTERNAME -Type A).IPAddress)"
    $Destinations = "168.62.20.37", "www.pingdom.com","168.168.21.21","cloudmasters.com"

    foreach ($Item in $Destinations) {
        # prepare an object to output
        $Object = [PSCustomObject]@{
            'Servername'      = $Servername
            'Port'            = $PortNumber
            'Destination'     = $Item
            'Ping Time'       = $null
            'Ping'            = $false
            'TCP'             = $false
            'Exception Error' = $null
        } 
        try {
            # capture warnings in variable $warnings and supress output using '3> $null'
            $Result = Test-NetConnection -Port $PortNumber -ComputerName $Item -ErrorAction Stop -WarningVariable warnings 3> $null

            $Object.'Ping Time'       = $Result.'PingReplyDetails'.Roundtriptime
            $Object.'Ping'            = $Result.PingSucceeded
            $Object.'TCP'             = $Result.TcpTestSucceeded
            $Object.'Exception Error' = $warnings -join ', '
        }
        catch {
            $Object.'Exception Error' = $_.Exception.Message
        }
        # output the object
        $Object
    }
} -ArgumentList $PortNumber | Sort-Object Servername

$Array | Select-Object * -ExcludeProperty RunspaceId, PSComputerName, PSShowComputerName | Out-GridView -Title "Results"
Sign up to request clarification or add additional context in comments.

3 Comments

Many thanks for the update and suggestion @Theo, however, the result on the OGV only the last domain controller or one only not each and every domain controllers ?
Actually it is only showing the result of the existing domain controller where I run the script from. Not looping through all of the DCs on the $Servers variable.
@SeniorSystemsEngineer Ah sorry, I forgot to add the -ComputerName parameter. Also added a sort on ServerName and excluded unwanted properties
1

I think you want something like this:

$Object += New-Object PSObject -Property @{
    Destination = [String]$Item;
    PingTime = [Int]$Delay;
    Ping = [Bool]$Result.PingSucceeded;
    TCP = [Bool]$Result.TcpTestSucceeded;
}

Comments

1

Move your Try-Catch statement into the foreach loop:

    $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)
        {
            Try
            {
                $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
            }
            Catch [System.Exception]{
                Write-host "Error" -backgroundcolor red -foregroundcolor yellow
                $_.Exception.Message
            }
        }
       
       
    } -ArgumentList $PortNumber -ErrorAction stop | Select-Object * -ExcludeProperty runspaceid, pscomputername, PSShowComputerName




$Array | Out-GridView -Title "Results"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.