TaskAgent & Windows PowerShell - Part 2
This is the second part of a series of posts related to the TaskAgent and Windows PowerShell. You can find the first part here: TaskAgent & Windows PowerShell - Part 1
Check the Downloads section at the end of this post for a PDF version of the full post and related source files.
Scenario 2 – Check the available disk space and send an e-mail notification
This scenario attempts to automate a standard system administrative task by periodically checking the available disk space on multiple servers and sending e-mail notifications if the free disk space is below a certain limit. To accomplish this I will use again a Windows PowerShell script which will collect the necessary information using Windows Management Instrumentation (WMI) objects and send the notifications using .NET objects. Lets start:
1. We will start by creating the PowerShell script. For your convenience, you can download the source for this demo and use the file demo_checkdiskspace.ps1
I will briefly explain what each part of the script is doing:
In the first section we just declare the list of computer names which should be checked and an empty array variable to store the results. Remember to change the list of server names to something which applies to your network.
In the second section the script iterates through the list of server names and uses the WMI object Win32_LogicalDisk to collect the disk drives information (marked with red). Since the servers may have multiple drives, the script iterates through the disk drives information of each server and adds to the result information the server name, calculates the percent free disk space and uses the calculated percentage to place the server disk drive one of the following 3 groups: less than 20% - CRITICAL, less than 35% - WARNING and everything else – OK (marked with blue). You can change these percentages to different levels if necessary.
The third part of the script further filters the results collected so far and leaves only the ones with CRITICAL or WARNING status. You can omit this step if you would like to send in the e-mail a report which includes information for all disk drives, not only the ones which are running low in free disk space.
The last section of the script checks if there are any results and formats them in a nice report which is written to a text file (marked with red). The script then uses a .NET object to prepare an e-mail message and send the results in the body of the message (marked with blue). Remember to change the From and To e-mail addresses. If your SMTP server requires authentication, you can also specify the necessary username and password.
2. Create a TaskAgent job and add a time event for it. (see Scenario 1 for more details how to do this)
3. Add a task for the job which we created above. The task type should be “Run External Program”.
4. On the “Task Settings” – “General” tab, in the “Program or File to Run” field, enter the PowerShell executable “powershell.exe” and in the “Command-line Parameters” field, enter the full path to the PowerShell script file.
5. On the “Task Settings” - “Security” tab, you can provide the Windows credentials which the TaskAgent should use when executing the external program (in our case this is the powershell.exe). By default, the TaskAgent will use the same credentials as the ones which the TaskAgent service is using. Our PowerShell script is using WMI to interrogate multiple servers for their disk drives – this requires administrative permissions which the TaskAgent service account may not have. If this is the case, on the “Security” tab enter the credential for a Windows account which has the necessary permissions.
6. On the “Task Settings” – “Execution Result” tab, you can specify what the TaskAgent should do after the external program is successfully started. In this specific case, I have configured the task to wait for 20000 milliseconds (20 seconds) for powershell.exe to finish its execution. If the powershell.exe does not finish within the allocated period, the TaskAgent will terminate the external process. In addition, the TaskAgent can examine the exit code of the external process to determine whether the execution was successful or not. In this specific case, I have configured the task to accept only 0 as a successful exit code.
Save the job and test it. If everything is correct and some of the servers in your list are running low on free disk space, you should get a report containing information similar to this:
Not bad for a short script – by integrating the TaskAgent with PowerShell, WMI and .NET we can automate a multitude of administrative and business tasks.
Downloads
You can download a PDF version of the full post here: TaskAgent & Windows PowerShell
The related source files are available for download here: Sample PowerShell scripts and other source files from the demos