MariusRoma
Expert
Expert

Create a task to update VMware Tools

I need to update VMware Tools on dozens ov VMs but I cannot reboot them during business hour.

I would like to create a job to schedule doring the night, but I would like to locate a (working) sample to start from.

Is such sample available anywhere?

Regards

marius

0 Kudos
6 Replies
LucD
Leadership
Leadership

Where do you want schedule the job?

In the vCenter or in a Windows Task Scheduler?


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

0 Kudos
LucD
Leadership
Leadership

When you want to use a vCenter scheduled task, you could do something like this.

Note1: this will create a scheduled task for each VM in a cluster. There are no additional tests, like for example if the VM is powered on.

Note2: When you are ready for a real run, change the $tSpec.Enabled to $true.

$stMgr = Get-View ScheduledTaskManager

Get-Cluster -Name cluster |

Get-VM -PipelineVariable vm | Select-Object -First 1 |

ForEach-Object -Process {

    $ma = New-Object VMware.Vim.MethodAction

    $ma.Argument = $null

    $ma.Name = "UpgradeTools_Task"


    $ar1 = New-Object VMware.Vim.MethodActionArgument

    $ar1.Value = $null

    $ma.Argument += $ar1

    # Run at 23:59:59

    $dTScheduler = New-Object VMware.Vim.OnceTaskScheduler

    $dTScheduler.runAt = Get-Date -Hour 23 -Minute 59 -Second 59

    # Create the scheduled task

    $tSpec = New-Object VMware.Vim.ScheduledTaskSpec

    $tSpec.Action = $ma

    $tSpec.Description = "Upgrade VMware Tools on " + $vm.Name

    $tSpec.Enabled = $false

    $tSpec.Name = "Tools upgrade " + $vm.Name

    $tSpec.Notification = "my.email@address.com"

    $tSpec.Scheduler = $dTScheduler

    $stMgr.CreateScheduledTask($vm.ExtensionData.MoRef, $tSpec)

}


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

MariusRoma
Expert
Expert

I attempted to use the code but I suspect that there is something missing.

The value of $tSpec looks as follows:

Name         : Tools upgrade MyVM

Description  : Upgrade VMware Tools on MyVM

Enabled      : False

Scheduler    : VMware.Vim.OnceTaskScheduler

Action       : VMware.Vim.MethodAction

Notification : my.email@address.com

I get an error on the following line:

     $stMgr.CreateScheduledTask($vm.ExtensionData.MoRef, $tSpec)

and the error is:

Impossibile chiamare un metodo su un'espressione con valore null.

(Impossible to call a method on an expression with null value)

In C:\<my path>\myscript.ps1:80 car:5

+     $stMgr.CreateScheduledTask($vm.ExtensionData.MoRef, $tSpec)

+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException

    + FullyQualifiedErrorId : InvokeMethodOnNull

I looked for documentation about "UpgradeTools_Task" but I was unaboe to find anything helpful, at least for me...

What can I do in order to make the code run?

Regards

marius

0 Kudos
LucD
Leadership
Leadership

Can you check if

Get-View ScheduledTaskManager

returns anything?

Or perhaps multiple objects (when you are connected to multiple vSphere servers).


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

0 Kudos
MariusRoma
Expert
Expert

Inserting the line:

Get-View ScheduledTaskManager

in my code returns:

ScheduledTask : {ScheduledTask-schedule-1501, ScheduledTask-schedule-1502, ScheduledTask-schedule-2, ScheduledTask-schedule-3}

Description   : VMware.Vim.ScheduledTaskDescription

LinkedView    :

MoRef         : ScheduledTaskManager-ScheduledTaskManager

Client        : VMware.Vim.VimClientImpl

Is it correct?

I am connected to a single vSphere server.

Regards

marius

0 Kudos
LucD
Leadership
Leadership

That is correct and only a single object.
What I then don't understand is why the error states that the variable $stMgr is $null.

How are you calling the code? From a .ps1 file?


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

0 Kudos