Showing posts with label scheduled tasks. Show all posts
Showing posts with label scheduled tasks. Show all posts

Tuesday, 20 September 2016

IP Address/Ping Monitoring using Powershell

Here is a script I developed to monitor/check the online availability of an IP address using powershell. I've set this script to run every 10 minutes to check that a device on my network (WDTV Live) is online as it had a tendency to go offline occasionally.

$ip = the IP address you wish to monitor
$ping = utilises the test-connection cmdlet to attempt to contact the IP address (specified in $ip) using ICMP (ping) packets

If the ping test does not return a result (ie. the IP address does not respond), a "flag" is placed in the form of a file called "WDTV.Offline". This flag file is used/checked by the script to prevent repeat notifications being sent after the first notification that the device is offline (or is back online after previously being offline)

If the ping test does return a result (ie. the IP address responded to the ping request), the script checks to see if it was previously offline (by looking for the flag file mentioned previously). If the flag file is found, another notification is sent advising the device is now back online, and the flag file is deleted. This means the next time the script runs, if the device is still online and no flag file is present, no further notifications are sent


$ip = "192.168.0.150"
$ping = test-connection $ip -count 2

if (!$ping)
    {
    $flagtest = test-path "D:\Powershell\WDTV.Offline"
    if ($flagtest -eq $false)
        {
        new-item "D:\Powershell\WDTV.Offline" -type file
        $from = "user@gmail.com"
        $to = "user@gmail.com"
        $subject = "$ip Offline"
        $body = "$ip not responding to ping"
        $smtp = New-Object System.Net.Mail.SmtpClient("smtp.gmail.com", "587");
        # Uncomment the below row if your ISP´s outgoing mail server requires authentication.
        $smtp.EnableSSL = $true
        $smtp.Credentials = New-Object System.Net.NetworkCredential("username", "password");
        $smtp.Send($From, $To, $subject, $body);
        #write-host "Mail Sent"
        }
    }

elseif ($ping)
    {
    $flagtest = test-path "D:\Powershell\WDTV.Offline"
    if ($flagtest -eq $true)
        {
        remove-item "D:\Powershell\WDTV.Offline"
        $from = "user@gmail.com"
        $to = "user@gmail.com"
        $subject = "$ip Online"
        $body = "$ip now responding to ping"
        $smtp = New-Object System.Net.Mail.SmtpClient("smtp.gmail.com", "587");
        # Uncomment the below row if your ISP´s outgoing mail server requires authentication.
        $smtp.EnableSSL = $true
        $smtp.Credentials = New-Object System.Net.NetworkCredential("username", "password");
        $smtp.Send($From, $To, $subject, $body);
        #write-host "Mail Sent"
        }
    }

You will need to update the $from, $to and username/password fields within the $smtp.credentials variables in order for the email notifications to work, as well as the values for outgoing mail server and port within the System.Net.Mail.SmtpClient

You can refer to my previous blog post Here which contains some more information about sending SMTP emails using authentication with powershell

Monday, 19 September 2016

Run/Trigger Powershell Script from Windows Task Scheduler

Windows built in Task Scheduler application can be used to trigger/run powershell scripts on custom schedules as required. Here's how to set it up.


  1. Open Task Scheduler (Control Panel > Administrative Tools > Task Scheduler)
  2. Right click on Task Scheduler Library and select Create Task

  3. Enter a name for the scheduled task. Select the option to Run whether user is logged on or not to ensure the task will still run even if you are not currently logged into the computer

  4. Go to the Triggers tab and click the New button. Set the task to run on the schedule as required (eg. daily, weekly, hourly, etc). I recommend setting the option to Stop the task if it runs longer than and set to 30 mins. This means if there is a bug/problem with the script it will terminate after this time and won't continue running in the background
  5. Select the Actions tab and click the New button. Configure as per below:

    Action: Start a program
    Program/script: C:\Windows\System32\WindowsPowershell\v1.0\powershell.exe
    Add Arguments: (Full path to your powershell script .ps1 file)

    Example path may be D:\Powershell\TestScript.ps1

  6. Other settings and conditions can be configured in their respective tabs as required, but the default settings within here will work

    Note: if you find that your scheduled task won't run, check out My Post on Powershell Execution Policies which may be preventing the scheduled task from running your powershell script