VMware Cloud Community
khalil1974
Enthusiast
Enthusiast
Jump to solution

Help with Scheduled Task Reboot using csv file

Hi, i was trying to use my existing script(that is working for creating scheduled snapshot task using csv file), i assumed all i need to do to setup weekly reboot using csv file was to change

$snapMemory = $false REMOVE

$snapQuiesce = $true REMOVE

Change the action to $spec.Action.Name = "RebootGuest"

remove unused variable from and this will be @($spec.Name,$spec.Description) , updated script below but it seem there is error again, can you pls point me in the right direction

##########################

$emailAddr = 'xxx@xxx.com'

$fileName = 'C:\Temp\Scripts\VMware PowerCLI\reboot-weekly.csv'

###############

Import-Csv -Path $fileName -UseCulture | %{

    $vm = Get-VM -Name $_.VMName

    $si = get-view ServiceInstance

    $scheduledTaskManager = Get-View $si.Content.ScheduledTaskManager

    $spec = New-Object VMware.Vim.ScheduledTaskSpec

    $spec.Name = "Reboot of",$vm.Name -join ' '

    $spec.Description = $_.Description

    $spec.Enabled = $true

    $spec.Notification = $emailAddr

    $obj = New-Object -TypeName VMware.Vim.WeeklyTaskScheduler

    $obj.Hour = $_.StartTimeHour

    $obj.Minute = $_.StartTimeMin

    $obj."$($_.On)" = $true

    $obj.Interval = $_.Repeat

    $obj.ActiveTime = $_.StartDate

    $spec.Scheduler = $obj

    $spec.Action = New-Object VMware.Vim.MethodAction

  $spec.Action.Name = "RebootGuest"

    @($spec.Name,$spec.Description) | %{

        $arg = New-Object VMware.Vim.MethodActionArgument

        $arg.Value = $_

        $spec.Action.Argument += $arg

    }

    $scheduledTaskManager.CreateScheduledTask($vm.ExtensionData.MoRef, $spec)

}

error

483760_483760.pngpastedImage_16.png

1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

When you call a method in a scheduled task, you refer to the method in Action.Name.
And you pass all arguments to that method via Action.Argument.

But the RebootGuest method has no parameters, so the Action.Argument property stays empty.

##########################

$snapMemory = $false

$snapQuiesce = $true

$emailAddr = 'xxx@xxx.com'

$fileName = 'C:\Temp\Scripts\VMware PowerCLI\snapshot-weekly.csv'

###############

Import-Csv -Path $fileName -UseCulture | %{

    $vm = Get-VM -Name $_.VMName

    $si = get-view ServiceInstance

    $scheduledTaskManager = Get-View $si.Content.ScheduledTaskManager

    $spec = New-Object VMware.Vim.ScheduledTaskSpec

    $spec.Name = "Reboot of",$vm.Name -join ' '

    $spec.Description = $_.Description

    $spec.Enabled = $true

    $spec.Notification = $emailAddr

    $obj = New-Object -TypeName VMware.Vim.WeeklyTaskScheduler

    $obj.Hour = $_.StartTimeHour

    $obj.Minute = $_.StartTimeMin

    $obj."$($_.On)" = $true

    $obj.Interval = $_.Repeat

    $obj.ActiveTime = $_.StartDate

    $spec.Scheduler = $obj

    $spec.Action = New-Object VMware.Vim.MethodAction

    $spec.Action.Name = "RebootGuest"

    $scheduledTaskManager.CreateScheduledTask($vm.ExtensionData.MoRef, $spec)

}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

2 Replies
LucD
Leadership
Leadership
Jump to solution

When you call a method in a scheduled task, you refer to the method in Action.Name.
And you pass all arguments to that method via Action.Argument.

But the RebootGuest method has no parameters, so the Action.Argument property stays empty.

##########################

$snapMemory = $false

$snapQuiesce = $true

$emailAddr = 'xxx@xxx.com'

$fileName = 'C:\Temp\Scripts\VMware PowerCLI\snapshot-weekly.csv'

###############

Import-Csv -Path $fileName -UseCulture | %{

    $vm = Get-VM -Name $_.VMName

    $si = get-view ServiceInstance

    $scheduledTaskManager = Get-View $si.Content.ScheduledTaskManager

    $spec = New-Object VMware.Vim.ScheduledTaskSpec

    $spec.Name = "Reboot of",$vm.Name -join ' '

    $spec.Description = $_.Description

    $spec.Enabled = $true

    $spec.Notification = $emailAddr

    $obj = New-Object -TypeName VMware.Vim.WeeklyTaskScheduler

    $obj.Hour = $_.StartTimeHour

    $obj.Minute = $_.StartTimeMin

    $obj."$($_.On)" = $true

    $obj.Interval = $_.Repeat

    $obj.ActiveTime = $_.StartDate

    $spec.Scheduler = $obj

    $spec.Action = New-Object VMware.Vim.MethodAction

    $spec.Action.Name = "RebootGuest"

    $scheduledTaskManager.CreateScheduledTask($vm.ExtensionData.MoRef, $spec)

}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

khalil1974
Enthusiast
Enthusiast
Jump to solution

thank you works perfectly sir

0 Kudos