Test if server online

Welcome Forums General PowerShell Q&A Test if server online

This topic contains 8 replies, has 5 voices, and was last updated by

 
Participant
3 weeks, 2 days ago.

  • Author
    Posts
  • #157992

    Participant
    Topics: 6
    Replies: 7
    Points: 35
    Rank: Member

    I've trying create a script that I can run every 15 minutes to verify that a server is online. The script below returns nothing.

    test-netconnection -ComputerName test12 -WarningAction SilentlyContinue | Where {$_.PingSucceeded -eq "false"} | Select ComputerName

    This doesn't return anything even though "test12" doesn't exist.

     

     

  • #157995

    Moderator
    Topics: 8
    Replies: 871
    Points: 2,660
    Helping Hand
    Rank: Community Hero

    Did you check Test-Connection cmdlet ?

    Get-Help Test-Connection -Onine

  • #158006

    Participant
    Topics: 6
    Replies: 653
    Points: 20
    Rank: Member

    Your where-object is not valid for what you are testing.  Use Get-Member to look at your returned object.

    PS C:\> Test-NetConnection -computername 8.8.8.8 | Get-Member
    
    TypeName: TestNetConnectionResult
    
    Name MemberType Definition
    ---- ---------- ----------
    Equals Method bool Equals(System.Object obj)
    GetHashCode Method int GetHashCode()
    GetType Method type GetType()
    ToString Method string ToString()
    AllNameResolutionResults Property System.Object AllNameResolutionResults {get;set;}
    BasicNameResolution Property System.Object BasicNameResolution {get;set;}
    ComputerName Property string ComputerName {get;set;}
    Detailed Property bool Detailed {get;set;}
    DNSOnlyRecords Property System.Object DNSOnlyRecords {get;set;}
    InterfaceAlias Property string InterfaceAlias {get;set;}
    InterfaceDescription Property string InterfaceDescription {get;set;}
    InterfaceIndex Property uint32 InterfaceIndex {get;set;}
    IsAdmin Property bool IsAdmin {get;set;}
    LLMNRNetbiosRecords Property System.Object LLMNRNetbiosRecords {get;set;}
    MatchingIPsecRules Property ciminstance[] MatchingIPsecRules {get;set;}
    NameResolutionSucceeded Property bool NameResolutionSucceeded {get;set;}
    NetAdapter Property ciminstance NetAdapter {get;set;}
    NetRoute Property ciminstance NetRoute {get;set;}
    NetworkIsolationContext Property string NetworkIsolationContext {get;set;}
    PingReplyDetails Property System.Net.NetworkInformation.PingReply PingReplyDetails {get;set;}
    PingSucceeded Property bool PingSucceeded {get;set;}
    RemoteAddress Property ipaddress RemoteAddress {get;set;}
    RemotePort Property uint32 RemotePort {get;set;}
    ResolvedAddresses Property ipaddress[] ResolvedAddresses {get;set;}
    SourceAddress Property ciminstance SourceAddress {get;set;}
    TcpClientSocket Property System.Net.Sockets.Socket TcpClientSocket {get;set;}
    TcpTestSucceeded Property bool TcpTestSucceeded {get;set;}
    TraceRoute Property string[] TraceRoute {get;set;}

    Notice that "PingSucceeded" is a boolean, not a string.  You are doing a string comparison with 'Where {$_.PingSucceeded -eq "false"}'.  You need to compare with an actual false, not string of characters that spell false.

    Where {$_.PingSucceeded -eq $false}

  • #158130

    Participant
    Topics: 6
    Replies: 7
    Points: 35
    Rank: Member

    Thanks Curtis and Kvprasoon,

    I think Test-Connection might work better for me since I need to test more than one server. Command below works by passing "true" or "false", but I can't get it to pass the computername thru the pipeline when adding "| select *"

    Test-Connection -ComputerName $servers -count 1 -quiet

  • #158135

    Moderator
    Topics: 8
    Replies: 871
    Points: 2,660
    Helping Hand
    Rank: Community Hero

    With -Quiet, it returns boolean. Remove it and use Select-Object Address to get the target computername

    Something like below.

    if($Ping = Test-Connection -ComputerName $servers -count 1 -ErrorAction SilentlyContinue){
        $Pingable = $Ping | Foreach-Object -Process {$_.Address}
       "Pinged $($Pingable -join ',')"
    }
    
    • #158144

      Participant
      Topics: 6
      Replies: 7
      Points: 35
      Rank: Member

      All examples work well. Thank you all.

  • #158138
    js

    Participant
    Topics: 22
    Replies: 613
    Points: 1,257
    Helping Hand
    Rank: Community Hero

    Ironically, any string is equal to $true:

    PS C:\users\js> $true -eq "hi"
    True
    

    I don't like either of those commands, since the timeout is so long. This function times out in 100 milliseconds for down computers.

    Function Get-Ping  {
    
      Param (
        [parameter(ValueFromPipeline)]
        [string[]]$Hostname='yahoo.com'
      )
    
      Begin {
        $Ping = New-Object System.Net.Networkinformation.ping
        $Timeout = 100 # ms
      }
    
      Process {
          $Ping.Send($hostname, $timeout) | Add-Member -passthru hostname $hostname[0] | 
          select hostname,address,status,roundtriptime
      }
    
    }
    
    
    PS C:\Users\js> echo a010 a011 | get-ping
    
    hostname Address        Status RoundtripTime
    -------- -------        ------ -------------
    a010     172.1.1.1     Success             1
    a011                  TimedOut             0
    
  • #158150

    Participant
    Topics: 0
    Replies: 7
    Points: 57
    Rank: Member

    I have written something that I use to monitor ConfigMGR servers in multiple countries due to network teams being nice by blocking access to servers or local staff unplugging servers to save power.

     

    [int]$Width = 44
    
    [int]$Height = 38
    
    $WindowSize = $Host.UI.RawUI.WindowSize
    
    $WindowSize.Width = [Math]::Min($Width, $Host.UI.RawUI.BufferSize.Width)
    
    $WindowSize.Height = $Height
    
    $Host.UI.RawUI.WindowSize = $WindowSize
    
    $host.ui.RawUI.WindowTitle = "WINDOW TITLE"
    
    [int]$keyEscape = 27
    
    $key = $null
    
    $delayMin = 5
    
    while ($key -eq $null -and $key -ne $keyEscape) { 
    
        Clear-Host
    
        if (Test-Connection "server 1" -Quiet) {write-host "server friendly name" -ForegroundColor Green} else {write-host "server friendly name" -ForegroundColor Red}
        if (Test-Connection "server 2" -Quiet) {write-host "server friendly name" -ForegroundColor Green} else {write-host "server friendly name" -ForegroundColor Red}
        if (Test-Connection "server 3" -Quiet) {write-host "server friendly name" -ForegroundColor Green} else {write-host "server friendly name" -ForegroundColor Red}
        if (Test-Connection "server 4" -Quiet) {write-host "server friendly name" -ForegroundColor Green} else {write-host "server friendly name" -ForegroundColor Red} 
        if (Test-Connection "server 5" -Quiet) {write-host "server friendly name" -ForegroundColor Green} else {write-host "server friendly name" -ForegroundColor Red} 
      
    
      Write-host
    
        Write-host
    
        Write-Host "ESC-Exit  Enter-Refresh" (" " * 31) -ForegroundColor White -BackgroundColor DarkCyan
    
    
    
    
        $delaySec = $delayMin * 60
    
        Write-Host
    
    
    
    
        $refresh = $false
    
    
    
    
        while ($delaySec -gt 0 -and $refresh -eq $false) {
    
            if (($delaySec % 60) -eq 0) {
    
                Write-Host ($delaySec / 60) -NoNewline -ForegroundColor gray
    
            } 
    
            else { 
    
                if ( ($delaySec % 10) -eq 0 ) {
    
                    Write-Host "." -NoNewline -ForegroundColor gray
    
                }
    
            }
    
    
    
    
            Start-Sleep -Seconds 1
    
            $delaySec--
    
    
    
    
            if ($host.UI.RawUI.KeyAvailable) {
    
                $key = $Host.UI.RawUI.ReadKey("NoEcho, IncludeKeyDown")
    
    
    
    
                #Write-Host "KEY->" $key.VirtualKeyCode
    
                #pause
    
    
    
    
                switch ($key.VirtualKeyCode) {
    
                    $keyEscape { $refresh = $true }
    
                    default {
    
                        Start-Sleep -Milliseconds 500
    
                        $host.UI.RawUI.FlushInputBuffer()
    
                        $key = $null
    
                        $refresh = $true
    
                    }
    
                }
    
            }
    
        }
    
    }

     

     

    Just change the Servers, window title, windows size (or remove that part) and milliseconds before refresh. If you don't have a static list you can use a AD query to get servers and do a foreach that will do same result (not recommending for anything over 100 servers)

    I use the basics of this script as a template for all monitoring so there is irrelevant sections.

  • #158151

    Participant
    Topics: 6
    Replies: 7
    Points: 35
    Rank: Member

    Well done Rob. I'll test it out.

    thanks

You must be logged in to reply to this topic.