Hello,
I have the below script:
$vCname = Read-Host “Enter the vCenter or ESXi host name to connect”
Connect-viserver $vcname
$VirtualMachines = Get-Content "VMList.txt"
if (-not (Get-PSSnapin VMware.VimAutomation.Core -ErrorAction SilentlyContinue)) {
Add-PSSnapin VMware.VimAutomation.Core
}
foreach ($vm in $VirtualMachines ) {
if($_.config.Tools.ToolsUpgradePolicy -ne "UpgradeAtPowerCycle") {
$config = New-Object VMware.Vim.VirtualMachineConfigSpec
$config.ChangeVersion = $vm.ExtensionData.Config.ChangeVersion
$config.Tools = New-Object VMware.Vim.ToolsConfigInfo
$config.Tools.ToolsUpgradePolicy = "UpgradeAtPowerCycle"
$vm.ExtensionData.ReconfigVM($config)
Write-Host "Update Tools Policy on $vm completed"
}
}
I tried also to add something like
$myVM = Get-View -Id $vm.Id
$myVM.ReconfigVM_Task($config)
Instead of $vm.ExtensionData.ReconfigVM($config) but i have the error message:
Get-View : Cannot validate argument on parameter 'Id'. The argument is null or empty. Supply an argument that is not null or empty and th
en try the command again.
At C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\Scripts\UpdateToolsPolicy.ps1:25 char:29
+ $myVM = Get-View -Id <<<< $vm.Id
+ CategoryInfo : InvalidData: (:) [Get-View], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.ViCore.Cmdlets.Commands.DotNetInterop.GetVIView
You cannot call a method on a null-valued expression.
At C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\Scripts\UpdateToolsPolicy.ps1:26 char:24
+ $myvM.ReconfigVM_Task <<<< ($config)
+ CategoryInfo : InvalidOperation: (ReconfigVM_Task:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
PowerCLI Version:
PowerCLI C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI> Get-PowerCLIVersion
PowerCLI Version
----------------
VMware vSphere PowerCLI 5.5 Release 1 build 1295336
---------------
Snapin Versions
---------------
VMWare AutoDeploy PowerCLI Component 5.5 build 1262826
VMWare ImageBuilder PowerCLI Component 5.5 build 1262826
VMware vCloud Director PowerCLI Component 5.5 build 1295337
VMware License PowerCLI Component 5.5 build 1265954
VMware VDS PowerCLI Component 5.5 build 1295334
VMware vSphere PowerCLI Component 5.5 build 1295334
Any idea?
Try like this
$vCname = Read-Host “Enter the vCenter or ESXi host name to connect”
if (-not (Get-PSSnapin VMware.VimAutomation.Core -ErrorAction SilentlyContinue)) {
Add-PSSnapin VMware.VimAutomation.Core
}
Connect-viserver $vcname
$VirtualMachines = Get-Content "VMList.txt"
foreach ($vm in (Get-VM -Name $VirtualMachines)){
if($vm.config.Tools.ToolsUpgradePolicy -ne "UpgradeAtPowerCycle") {
$config = New-Object VMware.Vim.VirtualMachineConfigSpec
$config.ChangeVersion = $vm.ExtensionData.Config.ChangeVersion
$config.Tools = New-Object VMware.Vim.ToolsConfigInfo
$config.Tools.ToolsUpgradePolicy = "UpgradeAtPowerCycle"
$vm.ExtensionData.ReconfigVM($config)
Write-Host "Update Tools Policy on $vm completed"
}
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Do you get an error on the 1st script as well ?
Is there a valid object in the $VM variable for the 2nd option ?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Sorry i didn't mention it. The first script runs but it's like the settings didn't apply.
PowerCLI C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\Scripts> .\UpdateToolsPolicy.ps1
â?oEnter the vCenter or ESXi host name to connectâ??:HOSTNAME
Name Port User
---- ---- ----
HOSTNAME 443 DOMAIN\ADMINUSER
Update Tools Policy on ServerName completed
Thank you
Is the VM powered on or powered off, when you run the 1st script ?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
The VM is powered on. I've done all these checks.
The only thing i believe maybe is failed because of the Get-Content but I think that I used it correct. I have run out of ideas, this is the test script, in the last version before I do it to ~150 VMs I will have also the check for if the VM is powered on. But i would like first to find the problem to this simpliest way.
Try like this
$vCname = Read-Host “Enter the vCenter or ESXi host name to connect”
if (-not (Get-PSSnapin VMware.VimAutomation.Core -ErrorAction SilentlyContinue)) {
Add-PSSnapin VMware.VimAutomation.Core
}
Connect-viserver $vcname
$VirtualMachines = Get-Content "VMList.txt"
foreach ($vm in (Get-VM -Name $VirtualMachines)){
if($vm.config.Tools.ToolsUpgradePolicy -ne "UpgradeAtPowerCycle") {
$config = New-Object VMware.Vim.VirtualMachineConfigSpec
$config.ChangeVersion = $vm.ExtensionData.Config.ChangeVersion
$config.Tools = New-Object VMware.Vim.ToolsConfigInfo
$config.Tools.ToolsUpgradePolicy = "UpgradeAtPowerCycle"
$vm.ExtensionData.ReconfigVM($config)
Write-Host "Update Tools Policy on $vm completed"
}
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
This worked like a charm.
Thank you man. Enjoy