VMware Cloud Community
matkay521225
Contributor
Contributor

Creating multiple threads

I've written a script to set a specific parm on my VMs (upgrade vmtools at boot).  I've set it up to do this per cluster.  I would like to know how to re-code it to run against more than 1 VM at a time.  Any help is appreciated.

Connect-VIServer -Server myvCenter -User myUser -Password myPassword

$clusterName = "myCluster"

$vmConfigSpec = New-Object VMware.Vim.VirtualMachineConfigSpec

$vmConfigSpec.Tools = New-Object VMware.Vim.ToolsConfigInfo

## Configure the script to set the automatic tools upgrade parameter on or off

##  Only 1 section should be enabled at a time (Comment-UnComment carefully)


## UnComment the next line to turn auto upgrade ON

$vmConfigSpec.Tools.ToolsUpgradePolicy = "UpgradeAtPowerCycle"

## UnComment the next line to turn auto upgrade OFF

## $vmConfigSpec.Tools.ToolsUpgradePolicy = "manual"

Get-Cluster -Name $clusterName | Get-VM | sort Name | %{

  $_.Extensiondata.ReconfigVM($vmConfigSpec) ##  Could this be coded into multiple threads instead of 1 VM at a time?

  Write-Host $_","$? ##  Could this be coded into multiple threads instead of 1 VM at a time?

}

0 Kudos
1 Reply
LucD
Leadership
Leadership

Most vSphere API methods come in 2 flavours, with and without a _Task suffix.

The ReconfigVM method has both options.

If you call the method with ReconfigVM_Task it will call the method, and return immediately to the script.

Similar to what is done with RunAsync switch on several PowerCLI cmdlets.

Another option is to use PowerShell jobs (background jobs).

Have a look at the Start-Job cmdlet..


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

0 Kudos