VMware Cloud Community
jvm2016
Hot Shot
Hot Shot
Jump to solution

upgrade vmware tools_powercli

Hi Luc/All,

could you check the following code to upgrade vmware tools .

blue color is giving output of all vms depending on nested properties .

pastedImage_2.png

However i want to add further "if" logic (red color) in order to get specific vm(shccertvsr01 )only  where vmware tools upgrade is needed but its looping to entire vm list.is this not the right place to put if statement.

$conn=$global:defaultviserver

$vc = @("tcclabc002vctt01.servers.chrysler.com","shccertvcs01.shdc.chrysler.com")

if ($conn -eq $null)

{

write-host "connecting to" $vc

Connect-VIServer -Server $vc -user "administrator@vsphere.local" -password ""

}

else{

write-host "powercli is connected "

}

$vm_names=get-cluster shccert|get-vmhost|get-vm

foreach ($vm in $vm_names)

{

Get-VM $vm | select Name,@{N='Tools status';E={$_.extensiondata.guest.toolsstatus}},@{N="Tools Version Status";E={$_.extensiondata.guest.toolsversionstatus}},@{N="tools running status";E={$_.extensiondata.guest.toolsrunningstatus}}

if ($vm.extensiondata.guest.toolstatus -ne "toolsOK")

{

write-host "update the vmware tools of" $vm

Get-VMGuest $vm | update-Tools -NoReboot

}

}

disconnect-viserver "*" -force -confirm:$false

Tags (1)
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Not 100% sure I get the question, but you can use a Where-clause to limit the VMs that enter the ForEach loop.

foreach ($vm in (Get-Cluster shccert|Get-VM | where{$_.extensiondata.guest.toolstatus -ne "toolsOK"}))

{

    $vm | select Name,@{N='Tools status';E={$_.extensiondata.guest.toolsstatus}},

            @{N="Tools Version Status";E={$_.extensiondata.guest.toolsversionstatus}},

            @{N="tools running status";E={$_.extensiondata.guest.toolsrunningstatus}}

}

In fact you can leave out the ForEach altogether

Get-Cluster shccert|Get-VM | where{$_.extensiondata.guest.toolstatus -ne "toolsOK"} |

select Name,@{N='Tools status';E={$_.extensiondata.guest.toolsstatus}},

            @{N="Tools Version Status";E={$_.extensiondata.guest.toolsversionstatus}},

            @{N="tools running status";E={$_.extensiondata.guest.toolsrunningstatus}}


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

View solution in original post

5 Replies
LucD
Leadership
Leadership
Jump to solution

Not 100% sure I get the question, but you can use a Where-clause to limit the VMs that enter the ForEach loop.

foreach ($vm in (Get-Cluster shccert|Get-VM | where{$_.extensiondata.guest.toolstatus -ne "toolsOK"}))

{

    $vm | select Name,@{N='Tools status';E={$_.extensiondata.guest.toolsstatus}},

            @{N="Tools Version Status";E={$_.extensiondata.guest.toolsversionstatus}},

            @{N="tools running status";E={$_.extensiondata.guest.toolsrunningstatus}}

}

In fact you can leave out the ForEach altogether

Get-Cluster shccert|Get-VM | where{$_.extensiondata.guest.toolstatus -ne "toolsOK"} |

select Name,@{N='Tools status';E={$_.extensiondata.guest.toolsstatus}},

            @{N="Tools Version Status";E={$_.extensiondata.guest.toolsversionstatus}},

            @{N="tools running status";E={$_.extensiondata.guest.toolsrunningstatus}}


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

jvm2016
Hot Shot
Hot Shot
Jump to solution

Thnaks.where-clause can be used .

Reply
0 Kudos
jvm2016
Hot Shot
Hot Shot
Jump to solution

so i modified like below.    can i use -whatif in  blue color line as tab does not work .

$vmtoupdate=Get-Cluster TCC003 | Get-VM | where{$_.extensiondata.guest.toolsversionstatus -ne "guesttoolscurrent"}

foreach ($vm in $vmtoupdate)

{

Get-VMGuest $vm | update-Tools -NoReboot

}

Thanks for your support.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

I'm afraid Update-Tools does not support WhatIf.


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

Reply
0 Kudos
jvm2016
Hot Shot
Hot Shot
Jump to solution

Thanks.

Reply
0 Kudos