VMware Cloud Community
Troy_Clavell
Immortal
Immortal
Jump to solution

Get-Content

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.

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

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

View solution in original post

0 Kudos
8 Replies
Troy_Clavell
Immortal
Immortal
Jump to solution

...and if possible, what about at a cluster level? So, two different pieces of code Smiley Happy

0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

Troy_Clavell
Immortal
Immortal
Jump to solution

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

0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

Troy_Clavell
Immortal
Immortal
Jump to solution

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)

0 Kudos
Troy_Clavell
Immortal
Immortal
Jump to solution

the error I posted was from the script that pulls from a txt file. The cluster script works great!!

0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

0 Kudos
Troy_Clavell
Immortal
Immortal
Jump to solution

That did the trick, thank you sir!

0 Kudos