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.
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)
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.
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?
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)
This is wonderful, thanks!