Hi All,
I do have a requirement to restart approx 400 VMs at every Monday 5:00 AM on weekly basis. I am trying to create the schedule task on vmware with the below script. Its creating the task but, one thing its not creating with date and time and second thing is that I need to create the task for 400 VMs so how can I define a csv file and what will the data format in that. please can some one help me on this.
$vmName = 'TESTVM01'
$si = Get-View ServiceInstance
$scheduledTaskManager = Get-View $si.Content.ScheduledTaskManager
$vm = Get-View -ViewType VirtualMachine -Filter @{"Name"=$vmName}
$spec = New-Object VMware.Vim.ScheduledTaskSpec
$spec.Name = "Restart $($vmName)"
$spec.Description = "Restart $($vmName)"
$spec.Enabled = $true
$spec.Scheduler = New-Object VMware.Vim.WeeklyTaskScheduler
$spec.Scheduler.Wednesday = $true
$spec.Scheduler.Hour = "05"
$spec.Scheduler.Interval = "1"
$spec.Action = New-Object VMware.Vim.MethodAction
$spec.Action.Name = "RebootGuest"
$scheduledTaskManager.CreateScheduledTask($vm.MoRef, $spec)
The time is entered in UTC, so if you want local time you will have to convert it.
Try something like this
$si = Get-View ServiceInstance
$scheduledTaskManager = Get-View $si.Content.ScheduledTaskManager
# The script assumes tjhat the CSV contains at least a column with the heading 'vmname'
#
# vmname
# vm1
# vm2
Import-Csv -Path .\vmnames.csv -UseCulture -PipelineVariable row |
ForEach-Object -Process {
$vm = Get-View -ViewType VirtualMachine -Filter @{"Name" = "^$($row.vmname)$" }
$spec = New-Object VMware.Vim.ScheduledTaskSpec
$spec.Name = "Restart $($row.vmname)"
$spec.Description = "Restart $($row.vmname)"
$spec.Enabled = $true
$spec.Scheduler = New-Object VMware.Vim.WeeklyTaskScheduler
$spec.Scheduler.Wednesday = $true
$spec.Scheduler.Hour = (Get-Date -Hour 5).ToUniversalTime().Hour
$spec.Scheduler.Interval = "1"
$spec.Action = New-Object VMware.Vim.MethodAction
$spec.Action.Name = "RebootGuest"
$scheduledTaskManager.CreateScheduledTask($vm.MoRef, $spec)
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Try something like this
$tasks = Get-View -Id (Get-View ScheduledTaskManager).ScheduledTask
Import-Csv -Path .\vmlist.csv -UseCulture |
ForEach-Object -Process {
$taskName = "Restart $($_.vmname)"
$task = $tasks | where{$_.Info.Name -eq $taskName}
if($task){
$task.RemoveScheduledTask()
}
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
The time is entered in UTC, so if you want local time you will have to convert it.
Try something like this
$si = Get-View ServiceInstance
$scheduledTaskManager = Get-View $si.Content.ScheduledTaskManager
# The script assumes tjhat the CSV contains at least a column with the heading 'vmname'
#
# vmname
# vm1
# vm2
Import-Csv -Path .\vmnames.csv -UseCulture -PipelineVariable row |
ForEach-Object -Process {
$vm = Get-View -ViewType VirtualMachine -Filter @{"Name" = "^$($row.vmname)$" }
$spec = New-Object VMware.Vim.ScheduledTaskSpec
$spec.Name = "Restart $($row.vmname)"
$spec.Description = "Restart $($row.vmname)"
$spec.Enabled = $true
$spec.Scheduler = New-Object VMware.Vim.WeeklyTaskScheduler
$spec.Scheduler.Wednesday = $true
$spec.Scheduler.Hour = (Get-Date -Hour 5).ToUniversalTime().Hour
$spec.Scheduler.Interval = "1"
$spec.Action = New-Object VMware.Vim.MethodAction
$spec.Action.Name = "RebootGuest"
$scheduledTaskManager.CreateScheduledTask($vm.MoRef, $spec)
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi Lucd, I am getting this error while running the script
That was from a test I did.
I corrected the code above.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi Lucd,
I am trying to run the script row by row using ISE. I did see an error while running the Import-csv command as below. please could you check and help me with that.
I have this data in the CSV file.
You can't run that line by line, there is a pipeline symbol at the end of the line.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi Lucd,
thanks for the script, I did ran it and its working. but when I check the schedule task the time is not set to what I want. It still showing the current date and time. I wanted to set BST time of Every Wednesday 05:00 AM . Please could you help with that.
Not sure what exactly you did but for me this is running at 5 am every wednesday.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi Lucd
The time zone I have in my environment is (UTC+00:00) Dublin, Edinburgh, Lisbon, London so is that the issue its not showing the correct time ?
What is this returning for you?
(Get-Date -Hour 5).ToUniversalTime().Hour
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi Lucd,
I did ran the script again and its working now. not sure what was the issue last time. could be a powershell ISE issue. I did closed the ISE and then open it again and ran the script. Its working now. Thank you very much for your help on this mate. Appreciate !!
Hi Luc,
I am trying to put the minutes also in this script but seems its not working. I did put like below and wanted to scheduled the task at 5.30 AM but its not working, its still create the task for 5 AM. Any thoughts pls
$spec.Scheduler.Hour = (Get-Date -Hour 5 -Minute 30).ToUniversalTime().Hour
Hi Luc,
I got it . I did added another line with -Minute and Its creating the schedule task with minutes too. Is there any way to modify the existing task , like the date and time which are creating using below code. Please can you help .
You can with the ReconfigureScheduledTask method.
Fetch the ScheduledTask you want to change, modify the ScheduledTaskSpec where you want and call that method
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I am trying to remove the scheduled task , I will create them again with changed timing. I am running below code , it did not through any error but the task is not getting removed . I can see the scheduled task is still there . Please can you help if I am missing something here.
$vms = Get-VM -Name (Import-Csv -Path .\vmlist.csv -UseCulture).Name
Get-View -Id (Get-View ScheduledTaskManager).ScheduledTask |
where{$_.Info.Name -eq $taskName} | %{
$_.RemoveScheduledTask()
}
What is in $taskName?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Task name is created from this code and its like "Restart VMName", i.e. Restart Server1
$spec.Name = "Restart $($row.vmname)"
It looks as if you only showed part of the code for the removal.
Can't really analyse why it wouldn't work.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi Luc,
So I am running below code to remove the scheduled Tasks form the VMs. The VMs is having task name as "Restart VMName" as shown below screen shot. But When I run this code the scheduled task is not deleting.
Code....
$vms = Get-VM -Name (Import-Csv -Path .\vmlist.csv -UseCulture).Name
Get-View -Id (Get-View ScheduledTaskManager).ScheduledTask |
where{$_.Info.Name -eq $taskName} | %{
$_.RemoveScheduledTask()
}
Screesnshot
That is exactly the same that you showed me before.
I don't see where $vms is used in that code.
I don't see where you place a value in $taskName.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference