Skip to the main content.

ScriptRunner Blog

The best PowerCLI commands for admins – part 1: VM inventory management

Table of contents 

 

 

Post Featured Image

Discover the top PowerCLI commands for your everyday admin life  in Part 1 of this series, you'll learn more about meta data in VMware and the use of tags and custom attributes. 

VMware recognized the potential of PowerShell quite early, which is why the technology company's PowerShell module (PowerCLI) is very mature. With more than 800 cmdlets, the module enables an almost complete automation of the VMware products ESX, vCenter or even vSphere. This series of articles provides an insight into various cmdlets that, in my view, play an important role in the administration of VMware environments and can make everyday administration work much easier.

The series is composed of the following articles:

  • Introduction and VM inventory management (part 1)
  • Creating snapshots and VMware templates (part 2)
  • General VMware navigation and advanced information (part 3)

VMware offers a large number of attributes with its products, which are assigned to each virtual machine. To a certain extent, the use of these attributes also makes sense – but company-specific attributes are not included in the standard set. For this purpose, however, VMware offers the option of using tags or custom attributes. These can be used to manage a variety of meta data for virtual machines. In this article, you will learn why the use of such meta data in VMware makes sense and how you can use the tags and custom attributes via PowerShell and PowerCLI in such a way that manual administration tasks and their error rate are drastically reduced.

 

Tag management

One way to illustrate the use of tags would be to label virtual machines as production, test or development systems. Often this information is mapped via the naming scheme of the virtual machines. While this can work in many cases, there is no guarantee that the naming scheme will not change in the future, which can lead to inconsistencies – and between you and me, no one really wants to do regex acrobatics on inconsistent VM names, do they?

In contrast, active management of meta data, such as tags, provides a more efficient method. When a virtual machine is created, it can be assigned the appropriate type directly. Among other things, this eliminates the need for tedious searching for information about the name.

Tags and the associated specifications can be used to flexibly design automated processes. One possible use case, for example, would be for VMware tools to automatically update test machines during the night, rebooting the systems in the process, while productive systems are checked manually before a reboot is performed.

Tags, however, not only provide opportunities for system type maintenance, but many other application areas, too. PowerShell can be used to simplify the management of tags by creating and assigning them and then filtering by them.

The previously described example can be put into practice with the following code:

 


# Creates a new tag category
New-TagCategory -Name "SystemType" -EntityType VirtualMachine

# Creates a new tag in the previously created tag category
$TagSystemType = Get-TagCategory -Name "SystemType" |
New-Tag -Name "Production"

# Adds the tag to the VM object
$vmTagAssignmentParams = @{
Tag = $TagSystemType Confirm = $false
}
Get-VM |
Where-Object {$_.Name -match "servername"} |
New-TagAssignment @vmTagAssignmentParams

# Find all VMs with the tag "Production"
Get-VM |
Get-TagAssignment |
Where-Object {$_.Tag -like 'Production'}

Thus, first a tag category should be created (if none exists as yet) and then the respective tag should be created under this category. It is of course possible to create further tags of this category at any time. Afterwards, the tag can be assigned to a VM. The last line shows how all VMs to which a certain tag is assigned can be displayed. Afterwards, the output list can be further processed depending on the use case.

 

Custom attributes

Unlike tags, custom attributes are based on the key-value pattern. For tags, an assignment can take place through the category (for example: System type – Productive), but this should be well-thought-out. In this example, we wouldn't expect the number of tags in the "System Type" category to increase significantly.

The tag "staging" might be added, but other tags do not make much sense from a technical point of view. However, the situation is different if the "tags" or attributes do not represent a limited set. The following scenario:

In a company, the expected deletion date is specified when a virtual machine is requested (you surely know that licenses, servers or applications are gladly requested, but the requestor is not half as energetic when it comes to having the respective asset deleted again if no longer needed). Based on this deletion date, the functional supervisor can be notified shortly before the deletion date, so that the deletion deadline can either be postponed or the VM can be released for deletion.

In this example, it becomes apparent that the number of tags would be astronomically high if the respective deletion date is specified. The better choice here is to use "custom attributes". These are assigned directly to the VMs without belonging to a category. A custom attribute consists of a key/identifier and a value. In our example, the identifier is always "DeletionDate" or "DateOfRemoval" – the value is composed of a date. This results in the following PowerShell code:


# Load VM Object 
$VM = Get-VM "servername"
# Set date to "today in 3 years" and save as string
$DeletionDate = ((Get-Date).AddDays(365 * 3)).ToString("dd.MM.yyyy")
# Create new custom attribute
New-CustomAttribute -TargetType "VirtualMachine" -Name "DateOfRemoval"
#  Add deletion period as value to custom attribute and assign it to VM
$annotationParams = @{
CustomAttribute = "DateOfRemoval"
Value = $DeletionDate
}
$VM | Set-Annotation @annotationParams

# Get custom attributes of a VM 
(Get-VM "servername").CustomFields

So first the date is stored as a string in the requested format. Then a new custom attribute is created. In the next step, this is assigned to a concrete VM together with the previously specified date. The custom attributes of a VM can now be returned via the "CustomFields" attribute. A query about specific tags works with the following query:


Get-VM | Get-Annotation -CustomAttribute "DateOfRemoval"

You can limit the output by using the Where-Filter:


$Date = (Get-Date).ToString("dd.MM.yyyy") 
$filteredAnnotations = Get-VM | 
	Get-Annotation -CustomAttribute "DateOfRemoval" |
Where-Object { $_.Value -eq $Date }

 

Conclusion

As you can see, all information can be maintained using tags or custom attributes in VMware environments. It is important to pay attention to the correct usage  – PowerShell provides the necessary tools for managing tags as well as custom attributes.

 

Good2know

VMUG: Webcast

In May, ScriptRunner participated at a VMware User Group webcast:

sr-webinar-vmware



  

Managing vSphere environments can be challenging, but with the right automation solution it doesn't have to be. PowerShell and the PowerCLI module are great, but typically only used by scripting experts.

What if you could use PowerShell to not only standardize and automate your recurring management tasks, but also delegate them to help desk teams and even create self-services for end users based on your scripts? This would allow you to empower these users to manage snapshots, reconfigure VMs, stop/start their VMs, or generate infrastracture reports.

Of course, not everyone is comfortable with the command-line, and you certainly don't want to give these users privileged access to your VMware environment.

So what does it take to automate and delegate your recurring tasks efficiently and securely in your organization with PowerShell?

 

 

Click here for the webcast!

 

 

 

Related links

About the author: