Description
The goal for this script is to schedule a task for a lot of Virtual Machine. You have to list all of your virtual machines that you want to affect by this schedule in a csv file. The name of the task is define in the script as well as the description. The CSV file must be have a column’s name “Name”. The task created is configure for Once time, it’s not do for periodic time. You can’t configure it for periodic time with this script but you can modify it on line “VMware.Vim.OnceTaskScheduler”.
Please look at VMware SDK to perform this task.
Example :
Name VM Name 1 VM Name 2 ...
Requirements
Some options are mandatory to be able to schedule any task.
The first option is “Task”. You can use three parameters for this option (RebootGuest, ShutdownGuest or PowerOnVM).
The second is “ScheduleTask”. You have to format this option like this “mm/dd/yy HH:mm”.
The third is “EmailNotification”. This one is not mandatory but you can add it if you would like to receive an email when the task is done.
Example
PS > ScheduleTask -PathFile C:\PathToMyCSVFile.csv -ScheduleTask "01/25/20 20:31" -Task ShutdownGuest -EmailNotification xxx@xxx.com
Script
function ScheduleTask { [CmdletBinding()] Param( [Parameter(Mandatory=$True,Position=0)] [string]$PathFile, [Parameter(Mandatory=$True,Position=1)] [ValidateSet("RebootGuest", "ShutdownGuest", "PowerOnVM")] [string]$Task, [Parameter(Mandatory=$False,Position=2)] [string]$EmailNotification, [Parameter(Mandatory=$True,Position=3)] [string]$ScheduleTask )#PARAM $Time = Get-Date $ScheduleTask $ImportCSV = Import-Csv -Path $PathFile if ($ImportCSV.Name -eq $null) { Write-Host "This file doesn't contain any columns Name" -ForegroundColor red Break } else { foreach ($VM in $ImportCSV.Name) { $vm = Get-VM -Name $VM $si = get-view ServiceInstance $scheduledTaskManager = Get-View $si.Content.ScheduledTaskManager if ($Task -eq "RebootGuest") { $spec = New-Object VMware.Vim.ScheduledTaskSpec $spec.Name = "RebootGuest VM $VM" -join ' ' $spec.Description = 'RebootGuest VM' $spec.Enabled = $true $spec.Notification = $EmailNotification $spec.Scheduler = New-Object VMware.Vim.OnceTaskScheduler $spec.Scheduler.runat = $Time $spec.Action = New-Object VMware.Vim.MethodAction $spec.Action.Name = "RebootGuest" $scheduledTaskManager.CreateObjectScheduledTask($vm.ExtensionData.MoRef, $spec) } elseif ($Task -eq "ShutdownGuest") { $spec = New-Object VMware.Vim.ScheduledTaskSpec $spec.Name = "Shutdown VM $VM" -join ' ' $spec.Description = 'Shutdown VM' $spec.Enabled = $true $spec.Notification = $EmailNotification $spec.Scheduler = New-Object VMware.Vim.OnceTaskScheduler $spec.Scheduler.runat = $Time $spec.Action = New-Object VMware.Vim.MethodAction $spec.Action.Name = "ShutdownGuest" $scheduledTaskManager.CreateObjectScheduledTask($vm.ExtensionData.MoRef, $spec) } elseif ($Task -eq "PowerOnVM") { $spec = New-Object VMware.Vim.ScheduledTaskSpec $spec.Name = "Power on VM $VM" $spec.Description = "Power on VM" $spec.Enabled = $true $spec.Notification = $EmailNotification $spec.Scheduler = New-Object VMware.Vim.OnceTaskScheduler $spec.Scheduler.runat = $Time $spec.Action = New-Object VMware.Vim.MethodAction $spec.Action.Name = "PowerOnVM_Task" $scheduledTaskManager.CreateScheduledTask($vm.ExtensionData.MoRef, $spec) } } } }
For more informations, please send me an email from Contact tab.
Have Fun !!!