Hello,
We are doing a rolling check for our VI of "Check and upgrade Tools before each power-on", using the code below.
get-vm | foreach-object { $vmConfigSpec = New-Object VMware.Vim.VirtualMachineConfigSpec $vmConfigSpec.Tools = New-Object VMware.Vim.ToolsConfigInfo $vmConfigSpec.Tools.ToolsUpgradePolicy = "UpgradeAtPowerCycle" (Get-View $_.ID).ReconfigVM($vmConfigSpec) }
With that said, we would like to use a text based import and do only about 100 at a time. Can you help? The script would pull from a file and only tick the box for guests listed in the text file.
Hope this make sense, and thanks in advance.
Oops, my mistake.
That one should have said
$vmConfigSpec = New-Object VMware.Vim.VirtualMachineConfigSpec $vmConfigSpec.Tools = New-Object VMware.Vim.ToolsConfigInfo $vmConfigSpec.Tools.ToolsUpgradePolicy = "UpgradeAtPowerCycle" Get-Content -Path "C:\names.txt" | %{ (get-vm -Name $_).Extensiondata.ReconfigVM($vmConfigSpec) }
____________
Blog: LucD notes
Twitter: lucd22
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
...and if possible, what about at a cluster level? So, two different pieces of code
If your input file is a simple .txt file with this layout
vmname1 vmname2 ...
you could do
$vmConfigSpec = New-Object VMware.Vim.VirtualMachineConfigSpec $vmConfigSpec.Tools = New-Object VMware.Vim.ToolsConfigInfo $vmConfigSpec.Tools.ToolsUpgradePolicy = "UpgradeAtPowerCycle" Get-Content -Path "C:\names.txt" | %{ get-vm -Name $_ $_.Extensiondata.ReconfigVM($vmConfigSpec) }
This requires PowerCLI 4.1 due to the Extensiondata property used.
Not sure what you want to do with the 2nd script.
Do you provide the clusternames from a file ?
Or do you want all guests in a cluster ?
____________
Blog: LucD notes
Twitter: lucd22
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
If I only want to tick the box for all guests in a certain cluster. I don't have to import from a file, can it be part of the code? So, if I want to make the change for all guests in Cluster1 and only Cluster1
Then you can do
$clusterName = "cluster1" $vmConfigSpec = New-Object VMware.Vim.VirtualMachineConfigSpec $vmConfigSpec.Tools = New-Object VMware.Vim.ToolsConfigInfo $vmConfigSpec.Tools.ToolsUpgradePolicy = "UpgradeAtPowerCycle" Get-Cluster -Name $clusterName | Get-VM | %{ $_.Extensiondata.ReconfigVM($vmConfigSpec) }
You implicitly, through the pipeline, use the -Location parameter on the Get-VM cmdlet to limit the guests returned to only those present under that container.
This could for example also be a specific datastore (Get-Datastore) or a specific host (Get-VMHost).
____________
Blog: LucD notes
Twitter: lucd22
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
also, got the error below when trying to run the code provided
You cannot call a method on a null-valued expression. At :line:7 char:28 + $_.Extensiondata.ReconfigVM <<<< ($vmConfigSpec)
the error I posted was from the script that pulls from a txt file. The cluster script works great!!
Oops, my mistake.
That one should have said
$vmConfigSpec = New-Object VMware.Vim.VirtualMachineConfigSpec $vmConfigSpec.Tools = New-Object VMware.Vim.ToolsConfigInfo $vmConfigSpec.Tools.ToolsUpgradePolicy = "UpgradeAtPowerCycle" Get-Content -Path "C:\names.txt" | %{ (get-vm -Name $_).Extensiondata.ReconfigVM($vmConfigSpec) }
____________
Blog: LucD notes
Twitter: lucd22
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
That did the trick, thank you sir!