Use PowerShell scheduled tasks to create daily mail reports with the scripts you have written
Today I’ll show you a really cool thing in PowerShell V3, and its the simplicity of creating a scheduled task
If you’re enthusiastic and excited about automating your daily admin tasks with PowerShell as much as I am, you have probably written a decent amount of scripts for you and your team.
Here is an example of a script I wrote back in V2 that draw’s data from the File Server’s D: Partition, which hold the user’s home folders and departmental UNC’s:
Import-Module ActiveDirectory
function getthedata {
$badcomp = @()
$CompList = Get-ADComputer -Filter 'name -like "fs*"' | select -ExpandProperty Name
foreach ($c in $CompList) {
Try {
Get-WmiObject win32_logicaldisk -ComputerName $c -Filter 'DeviceID = "D:"' -ea stop |
Select-Object @{l='ServerName';e= {$_.__SERVER} },
@{l='TotalSize(GB)';e = {$_.Size / 1GB -as [int]} },
@{l='FreeSize(GB)';e = {$_.freespace / 1GB -as [int]} }
}
Catch {
$badcomp += $c
}
}
"`nthe following servers could not be reached:"
$badcomp
}
getthedata
A quick review of the script:
- get the computer names from AD that starts with FS. in my case its only file servers, but you can also create a .txt file with computer names and use Get-Content C:\CompNames.txt, for example.
- enumerates the computer names (which are actually string objects) and query the Win32_LogicalDisk WMI library for each of them, and then calculates the properties that I chose using Select-Object.
- the Try and Catch block are used for error-handling, and instead of having ugly error’s in my output that infroms me about servers I don’t have access to (that is the case with my errors) I just add the computer names that I cannot access to a collection, and then im outputting that collection in the end of script
As a sys admin, you dont always have the time, or you just simply forget, to do these kind of tasks.
This is where PowerShell scheduled tasks and the Send-MailMessage cmdlet comes in.
At the end of the script, instead of calling the function, you can call it in the Send-MailMessage -Body parameter:
function getthedata {
. . . . .
}
Send-MailMessage -From MyEmail@Domain.com -To MyEmail@Domain.com -Subject “File Server Disk Report” -Body (getthedata | Out-String) -SmtpServer ExchangeServer
Notice that the function is called and then piped to Out-String inside brackets in the -Body parameter. In the Background, the function is called and its data is outputted to the type of System.String, which is the only type of objects the -Body parameter accepts. Straight-out calling the function will just send you an email with the function’s name inside the email’s content.
Once the script runs, it will send an email with the script’s (or function’s) output.
That email can also be sent to my team, all I have to do is just add their email addresses (separated by a comma) in the -To parameter.
The last step is to register a scheduled job. I can do that easily with PowerShell’s PSScheduledJob Module – a new module for PowerShell V3.
First I need to create a Trigger Object, which is a set of criteria and conditions that trigger the job to run. quite simply:
PS D:\> $trigger = New-JobTrigger -Daily -At 2:00
PS D:\> $triggerId Frequency Time DaysOfWeek Enabled
– ——— —- ———- ——-
0 Daily 12/3/2012 2:00:00 AM True
Next, I create a new scheduled job using Register-ScheduledJob, and populate its -Trigger parameter with the trigger I just created, and assign the script’s path to the -FilePath parameter:
PS D:\> Register-ScheduledJob -Name FileServ_Report -FilePath D:\ps\FileServ_MailReport.ps1 -Trigger $Trigger -Credential YOURDOMAIN\UserName
Very important notice – Make sure you type the -Credetial parameter! otherwise, the scheduled task will only have privileges to run on your local computer, which means it will not contact the servers mentioned in the script, and you will get an email with blank data!
If it asks you for a password, you will know you did it right.
You can confirm the task is Scheduled by using Get-ScheduledJob:
PS D:\> Get-ScheduledJob -Name FileServ_Report
Id Name JobTriggers Command Enabled
— —- ———– ——- ——-
17 FileServ_Report 1 D:\ps\FileServ_MailReport.ps1 True
And run it to see how it looks like:
PS D:\> (Get-ScheduledJob -Name FileServ_Report).Run()