Breakdown Multivalued vROPS Custom Properties / Metrics values to Separate Columns.

Breakdown Multivalued vROPS Custom Properties / Metrics values to Separate Columns.

Its been a long time since I blogged on anything, got a tricky request from one of our large customers, they were running a vROPS report to get a set of vSphere tags for VM’s in their environment.

vROPS did give them the report as below

but the customer’s expectation was to get each of the vSphere tags split into their multiple columns

Unfortunately vROPS cant split property/metric data into its very own individual column.

Hence – I resorted to a bit of PowerCLI and Powershell to get the format the customer needs it.

You can find the script below which does all this conversion’s – notes are also updated for reference.

This script / (scriptlet in your master script ) can be used to split data into multiple columns for any property/metric.

 

param ($vropsFqdn,$vropsUserName,$vropsPassword,$outputfilename)

Import-Module VMware.PowerCLI -ErrorAction SilentlyContinue

$secpasswd = ConvertTo-SecureString $vropsPassword -AsPlainText -Force
$vccredential = New-Object System.Management.Automation.PSCredential ($vropsUserName, $secpasswd)




# Connect to the vROPS Instance and get all the VM's monitored by vROPS
$server =Connect-OMServer $vropsFqdn -Credential $vccredential
$vms = Get-OMResource -ResourceKind Virtualmachine
#$a= Get-OMResource -Name dev-198


# Initialize an Array to iterate across all the VM's, find the number of tags each of them has and get the maximum count of tags across VM's.
# As multiple VM's can have separate count of tags, this number would be used to create the exact number of key value pairs for the VM PSObject so that the  number of columns and row's remain consistent
# per vm

$count = @()


foreach ($a in $vms)

{

$tags = (($a.ExtensionData.GetResourceProperties()).property | ?{$_.name -eq 'summary|tag'}).value.TrimStart('[').TrimEnd(']').split(",").trim()
#<vSphere Tag-Production>, <Cost Center-90210>

$count += $tags.count

}


$maxtagcount = ($count | measure -Maximum).Maximum

# Decrease the count by 1 as we are iterating though an array , with this we dont create additional dynamic columns

$maxtagcount = $maxtagcount  - 1

# Next we iterate across all VM's and extract the custom tags per VM and export that to a CSV file, we also replace the unwanted 'vSphere-Tag-' String which comes across
# Note how cleanely vROPS puts up a none across VM's which do not have any tags, nice touch!

$tagsd = @()

foreach ($a in $vms)

{

[array]$tags = (($a.ExtensionData.GetResourceProperties()).property | ?{$_.name -eq 'summary|tag'}).value.TrimStart('[').TrimEnd(']').split(",").trim()
#<vSphere Tag-Production>, <Cost Center-90210>


$Object = [pscustomobject][ordered]@{
        VmName = $a.name
}




0..$maxtagcount | % {


Add-Member -InputObject $Object -NotePropertyName "VMtag$_" -NotePropertyValue ($tags[$_] -replace 'vSphere Tag-',"")


}

$tagsd += $Object

}

$tagsd | ft -AutoSize -Wrap

$CurrentDate = Get-Date
$CurrentDate = $CurrentDate.ToString('MM-dd-yyyy_hh-mm-ss')

$outputfilename = (Get-Location).path + $outputfilename +  "_" + $CurrentDate + ".csv"



$tagsd | Export-csv $outputfilename -Force -NoClobber -NoTypeInformation

Disconnect-OMServer -Server $server -Force -Confirm:$false

 

Once you run the above, you would see the output is exactly as what the customer expected!

 

 

 

Share this post

Post Comment