Friday 10 September 2010

Powershell snap-ins

I keep forgetting to load the relevant snap-ins when I write scripts for scheduled tasks, resulting in disappointment. I need to remember to 'add-pssnapin'...

List available snap-ins:
>get-pssnapin -registered


Load something useful:
>add-pssnapin Microsoft.Exchange.Management.PowerShell.Admin
-or-
>add-pssnapin Microsoft.Exchange.Management.Powershell.Support
-or-
>add-pssnapin VMware.VimAutomation.Core

Then (if you have PowerCLI installed) you can do useful things like weekly reports of systems running snapshotted:
+-----------------------------------------------------------------------------+
#BC/WTS/BBSRC - 27-08-2010 - report guests with snapshots
add-pssnapin VMware.VimAutomation.Core
#get variables together
$myVCServer="myvcsvr.example.com"$mySMTPServer = "smtp.example.com"
#munge a date-time and report filename together
$now=get-date -format "MMM-dd_hh-mm-ss"
$filename = "%temp%\snapshots_" + $now + ".csv"
#probably this is redundant
$file=$filename
#connect
connect-viserver $myVCServer
#get our snapshot list
$report=get-vm|get-snapshot|Select-Object VM,Created,SizeMB,Name
$report |export-csv $filename -NoTypeInformation
$msg = new-object Net.Mail.MailMessage
$att = new-object Net.Mail.Attachment($file)
$smtp = new-object Net.Mail.SmtpClient($mySMTPServer)
#mail the file out
$msg.From = " reports@example.com"
$msg.To.Add("reports@example.com")
$msg.To.Add("barry@example.com")
$msg.Subject = "ESX environment - Snapshot listing"
$msg.Body = "Snapshots associated with ESX guests, enumerated at $now.`n"
$msg.Body = $msg.Body + "This report runs weekly from $myVCServer.`n`n"
#$msg.Body = $msg.Body + (get-content $filename)
$msg.Attachments.Add($att)
$smtp.Send($msg)
$att.Dispose()
+------------------------------------------------+


Many, many opportunities for making this more glamorous, or in fact useful, but this is a useful recipe I think.

No comments:

Post a Comment