VMware Cloud Community
DennieTidwell
Enthusiast
Enthusiast
Jump to solution

Attempting to mark all VMs on vCenter to "upgradeAtPowerCycle" for VMTools install

Reference Hal Rottenberg's book page 258 - yes I bought it, no, I haven't read it all

Attempting to mark all VMs on vCenter to "upgradeAtPowerCycle" for VMTools install

Errors and details below. All points will be awarded. Thank you.

PowerCLI C:\> $spec = new-object vmware.vim.virtualmachineconfigspec
PowerCLI C:\> $spec.Tools = new-object vmware.vim.toolsconfiginfo
PowerCLI C:\> $spec.Tools.ToolsUpgradePolicy = 'upgradeAtPowerCycle'
PowerCLI C:\> $vmview = get-vm | % { (get-view $_).MoRef}
PowerCLI C:\> $vmview.ReconfigVM( $spec )
Method invocation failed because [System.Object[]] doesn't contain a method nam
ed 'ReconfigVM'.
At line:1 char:19
+ $vmview.ReconfigVM <<<< ( $spec )
    + CategoryInfo          : InvalidOperation: (ReconfigVM:String) [], Runtim
   eException
    + FullyQualifiedErrorId : MethodNotFound

PowerCLI C:\> $spec


ChangeVersion                :
Name                         :
Version                      :
Uuid                         :
InstanceUuid                 :
NpivNodeWorldWideName        :
NpivPortWorldWideName        :
NpivWorldWideNameType        :
NpivDesiredNodeWwns          :
NpivDesiredPortWwns          :
NpivTemporaryDisabled        :
NpivOnNonRdmDisks            :
NpivWorldWideNameOp          :
LocationId                   :
GuestId                      :
AlternateGuestName           :
Annotation                   :
Files                        :
Tools                        : VMware.Vim.ToolsConfigInfo
Flags                        :
ConsolePreferences           :
PowerOpInfo                  :
NumCPUs                      :
NumCoresPerSocket            :
MemoryMB                     :
MemoryHotAddEnabled          :
CpuHotAddEnabled             :
CpuHotRemoveEnabled          :
VirtualICH7MPresent          :
VirtualSMCPresent            :
DeviceChange                 :
CpuAllocation                :
MemoryAllocation             :
CpuAffinity                  :
MemoryAffinity               :
NetworkShaper                :
CpuFeatureMask               :
ExtraConfig                  :
SwapPlacement                :
BootOptions                  :
VAppConfig                   :
FtInfo                       :
VAppConfigRemoved            :
VAssertsEnabled              :
ChangeTrackingEnabled        :
Firmware                     :
MaxMksConnections            :
GuestAutoLockEnabled         :
ManagedBy                    :
MemoryReservationLockedToMax :
DynamicType                  :
DynamicProperty              :

PowerCLI C:\> $spec.Tools


ToolsVersion         :
AfterPowerOn         :
AfterResume          :
BeforeGuestStandby   :
BeforeGuestShutdown  :
BeforeGuestReboot    :
ToolsUpgradePolicy   : upgradeAtPowerCycle
PendingCustomization :
SyncTimeWithHost     :
LastInstallInfo      :
DynamicType          :
DynamicProperty      :

PowerCLI C:\> $vmview

Type                                    Value
----                                    -----
VirtualMachine                          vm-1419
VirtualMachine                          vm-1481
VirtualMachine                          vm-1134
VirtualMachine                          vm-1098
VirtualMachine                          vm-1099
VirtualMachine                          vm-1097
VirtualMachine                          vm-1094
VirtualMachine                          vm-1182
VirtualMachine                          vm-1432
VirtualMachine                          vm-974
VirtualMachine                          vm-976
VirtualMachine                          vm-975

etc. etc. etc.

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Your $vmview variable holds an array of VirtualMachine objects, not a single VirtualMachine object.

And the ReconfigVM method is known on a VirtualMachine object, not on an array.

What you can do

$spec = new-object vmware.vim.virtualmachineconfigspec 
$spec
.Tools = new-object vmware.vim.toolsconfiginfo
$spec
.Tools.ToolsUpgradePolicy = 'upgradeAtPowerCycle'
get-vm
| % {     $_.Extensiondata.ReconfigVM($spec) }

