PowerCLI: VMware Tools one-liners


In this post I’ll show you three PowerCLI one-liners to perform some operations with VMware Tools.

The first one-liner will return all Powered on VM’s with Windows selected as guest OS and perform a VMware Tools upgrade without a reboot at the end.

Get-VM | Where {$_.PowerState -eq "PoweredOn" -and $_.Guest.OSFullName -match "Win*"} | % {Update-Tools -VM $_ -NoReboot}

Sometimes if you want to perform a vMotion or when you try set a host in maintenance mode, the VM won’t vMotion away from the host. Most of the time this is caused by a mounted ISO from a locale datastore. In my environment this is not possible because I use a shared storage datastore to storage all the ISO files. But there is one ISO that’s mounted from a locale datastore, this is the VMware Tools ISO.  Sometimes the ISO isn’t automatically dismounted from the VM. This one-liner creates a list of VM’s where the VMware Tools ISO is mounted:

(Get-VM | Get-View | Where {$_.Runtime.ToolsInstallerMounted}) | % {$_.Name}

The last one-liner will dismount the VMware Tools if necessary:

(Get-VM | Get-View | Where {$_.Runtime.ToolsInstallerMounted}) | % {Dismount-Tools -VM $_.Name}

I think there will be a lot more possibilities to script with PowerCLI and VMware Tools. So if you have a question or an idea, please leave a comment below.

Advertisements

10 thoughts on “PowerCLI: VMware Tools one-liners

  1. Pingback: Tweets die vermelden PowerCLI: VMware Tools one-liners « ICT-Freak.nl -- Topsy.com

  2. Evgeny

    Hello,

    Really great post. espetialy with vmware tools upgrade.

    My question can you get inventory information via VMtools from Windows 2008 VM? Things like disks information and free space inside Virtual Machine by using Power CLI?
    I was looking for this informaiton but can’t find it anywhere.

    Thanks,
    Evgeny

    Reply
    1. albertwt

      use this mate,

      Clear-Host
      # Issue warning if % free disk space is less
      $percentWarning = 40;
      # Get server list
      $servers = Get-QADComputer -OSName “Windows Server*” | Select-Object -expand name

      $datetime = Get-Date -Format “yyyyMMddHHmmss”;

      # Add headers to log file
      #Add-Content “$Env:USERPROFILE\server disks $datetime.txt” “server,deviceID,size,freespace,percentFree”;
      # How many servers
      $server_count = $servers.Length;
      # processed server count
      $i = 0;

      foreach($server in $servers) {
      $server_progress = [int][Math]::Ceiling((($i / $server_count) * 100))
      # Parent progress bar
      Write-Progress -Activity “Checking $server” -PercentComplete $server_progress -Status “Processing servers – $server_progress%” -Id 1;
      Sleep(1); # Sleeping just for progress bar demo
      # Get fixed drive info
      $disks = Get-WmiObject -ComputerName $server -Class Win32_LogicalDisk -Filter “DriveType = 3”;

      # How many disks are there?
      $disk_count = $disks.Length;

      $x = 0;
      foreach($disk in $disks) {
      $disk_progress = [int][Math]::Ceiling((($x / $disk_count) * 100));
      $disk_name = $disk.Name;
      $volumename = $disk.VolumeName;
      Write-Progress -Activity “Checking disk $disk_name” -PercentComplete $disk_progress -Status “Processing server disks – $disk_progress%” -Id 2;
      Sleep(1);
      $deviceID = $disk.DeviceID;
      [float]$size = $disk.Size;
      [float]$freespace = $disk.FreeSpace;

      $percentFree = [Math]::Round(($freespace / $size) * 100, 2);
      $sizeGB = [Math]::Round($size / 1073741824, 2);
      $freeSpaceGB = [Math]::Round($freespace / 1073741824, 2);
      $usedGB = $sizeGB – $freeSpaceGB

      $colour = “Green”;
      if($percentFree -lt $percentWarning) {
      $colour = “Red”;
      }

      if ( $volumename -eq “” ) {
      $volumename = “*”
      }

      Write-Host -ForegroundColor $colour “$server $deviceID – disk size – $volumename ($sizeGB GB), used space ($usedGB GB), free space ($freeSpaceGB GB), percentage free space = $percentFree %”;
      $x++;
      }
      # Finish off the progress bar
      Write-Progress -Activity “Finished checking disks for this server” -PercentComplete 100 -Status “Done – 100%” -Id 2;
      Sleep(1); # Just so we see!
      $i++;
      }
      Write-Progress -Activity “Checked all servers” -PercentComplete 100 -Status “Done – 100%” -Id 1;
      Sleep(1);

  3. Ryan

    Can you tell me if running your first script will cause any down time, network outages, etc when the tools are updated?

    Reply
  4. afokkema

    it depends on which network adapter you use. If you are using the VMXNET3, the driver will be updated and you will lose the connection for a short period.

    Reply
  5. albertwt

    Hi Meneer. AFOKKEMA 🙂

    From whatI understand so far any Vmware tools upgrade will require VM to restart, so we must restart the VM anyway right ?

    Reply
  6. Pingback: Avoiding Those Pesky Reboots when Updating VMware Tools « Wahl Network

  7. raj

    Is there a way of updating the tools to the latest version even if the status of the tools stays at ok in gui.
    For E.g. ESXi 4.1 P04 had a fix for tools , however the version of the tools was not changed . Because of which the VMs still would report tools status as OK.
    For e.g. Build # for tools installed with 4.1 U3 was 784891 , and version was 8.3.17.
    With 4.1 P06 the build of the tools changed to 870839 however version remained same i.e. 8.3.17 because of which the VMware tools status remains at OK.

    Would like to know if there is a way of patching these VMs using PowerCli.

    Thanks and regards,
    Raj

    Reply
  8. Pingback: Erdal DİNÇ | Update VMware Tools without reboot

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s