VMware Cloud Community
parisvc
Enthusiast
Enthusiast
Jump to solution

Script to update vmware tools then vm hardware version then reboot

Does anyone have a script that would update all vm's vmware tools then their h/w verision and then reboot them?

I would like to do this for all our vm's on our cluster not sure if there's an option but if it's possible to randomize the time it's done in between a certain time frame that would be perfect so it would cause too much load at the same time. If not I could just update machines with dev* in their ame then prod* for example.

1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

For Windows the VMware Tools are not guest managed, so that is easier to update.

Do you want to do these VMs in sequence? No parallelism?

If in sequence, you could do something like this (there is a random 'sleep' (between 30 and 60 seconds) after the reboot)

$clusterName = 'MyCluster'

Get-Cluster -Name $clusterName | Get-VM |

ForEach-Object -Process {

    if($_.Guest.ExtensionData.ToolsStatus -eq 'toolsOld)'){

        Update-Tools -VM $_ -NoReboot

    }

    if($_.Version -ne 'v11'){

        Set-VM -Version v11 -Confirm:$false

    }

    Restart-VMGuest -VM $_ -Confirm:$false

    sleep (Get-Random -Minimum 30 -Maximum 60)  

}

And yes, the VCSA has the so-called "guest managed" tools.

You will need a guest OS command (in the VCSA) to update the VMware Tools.


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

View solution in original post

14 Replies
LucD
Leadership
Leadership
Jump to solution

What kind of guest OS do you have on the VMs?

Do you happen to have Linux VM with the builtin open Tools?


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

0 Kudos
parisvc
Enthusiast
Enthusiast
Jump to solution

Windows, I could install an ubuntu vm if easier?

I forgot to mention we are running ESXI 6.0 hosts with a 6.5 VCSA.

Does the VCSA have the built in tools?

0 Kudos
LucD
Leadership
Leadership
Jump to solution

For Windows the VMware Tools are not guest managed, so that is easier to update.

Do you want to do these VMs in sequence? No parallelism?

If in sequence, you could do something like this (there is a random 'sleep' (between 30 and 60 seconds) after the reboot)

$clusterName = 'MyCluster'

Get-Cluster -Name $clusterName | Get-VM |

ForEach-Object -Process {

    if($_.Guest.ExtensionData.ToolsStatus -eq 'toolsOld)'){

        Update-Tools -VM $_ -NoReboot

    }

    if($_.Version -ne 'v11'){

        Set-VM -Version v11 -Confirm:$false

    }

    Restart-VMGuest -VM $_ -Confirm:$false

    sleep (Get-Random -Minimum 30 -Maximum 60)  

}

And yes, the VCSA has the so-called "guest managed" tools.

You will need a guest OS command (in the VCSA) to update the VMware Tools.


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

parisvc
Enthusiast
Enthusiast
Jump to solution

looks good thanks.

0 Kudos
tkdreamer2
Enthusiast
Enthusiast
Jump to solution

Hello Luc

I suppose that I have to list my Windows VM first to make this script first? I need to avoid my Linux machines and also the appliances.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

If the VMs are powered on and you have VMware Toos installed, you could use a Where-clause.

Something like this for example

Get-Cluster -Name $clusterName | Get-VM |

Where-Object {$_.Guest.OSFullName -like "*Windows*"}

To make sure you capture all your VMs with a Windows-based guest OS, you might first pull a report.

Get-Cluster -Name $clusterName | Get-VM |

Select Name,@{N='OS';E={$_.Guest.OSFullName}}


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

0 Kudos
tkdreamer2
Enthusiast
Enthusiast
Jump to solution

Thank you but the script reveals another problem - - '

It seems that some Linux VM were created with a Windows OS setting...

So I have a few Linux VM in the output!!!

Fortunatelly I can find my Windows VM by their names because the topology was well defined.

Thank you!

0 Kudos
LucD
Leadership
Leadership
Jump to solution

What is reported under the Guest property are the values that VMware Tools actually discovered inside the guest OS.
And indeed, you can create a VM for a Linux OS but install a Windows OS on there.


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

0 Kudos
tkdreamer2
Enthusiast
Enthusiast
Jump to solution

Right, but how can we explain we have some Linux in the output?
I used the remote console of some of the "Windows" listed in the output: those are really Linux...

0 Kudos
LucD
Leadership
Leadership
Jump to solution

What does this return?

Get-Cluster -Name $clusterName | Get-VM |

Select Name,PowerState,

   @{N='Tools';E={$_.ExtensionData.Guest.ToolsRunningStatus}},

  GuestId,@{N='OS';E={$_.Guest.OSFullName}}


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

0 Kudos
tkdreamer2
Enthusiast
Enthusiast
Jump to solution

I've got my Windows vm of course but also some  Linux, like (I don't copy the VM name):

PoweredOnguestToolsRunningrhel7_64GuestRed Hat Enterprise Linux 7 (64-bit)

Just for you to know, I use "Get-Cluster *"

0 Kudos
LucD
Leadership
Leadership
Jump to solution

That is normal with the last snippet of code, it just lists all VMs.

But does the one with Where-clause also list Linux VMs?

Get-Cluster -Name $clusterName | Get-VM |

Where-Object {$_.Guest.OSFullName -like "*Windows*"}


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

tkdreamer2
Enthusiast
Enthusiast
Jump to solution

This one works! I have less VM and it doesn't seem to have Linux VMs.

Thanks

0 Kudos
tkdreamer2
Enthusiast
Enthusiast
Jump to solution

I didn't it missed a pipe annd we can make the command line shorter:

get-vm | ? {$_.Guest.OSFullName -match " Windows"}

0 Kudos