Do the call for each VM seperately in a foreach loop.

The VirtualMachine object is available through the Extensiondata property. So you can call the ReconfigVM method on that property.


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

View solution in original post

Reply
0 Kudos
14 Replies
LucD
Leadership
Leadership
Jump to solution

Your $vmview variable holds an array of VirtualMachine objects, not a single VirtualMachine object.

And the ReconfigVM method is known on a VirtualMachine object, not on an array.

What you can do

$spec = new-object vmware.vim.virtualmachineconfigspec 
$spec
.Tools = new-object vmware.vim.toolsconfiginfo
$spec
.Tools.ToolsUpgradePolicy = 'upgradeAtPowerCycle'
get-vm
| % {     $_.Extensiondata.ReconfigVM($spec) }

Do the call for each VM seperately in a foreach loop.

The VirtualMachine object is available through the Extensiondata property. So you can call the ReconfigVM method on that property.


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

Reply
0 Kudos
DennieTidwell
Enthusiast
Enthusiast
Jump to solution

Thank you Sir!

Points awarded. Your book is next purchase.

And for the remaining helpful points ... can I backout of this change by doing the same thing, but leaving the $spec.Tools.ToolsUpgradePolicy = 'upgradeAtPowerCycle'  empty '' like this $spec.Tools.ToolsUpgradePolicy = ''

??

Thxs.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Thanks.

If you don't assign any value to the ToolsUpgradePolicy property (which will result in $null) the setting will be reset.


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

aTOMic201110141
Contributor
Contributor
Jump to solution

Good afternoon.

I am in the same type of problem, but since I am a fairly fresh programmer I dont know how to solve this. So hoping for some expert help.

I am getting my VM list from a CSV file, that seem to throw off this function

$vmlist = Import-Csv "C:\disk\vms.csv"

$vmConfigSpec = New-Object VMware.Vim.VirtualMachineConfigSpec

$extra = New-Object VMware.Vim.optionvalue

$extra.Key="disk.EnableUUID"

$extra.Value="true"

$vmConfigSpec.extraconfig += $extra

foreach($vm in $vmlist)

{

     $vm.ReconfigVM($vmConfigSpec)

}

I end up with the same error indicating that I am passing an array instead of a value to the foreach loop.

My CSV looks like this

Name
vm1
vm2


Any help would be much appreciated.

Regards, Tom

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Try it like this

$vmlist = Import-Csv "C:\disk\vms.csv"
$vmConfigSpec = New-Object VMware.Vim.VirtualMachineConfigSpec 
$extra
= New-Object VMware.Vim.optionvalue
$extra
.Key="disk.EnableUUID"
$extra
.Value="true"
$vmConfigSpec.extraconfig += $extra
foreach
($vm in $vmlist) {      (Get-VM -Name $vm.Name).ExtensionData.ReconfigVM($vmConfigSpec) }


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

Reply
0 Kudos
aTOMic201110141
Contributor
Contributor
Jump to solution

Thank you.

I tried this but ended up with the following errors.

Get-VM : 08.05.2012 14:44:35    Get-VM        VM with name '@{Name=vm1}' was not found, using the specified filter(s).
At C:\appl\PowerCLI\Test_VMX.ps1:21 char:13
+      (Get-VM <<<<  -Name $vm).ExtensionData.ReconfigVM($vmConfigSpec)
    + CategoryInfo          : ObjectNotFound: (:) [Get-VM], VimException
    + FullyQualifiedErrorId : Core_OutputHelper_WriteNotFoundError,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetVM
You cannot call a method on a null-valued expression.
At C:\appl\PowerCLI\Test_VMX.ps1:21 char:49
+      (Get-VM -Name $vm).ExtensionData.ReconfigVM <<<< ($vmConfigSpec)
    + CategoryInfo          : InvalidOperation: (ReconfigVM:String) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull
Get-VM : 08.05.2012 14:44:39    Get-VM        VM with name '@{Name=vm2}' was not found, using the specified filter(s).
At C:\appl\PowerCLI\Test_VMX.ps1:21 char:13
+      (Get-VM <<<<  -Name $vm).ExtensionData.ReconfigVM($vmConfigSpec)
    + CategoryInfo          : ObjectNotFound: (:) [Get-VM], VimException
    + FullyQualifiedErrorId : Core_OutputHelper_WriteNotFoundError,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetVM
