Test-Connection

This topic contains 18 replies, has 5 voices, and was last updated by Profile photo of Graham Beer Graham Beer 3 months ago.

Viewing 15 posts - 1 through 15 (of 19 total)
  • Author
    Posts
  • #44011
    Profile photo of Brad Lankford
    Brad Lankford
    Participant

    I run this script and I want it to tell me the name of the failed ping. Where "$computer is offline" is, I want it to show "pc2 is offline" or whatever pc fails the ping basically. Any ideas?

    $csv = Get-Content pclist.csv

    foreach ($computer in $csv)
    {
    try
    {
    Test-Connection $computer -Count 1 -ErrorAction Stop | Select-Object Address, IPV4Address
    }
    catch [System.Net.NetworkInformation.PingException]
    {
    '$computer is offline.'
    }
    }
    pause

    I'll end with a report that looks like this

    Address IPV4Address
    ——- ———–
    pc0 192.168.1.1
    pc1 192.168.1.2
    $computer is offline.

    #44015
    Profile photo of Dan Potter
    Dan Potter
    Participant

    'comp1','comp2' | % {[pscustomobject]@{online = [bool](test-connection $_ -Count 1 -quiet);computername = $_}}

    #44033
    Profile photo of Brad Lankford
    Brad Lankford
    Participant

    I apologize, I am very new to powershell, is this line replacing something or where specifically am i placing it?

    #44038
    Profile photo of Dan Potter
    Dan Potter
    Participant

    Pinging computers is every beginners favorite thing to do:-) I suggest browsing the the technet script repository here.

    https://gallery.technet.microsoft.com/scriptcenter

    #44042
    Profile photo of Brad Lankford
    Brad Lankford
    Participant

    Thanks! I will look into it. Appreciate the responses. I will see what I can do.

    #44049
    Profile photo of Dan Potter
    Dan Potter
    Participant
    
    foreach($computer in $computers){
    
    $test = test-connection $computer -Count 1  -ea 'silentlycontinue'
    
    [pscustomobject]@{
    
    status = if($test){'online'}else{'offline'}
    ipv4 = $test.ipv4address.ipaddresstostring
    name = $computer
    
    }
    
    }
    
    
    #44053
    Profile photo of Stefan Werner
    Stefan Werner
    Participant
    '$computer is offline.'

    This would not work if you want variable expansion. You would need to use double quotes for that.

    Here's another example of a one liner...

    Get-Content pclist.csv | Foreach-Object { If (Test-Connection -ComputerName $_ -Quiet -Count 1) { Write-Host "$_ is online" -ForegroundColor Green } Else { Write-Host "$_ failed ping" -ForegroundColor Red" } }
    #44057
    Profile photo of Dan Potter
    Dan Potter
    Participant

    You just killed four puppies:-)

    #44063
    Profile photo of Stefan Werner
    Stefan Werner
    Participant

    Why would you have me do such a thing? lol

    #44065
    Profile photo of Brad Lankford
    Brad Lankford
    Participant

    Ahhh, Thanks! That was my issue. lol

    #44107
    Profile photo of Graham Beer
    Graham Beer
    Participant

    To make life easier with Dan's excellent piece of code you can wrap a small function around it:

    function test-ping {
        
        $args | % {[pscustomobject]@{online = [bool](test-connection $_ -Count 1 -quiet);computername = $_}}
    }
    

    Then you can just type

    Test-ping Device1 Device2
    
    #44114
    Profile photo of Nigel Tatschner
    Nigel Tatschner
    Participant

    "Test-Connection -ComputerName SERVER -Quite" returns a System.Boolean Type,
    basically a "True" or "False"

    You could use it like;

    $Import = Get-Content -Path C:\ServerList.txt
    
    foreach ($a in $Import) {
        if (Test-Connection -ComputerName $a -Count 2 -Quite) { 
                Write-Host "Ping Succeeded" 
                } Else {
                  Write-Host "Ping Failed"
                }
    }
    
    #44140
    Profile photo of Graham Beer
    Graham Beer
    Participant

    Yes Nigel you are quite right, no requirement for the [bool] type.

    So as a short function :

    function test-ping { $args | % {[pscustomobject]@{online = test-connection $_ -Count 1 -quiet;computername = $_}} }
    
    #run
    Test-ping Device1 Device2
    
    #44187
    Profile photo of Brad Lankford
    Brad Lankford
    Participant

    I really like the output and speed of Grahams edit of Nigel's script! I wish it could show ipv4 and servicetag info as well. How would I pipe these into the script? Without the need for the txt/csv. Also I noticed it truncates the list of my ipv4 addresses with my original script which was annoying not to see the last octet of the contacted host. The last script listed seems to have a different list view output.

    $a = Get-Content "C:\Powershell\Computers.txt"

    foreach ($i in $a)
    {$i + "`n" + "=========================="; Get-WmiObject win32_SystemEnclosure -computername $i | select serialnumber}

    #44313
    Profile photo of Graham Beer
    Graham Beer
    Participant

    Hi Brad,

    This is a bit more code but should do what you require :

    function test-ping {
        param([Object[]]$Devices)        
        
                foreach ($Device in $Devices) {
    
                    if (test-connection $Device -Count 1 -quiet) {
        
                        [pscustomobject]@{
    
                        computername = $Device 
                        online       = "True"
                        IPaddress    = (test-connection $Device -Count 1).IPV4Address.IPAddressToString
                        serialnumber = (Get-WmiObject -ComputerName $device win32_SystemEnclosure).serialnumber
                        }
    
                    } else {
                        
                        [pscustomobject]@{
    
                        computername = $Device 
                        online       = "false"
                        IPaddress    = " `t- "
                        serialnumber = " `t-"
                        }
                    }
            }
    }
    
    

    let me know.

Viewing 15 posts - 1 through 15 (of 19 total)

You must be logged in to reply to this topic.