Tag Archives: powercli

PowerShell Snippet – Find Windows 2008 VMs before EOL

Support for Windows Server 2008 and 2008 R2 ends in January 2020– just two years from the date of this post. Four years ago I put up a script to find XP and 2003 VMs, and I’ve modified this to search a vSphere environment for powered on VMs running Server 2008 as a Guest OS:

Get-VM |
 Where {$_.PowerState -eq "PoweredOn" -and
 ($_.Guest -like "*Server 2008*")} |
 get-VMGuest |
 select VmName, OSFullName

VmName    OSFullName
------    ----------
MyVM1     Microsoft Windows Server 2008 R2 (64-bit)
MyVM2     Microsoft Windows Server 2008 R2 (64-bit)
MyVM3     Microsoft Windows Server 2008 (64-bit)
MyVM4     Microsoft Windows Server 2008 R2 (64-bit)


Exploring Tags and PowerCLI

Tags were added to vSphere back in version 5.1 so they’re not a new feature but are still often overlooked. One or more tags can be applied to items (entities) in the inventory and then used as a search term or metadata not only in the GUI but also through tools such as PowerCLI. This post covers a few useful cmdlets for working with tags.


There are a number of cmdlets which deal with tags, here’s a quick list using Get-Command.


Notice that there’s three Nouns used here- “Tag” represents the tag itself. “TagAssignment” represents a relationship between a tag and another object (for example “This VM has been assigned This (or These) tags). Finally there’s “TagCategory” which represents the category that a tag belongs to.

Getting Tags

So, what can we do with tags in PowerCLI? Well, first we can look at a list of all the tags using Get-Tag. This returns a lot of information, particularly if you have assigned tags already, so we can neaten the quick view using the PowerShell “Select” function to show just the tag name and description:

Get-Tag | Select Name, Description

Name                 Description
– –                 – – – – – –
UrlShortener         URL Shortener Service
Documents            Document Management Service
Change               Change Management Service

In this example, I’ve created three tags to represent three different services operating in my environment. We can carry on from here and find out which entities have been assigned the “Documents” tag- i.e. what VMs form the Document Management Service.

(Get-TagAssignment |
  Where {$_.Tag.Name -eq 'Documents'}).Entity

Name                 PowerState Num CPUs MemoryGB
– –                  – – – – – – – – – – – – –
DocuWebServ          PoweredOn  1        4.000

DocuDBServ           PoweredOn  2        16.000
DocuFileServ         PoweredOn  1        4.000

Or we could flip that and ask the question- “What tags does this VM have assigned?”

Get-VM "DocuWebServ" |
     Get-TagAssignment | Select Tag

– –


Getting Bigger

As we’re using PowerCLI we can join more and more functions together and make bigger and bigger queries. For example, we can  list all VMs with their tags in a table.

Get-VM |
      Select Name,@{Name="Tags";Expression={(Get-TagAssignment -Entity $_).Tag.Name}} |
      Where {$_.Tags} |
      Format-Table - Autosize

Name         Tags
– –          – –
DocuWebServ  {Documents, WebServers}
DocuDBServ   Documents
DocuFileServ Documents
URLShort1    {UrlShortener, WebServers}
URLShort2  {UrlShortener, WebServers, TestAndDev}

This is only scratching the surface of the possibilities- by having useful metadata that lives with the VM and can be accessed programmatically we have plenty of avenues to explore in automation and reporting.


I was lucky enough to take delivery of some new ESXi hosts recently. After installing them in the datacentre, I wanted to test that the network had been patched correctly. This environment is going to have Distributed vSwitches configured, but I wanted to test the physical connectivity before joining them to vCenter- have the physical NICs been patched to the correct networks?

PowerCLI to the rescue! I put together some code which automates this process. Provided with a hostname and a list of NICs and targets which should respond, the code fires off a ping for each interface in turn and reports back with success/fail messages.

For each NIC it creates a temporary switch, portgroup, and VMkernel interface:

#Create Virtual Switches
$Switch1=New-VirtualSwitch -Name "sw_Connectivity_Test" -Nic $Nic
#Create PortGroups
$Portgroup1=New-VirtualPortGroup -Name "pg_Connectivity_Test" -VirtualSwitch $Switch1
#Create VMK Adapter
$vmk1=New-VMHostNetworkAdapter -PortGroup $Portgroup1 -VirtualSwitch $Switch1 -IP $HostIP -SubnetMask $SubnetMask

Then the esxcli functionality is used to ping a given target address:

#Test the connection
$esxcli= get-esxcli -V2                              #Use the ESXCLI to run the ping from the host
$arguments = $esxcli.network.diag.ping.CreateArgs()
$arguments.host=$TargetIP                            #Set IP Address to Ping
$arguments.count="2"                                 #How Many Times to Ping
$arguments.interface=$vmk1                           #Use the configured VMKernel Interface

Once the test is complete, the temporary virtual network components are removed.

#Tidy up- delete all the Networking Components created
Remove-VMHostNetworkAdapter $vmk1 -Confirm:$false
Remove-VirtualPortGroup $PortGroup1 -Confirm:$false
Remove-VirtualSwitch $Switch1 -Confirm:$false

The full code is available for download (and potential improvement) on GitHub.



Finding Deprecated VMFS Volumes

Following an upgrade to vSphere 6, hosts popped up an error message reporting that some old VMFS volumes were found on the host. Whilst these still worked, it would be prudent to tidy them up and replace them with modern datastores. The message is:

“Deprecated VMFS volume(s) found on the host. Please consider upgrading volume(s) to the latest version”


Whilst this highlights that there is an issue, the GUI message doesn’t indicate which volumes (or how many) are affected. The following quick bit of PowerCLI produces a table of the datastores with a VMFS version of less than 5.

Get-Datastore |
 Where-Object { $_.ExtensionData.Info.VMFS.MajorVersion -lt 5} |
 Select-Object Name, FreeSpaceGB, CapacityGB, @{n='VMFS Version';e={$_.ExtensionData.Info.VMFS.MajorVersion}}

Example Output:

Name           FreeSpaceGB CapacityGB VMFS Version
----           ----------- ---------- ------------
VOL-VM01            574.77    2047.75            3
VOL-VM02           1924.05    2047.75            3