You cannot call a method on a null-valued expression.
At C:\appl\PowerCLI\Test_VMX.ps1:21 char:49
+      (Get-VM -Name $vm).ExtensionData.ReconfigVM <<<< ($vmConfigSpec)
    + CategoryInfo          : InvalidOperation: (ReconfigVM:String) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

It seems it returns @{Name=vm1} instead of just vm1 as the name should be, perhaps that is the reason for the rest of the errors as well?

Would it be possible to use $vm.Name instead of just $vm ?

Regards, Tom

Edit: Yeah, I just changed to the following statement

(Get-VM -Name $vm.Name).ExtensionData.ReconfigVM($vmConfigSpec)

That seemed to solve the problem.

Thanks again for your help, much apprecaited Smiley Happy

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

I jsut updated the code.

It seems you already found the error 🙂


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

Reply
0 Kudos
aTOMic201110141
Contributor
Contributor
Jump to solution

I will ask one more thing while at it Smiley Wink

We have about 3000 VMs in this environment and a lot of the servers already have the statement in the VMX file. So I should have a check to see if the "disk.EnableUUID" key is present already and just abort that VMX file if it is. Is that a simple thing to add in?

Regards, Tom

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

You can find the current settings under $vm.ExtensionData.Config.ExtraConfig.

With a where-clause it should be easy to filter the ones you want.


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

Reply
0 Kudos
aTOMic201110141
Contributor
Contributor
Jump to solution

Good morning.

I found this code from you from another post.

foreach($vm in Get-VM){
  $vm.ExtensionData.Config.ExtraConfig |
  where {$_.Key -eq "disk.enableUUID"} | select @{N="VM";E={$vm.Name}},@{N="disk.enableUUID";E={$_.Value}}
}

Since I am new to coding, I am not at all sure how to fit this into my current script, the below being the important part of it.


foreach($vm in $vmlist)

{

     (Get-VM -Name $vm.Name).ExtensionData.ReconfigVM($vmConfigSpec)

}

My scenario is that the key either exists with the correct value, or it is not there at all. Two things that worry me is a) what happens if I run the lower part and put in the value in a file that already have the value from before, will it be a double entry or will the VM itself be smart enough to sense that on reboot and keep just one line? And b) if it should happen to be there from before, but with "false", will it edit the current setting or add another line making a possible conflict of the two settings?

Sorry for asking such stupid questions, I am trying to learn to do this proper Smiley Happy

Regards, Tom

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

This implements the Where-clause

$vmlist = Import-Csv "C:\disk\vms.csv"
$vmConfigSpec = New-Object VMware.Vim.VirtualMachineConfigSpec 
$extra = New-Object VMware.Vim.optionvalue 
$extra.Key="disk.EnableUUID" 
$extra.Value="true" 
$vmConfigSpec.extraconfig += $extra
foreach
($vm in $vmlist) {      Get-VM -Name $vm.Name |
    
where {$_.ExtensionData.Config.ExtraConfig |        where {$_.Key -eq "disk.enableUUID" -and !$_.Value}} | %{         $_.ExtensionData.ReconfigVM($vmConfigSpec)     } }


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

Reply
0 Kudos
aTOMic201110141
Contributor
Contributor
Jump to solution

So this will just add the key and value to the files that are missing the key alltogether then?

If so, that is super, thank you so much for the help.

Regards, Tom

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Not only those where the entry is missing, but also those where the value is $False.


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

aTOMic201110141
Contributor
Contributor
Jump to solution

It seems there is something that don't work with this last script. When I tested it on a VM I could power on and off, and edit the VMX file manually on, it did not write the value to the file even though it did not give any errors. The first script, the one without checking works good, both on a file that has it, and a file that don't have the value. The testing show that it does not matter if the value is there from before or not, if it's there it will just overwrite with same value, if it's not it will put it there. So I guess the first draft of the script will be more than good enough in this special case anyways.

So thanks again, good to get this help.

Reply
0 Kudos