Category Archives: VMware

Fun With 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.

CmdLets

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

image

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

Tag
– –
Documents

WebServers

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.

test-ESXi-Network

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.

Code
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
$Result=($esxcli.network.diag.ping.Invoke($arguments)).Summary.Recieved

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.

 

 

Let’s talk about SexiGraf

I was introduced to SexiGraf by Eric Bussink during a roundtable at the April London VMUG meeting. The room was divided into those who said “Oh yeah, SexiGraf, it’s awesome” and those, like me, who hadn’t yet discovered it.

2017-06-20

SexiGraf is a free community tool that creates some great (you might even say sexy) graphs based on vCenter statistics. Whilst it might not have all the bells and whistles of the big commercial monitoring solutions it does provide a neat, easy to use, web-based interface to get at those important headline figures and how they’ve changed over time.

Screenshot_20170620-092020The small footprint  (2vCPU, 2GB) makes it ideal for the homelab, but there’s also scope for keying into larger production environments to get a quick look at the state of your environment, event from a mobile device when you’re at the airport or in a meeting.

Installation is straightforward and well documented- SexiGraf deploys via an OVF template to the Virtual Infrastructure and connection to vCenter just requires a read-only vSphere credential to be provided. Updates are quick and easy too as a simple patch package can be uploaded via the web admin interface and the server patched in minutes without any loss of history.

Having run this myself for just a couple of months I can already see the benefits both from a capacity planning point of view but also when troubleshooting- “is this host using resources differently to others?” or “is the cluster usage different to normal?”.

As well as the vSphere statistics, SexiGraf is continually expanding it’s range. VMware vSAN, Windows, and FreeNAS connectivity is all offered and HP C7000 and S.M.A.R.T counters are under development.

If you haven’t yet discovered it, I’d recommend having a look. Downloads and full details are here- http://www.sexigraf.fr/