Use Powershell to manipulate scheduled tasks for server RAID status

Asset 13

Receive an email alert when a RAID array disk is predicted to fail

Posted on 17 April 2019 by Beaming Support

Powershell can be used to create, delete or change scheduled tasks on a Windows PC or server.

The example below will create a task that uses the Dell Openmanage software to check the status of a RAID array, then send an email alert if the array has a disk that is predicted to fail.

The section below will first check if a task exists or not, then if it doesn’t exist, it will create it. The next section should be run as one script on a physical server.

# Checks if Daily task already exists and adds it if not

if (!(schtasks /query /TN Raidcheck)){

                schtasks /Create /SC DAILY /TR “C:\RaidCheck\Raid.bat” /TN RaidCheck /ST “08:00:00” /NP /RU “NT authority\local service” /RL HIGHEST

}

# Checks if Daily task already exists and adds it if not

if (!(schtasks /query /TN Raidcheck2)){

                schtasks /Create /SC DAILY /TR “C:\RaidCheck\Raid.bat” /TN RaidCheck2 /ST “13:00:00” /NP /RU “NT authority\local service” /RL HIGHEST

}

# Sets current RAID, if file doesn’t already exist. This is for comparison if the RAID fails

if (!(test-path -path “C:\raidcheck\raid.txt”))

{

omreport storage pdisk controller=0 | Select-String “Failure Predicted”, “Name” | Out-String | out-file C:\raidcheck\raid.txt

}

This will create two tasks that will run daily at 8am and 1pm, calling the script Raid.bat.

The RAID script is below and will use the Openmanage program to check the RAID array. You should copy the following sections into raid.bat and raid.ps1 respectively and save them into C:\RaidCheck on the server.

Raid.bat

@echo off

Powershell.exe -executionpolicy remotesigned -file “C:\raidcheck\RAID.ps1”

RAID.ps1

# Sets hostname for later use

$hostn = hostname

# Correct RAID status

$oldraid  = get-content “C:\raidcheck\raid.txt”

# Sets $i as equal to the amount of hard drives present

$ii = ($oldraid).count

# Sets up the formatted lines

for ($i=1; $i -lt $ii; $i++){

                $or += $oldraid[$i]

                $or += ”  “

                if ($i -eq 1 -or $i -eq 3 -or $i -eq 5 -or $i -eq 7){

                $or += ” “

                }

                if ($i -eq 2 -or $i -eq 4 -or $i -eq 6 -or $i -eq 8){

                $or += “<br />”

                }

}

# Sets state for whether Virtual disk is degraded or not

$state = omreport storage vdisk controller=0 | Select-String “State” | Out-String

# If RAID is degraded, send warning email

if ($state -like ‘*degraded*’){

                $body = “<HTML><HEAD><META http-equiv=””Content-Type”” content=””text/html; charset=iso-8859-1″” /><TITLE></TITLE></HEAD>”

                $body += “<body><p style=’color:red;font-size:20;font-weight:bold;font-family:arial’>$hostn RAID has failed</p>”

                $body += “<p style=’color:red;font-size:18;font-weight:bold;font-family:arial’>$state</p>”

                $body += “<br />”

                $body += “<p style=’color:blue;font-size:16;font-weight:bold;font-family:arial’>Currently Connected Drives :</p>”

                $body += “<p style=’color:black;font-size:14;font-family:arial’>”

                $body += omreport storage pdisk controller=0 | Select-String “Failure Predicted”, “Name” | Out-String

                $body += “</p>”

                $body += “<p style=’color:blue;font-size:16;font-weight:bold;font-family:arial’>Correctly Connected Drives:</p>”

                $body += “<p style=’color:black;font-size:14;font-family:arial’>”

                $body += $or

                $body += “</p><br /><p style=’color:red;font-size:18;font-family:arial’>This alert will now come through daily at 8am and 1pm, until the drive is replaced!”

                $body += “</p></body>”

                Send-MailMessage -to “email@address.com” -from “$hostn-RAID@domain.com” -Subject “$hostn RAID” -body $body -SmtpServer SMTP -bodyashtml

                }

Using this script, you will receive an email if the RAID becomes degraded due to a predicted failure or a complete drive failure. The email will show you the original RAID drive layout as well as the current RAID layout. This will help to determine which drive has failed.

You will need to edit the email to address, from domain and add your own SMTP details based on your mail server.

Found this useful?

  • Sign up and we’ll pop up in your inbox once a month with guides from our support team that boost efficiency and troubleshoot common IT & connectivity problems, as well as news on cyber security threats

Related

Get the best of Beaming

Sign up to our monthly email updates on:

  • Easy cyber security advice anyone can follow
  • Research into what businesses are doing with their tech
  • How-to guides to boost productivity and efficiency

  • This field is for validation purposes and should be left unchanged.