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 .
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
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
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
Thnaks.where-clause can be used .
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.
I'm afraid Update-Tools does not support WhatIf.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks.