none
[Newbie] Simple network monitoring script RRS feed

  • Question

  • Hello. I am currently stuck trying to get this thing to work as intended:

    $serverlist = Get-Content path\PCs.tst $OldStatus = $True while($true) { ForEach ($computer in $serverlist) { $NewStatus = Test-Connection -ComputerName $computer -Dealy 2 -Count 2 -quiet If ($OldStatus -ne $NewStatus) { Write-Host "$computer is $NewStatus" $OldStatus = $NewStatus } Else { Write-Host "OK" } }
    Start-Sleep 5
    }

    The expected behavior is: Script grabs the list of computers from text file, pings them, (at this step) compares their $NewStatus against default $OldStatus and based on that either: send a message with new status (will be replaced by Send-MailMessage once it starts working) and updates $OldStatus with $NewStatus or just confirms that nothing has changed since the last loop.

    Basically, it should notify me when hosts go up and down, without spamming me with "Device is offline" and "Device is online" messages every 5 seconds (due to Start-Sleep 5)

    However, I have no idea how to make it compare value of Old and New status against the same $computer, because right now, due to ForEach, it replaces universal $OldStatus with the latest one and outputs stuff like this:

    OK (initial comparison against predefined $status, that's how all debug lines should look like when computer state is unchanged)
    OnlineComputer is True
    FakeComputer is False
    OnlineComputer is True

    and so on ad eternum.


    Tried looking for similar code online, but no one seems to enjoy magic of Compare-Objects combined with Test-Connection
    I had successful experience running this script while storing $OldStatus in path\$computer.txt file, but that is a waste of HDD time and space. Just plain stupid.
    Tried running it with jobs - kinda fine, but I reach millions of jobs done pretty fast (I did have Get-Job | Remove-Job in While, though). Pretty messy code in the end, and I try to keep it as short as possible.
    Will pipes save me from this purgatory?



    Thank you in advance for any advice or help!


    Friday, September 7, 2018 9:13 AM

Answers

  • Here's code after giving it some thought, just in case someone ever need free script to monitor their computers online status but does not want to run freeware (yeah, I'm that kind of person)

    $To = Get-Content -Path "C:\temp\To.txt"
    $Rly = "my.relay.com"
    $pcs = Get-Content -Path "C:\temp\Servers.txt"
    $Bdy = "as reported by ShortMonitor2.0"
    $OldS = $True
    $NewS = $False
    
    foreach ($_ in $pcs){
        $hash += @{
            $_ = @{
            Old = $OldS
            New = $NewS
            }
        }
    }
    
    While($True){
        ForEach ($key in $hash.Keys){
            $hash[$key].new = Test-Connection -ComputerName $key -Count 3 -Quiet
            IF($hash[$key].new -ne $hash[$key].old){
            $hash[$key].old = $hash[$key].new
            $message =  '{0} status is now {1}' -f $key, $hash[$key].new
            Send-MailMessage -To $To -Subject "$message" -Body $Bdy -SmtpServer $Rly -From "$key@garsar.ru"
            }
        }
    Start-Sleep -Seconds 5
    }

    Monday, September 17, 2018 10:53 AM

All replies

  • We do not use Compare-Object to compare Booleans.  Just compare diresctly.

    if($oldstatus -eq $newstatus){
         # same
    }else{
         # different
    }

    Consider taking time to actually learn PowerShell.  Learn PowerShell  


    \_(ツ)_/

    Friday, September 7, 2018 9:22 AM
  • That is a good way to make code cuter, will use it in the final version (for some reason often had "-eq is not a thing" in IF statements in first script revisions, had to rely to less orthodox methods)

    However, the issue with splitting variables per $computer still stands tall.

    And yeah, I'm trying to learn PS doing stuff, hence the [Newbie] in title.

    Friday, September 7, 2018 9:41 AM
  • Here's code after giving it some thought, just in case someone ever need free script to monitor their computers online status but does not want to run freeware (yeah, I'm that kind of person)

    $To = Get-Content -Path "C:\temp\To.txt"
    $Rly = "my.relay.com"
    $pcs = Get-Content -Path "C:\temp\Servers.txt"
    $Bdy = "as reported by ShortMonitor2.0"
    $OldS = $True
    $NewS = $False
    
    foreach ($_ in $pcs){
        $hash += @{
            $_ = @{
            Old = $OldS
            New = $NewS
            }
        }
    }
    
    While($True){
        ForEach ($key in $hash.Keys){
            $hash[$key].new = Test-Connection -ComputerName $key -Count 3 -Quiet
            IF($hash[$key].new -ne $hash[$key].old){
            $hash[$key].old = $hash[$key].new
            $message =  '{0} status is now {1}' -f $key, $hash[$key].new
            Send-MailMessage -To $To -Subject "$message" -Body $Bdy -SmtpServer $Rly -From "$key@garsar.ru"
            }
        }
    Start-Sleep -Seconds 5
    }

    Monday, September 17, 2018 10:53 AM