Work Smart, not Hard – Automate Custom Attributes

I’m not sure how widely vCenter custom attributes are used in general, but I certainly find them useful for a summary of important information in a single view (the VM summary tab). The only problem with using additional annotations, is that the data only becomes useful if it is maintained and can be relied upon.

So with this in mind I am setting out to create a series of workflows which will automatically populate these values without any manual intervention. From the custom attributes that I use there are two sources of information (vCenter and a MSSQL database). This post will cover the vCenter data, and a future one will cover the SQL data.

Since I use fully automated HA/DRS clusters including storage DRS I want to know (from the summary tab) which datastore the VM is located on. Yes, I know which cluster it’s on but I cant see the actual datastore without going to the maps or storage views tab.

In addition to this requirement, I want to know which resource pool the VM is located in (in the case of newly provisioned systems, or an admin simply moving the VM to a different pool).

So my requirements are simple;

  • I want a single summary view of reliable data.
  • I want the values updated dynamically without manual intervention.

Here is an example of the custom annotations that I use;

For the first stage, I am going to create a PowerCLI script which will be executed on a set schedule. This will query vCenter, and then update the values for all the VM’s in scope.

Here is the script, just remember to change the highlighted rows to suite your own environment;

## Connect to vCenter
$vcenter = 'myvcenter.mydomain.fqdn'
Write-Host "Connecting to $vcenter" -ForegroundColor Green
Connect-VIServer $vcenter | Out-Null

## Update Datastores Custom Attribute
Write-Host "Updating VM Custom Attributes - Datastore" -ForegroundColor Yellow

$datastores = Get-Datacenter "DC-MYDATACENTER-01" | Get-Datastore | Where {$_.Name -notmatch "-das-b1"} | sort name

foreach ($datastore in $datastores){
    Write-Host " - $datastore"
    Get-Datastore $datastore | Get-VM | Set-CustomField -Name "Datastore" -Value $datastore -confirm:$false | Out-Null

## Update Resource Pools Custom Attribute
Write-Host "Updating VM Custom Attributes - Resource Pool" -ForegroundColor Yellow

$respools = Get-Datacenter "DC-MYDATACENTER-01" | Get-ResourcePool | Where {$_.Name -like "RP-*" } | sort Name

foreach ($respool in $respools){
   Write-Host " - $respool"
   Get-ResourcePool $respool | Get-VM | Set-CustomField -Name "Resource Pool" -Value "$respool" -confirm:$false | Out-Null

## Disconnect from vCenter
Write-Host "Disconnecting from $vcenter" -ForegroundColor Yellow
Disconnect-VIServer -Server $vcenter -Force -Confirm:$false

For the other custom attributes that I need to fetch from a SQL database I am currently using a ODBC data connection to build up the PowerShell code in MS-Excel which can then be refreshed on demand, copied and pasted ready for execution.

So although this works, it’s a manual process and I want to automate this fully … but that will be in a follow up post once I have written the script and tested the process.

23,454 total views, 30 views today

Share on LinkedInShare on FacebookTweet about this on TwitterShare on Google+Digg thisShare on RedditPin on PinterestEmail this to someonePrint this page

Speak Your Mind