VMware Cloud Community
rbmadison
Contributor
Contributor

VC - Schedule Powershell script

It would be nice to add the ability to run a scheduled powershell script to the list of scheduled tasks you can set up in the VC.

0 Kudos
5 Replies
halr9000
Commander
Commander

Yup, I'm hoping that's something VMware will cover in the next revision of VC. In the meantime, check out the VI Plugins project (http://viplugins.com), there's some really cool stuff there. Also, I plan on attempting to get command-line scheduling via powershell to work so that you could do it that way. No ETA yet.

Hal Rottenberg

Co-Host, PowerScripting Podcast (http://powerscripting.net)

My signature used to be pretty, but then the forum software broked it. vExpert. Microsoft MVP (Windows PowerShell). Author, Podcaster, Speaker. I'm @halr9000
0 Kudos
pfuhli
Enthusiast
Enthusiast

At this time you can accomplish this e.g. by using the scheduled task function of the OS your VIToolkit is running on.

I have installed the VIToolkit on a virtual XP box and use the scheduled tasks to generate reports every night which will be mailed to our sysalert mailbox.

0 Kudos
aw443
Contributor
Contributor

That would be very useful. I'd like to run these get- reports before I come in every morning. Pfuhli, do you just save it as a ps1 file and set the scheduled task to run that file?

0 Kudos
pfuhli
Enthusiast
Enthusiast

Hi Alan,

I assigned this task to a Windows XP VM using the scheduled jobs.

This VM has installed the MS PowerShell and the PowerShell Toolkit from VMware.

This is the link in the scheduled jobs section:

C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe -PSConsoleFile "C:\Programme\VMware\Infrastructure\VIToolkitForWindows\vim.psc1" c:\scripts\dailyreport.ps1

where dailyreport1.ps1 is the script which generates the report mail. Unfortunately I didn't managed to get the output formatted pretty in the email body so I decided to attach it as a file to the mail. Maybe someone is able to improve the script in this way?!

See the script content below.

HTH

daniel

$SmtpClient = new-object system.net.mail.smtpClient

$SmtpServer = "fqdn.of.your.smtp.server"

$From = "sender@host.com"

$To = "recipient@host.com"

$Cc = "cc.recipient@host.com"

$Title = "[VIToolkit]:Daily Report for " + ::Now

$file = "C:\scripts\scriptlog\dailyreport.txt"

$Body = "See attached file for report info!"

$server = Get-VIServer -Server <serverIP> -User <username> -Password <passwd>

"VMs with CDROMs connected:" | Out-File -FilePath $file

"============================" | Out-File -FilePath $file -Append

get-vm | where { $_ | get-cddrive | where { $_.ConnectionState.Connected -eq "true" } } | select Name,powerState | Out-File -FilePath $file -Append

"`r`n`r`nVMs with existing snapshots:" | Out-File -FilePath $file -Append

"============================" | Out-File -FilePath $file -Append

Get-VM | Get-Snapshot | select { $_.vm.name },name,created | Out-File -FilePath $file -Append

"`r`n`r`nVMs with existing snapshots older than 3 months:" | Out-File -FilePath $file -Append

"==========================================================" | Out-File -FilePath $file -Append

Get-VM | Get-Snapshot | where {$_.created -lt ((get-Date).addMonths(-3))} | select { $_.vm.name },{ $_.vm.powerState },name,created | Out-File -FilePath $file -Append

$msg = new-object System.Net.Mail.MailMessage $From, $To, $Title, $Body

$msg.TO.add($Cc)

$attachment = new-object System.Net.Mail.Attachment $file

$msg.Attachments.Add($attachment)

$client = new-object System.Net.Mail.SmtpClient $SmtpServer

$client.Send($msg)

0 Kudos
aw443
Contributor
Contributor

This is wonderful, thanks!

0 Kudos