November 3, 2009

Sending Automated emails with powershell

James Brundage shows how in less than 25 lines of powershell code, you can create a scheduled task to email detailed reports of the installed programs on the machine. The main point here was to show that from his powerpack that he released a few weeks ago, it is simple to email reports from powershell. You can see the full article here. The code snippet that he posted is below:


New-Task |
Add-TaskAction -Hidden -Script {
$ErrorActionPreference = "Stop"
try {
$messageParameters = @{
Subject = "Installed Program report for $env:ComputerName.$env:USERDNSDOMAIN - $((Get-Date).ToShortDateString())"
Body = Get-WmiObject Win32_Product |
Select-Object Name, Version, Vendor |
Sort-Object Name |
ConvertTo-Html |
Out-String
From = "Me@MyCompany.com"
To = "Me@MyCompany.com"
SmtpServer = "SmtpHost"
}
Send-MailMessage @messageParameters -BodyAsHtml
} catch {
$_ |
Out-File $env:TEMP\ProblemsSendingHotfixReport.log.txt -Append -Width 1000
}
} |
Add-TaskTrigger -Daily -At "9:00 AM" |
Add-TaskTrigger -OnRegistration |
Register-ScheduledTask "DailyHotfixReport"

No comments:

Post a Comment