VMware Cloud Community
Shigura
Contributor
Contributor
Jump to solution

Script to list Annotations

All

Could some kind soul point me in the direction of how to pull out the information from Custom Attribute Values under the VM's global/Virtual Machine Annotation sections.

Bascially I've added Custom Attributes showing the hosts installed applications, contact support teams etc, and I wanted to pull all of these out into a report.

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

For an obscure reason the current VITK build doesn't contain a Get-CustomField cmdlet.

See also .

But as rusev remarked the CustomField property is present in each VI object.

You could for example use a script like this one to get at the fields.

$vmImpl = Get-VM <VM-name>
for($i=0; $i -lt $vmImpl.CustomFields.Count; $i++){
  Write-Host $vmImpl.CustomFields.Keys[$i] "equals" $vmImpl.CustomFields.Values[$i]
}

Or you could use Hal's


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
3 Replies
LucD
Leadership
Leadership
Jump to solution

For an obscure reason the current VITK build doesn't contain a Get-CustomField cmdlet.

See also .

But as rusev remarked the CustomField property is present in each VI object.

You could for example use a script like this one to get at the fields.

$vmImpl = Get-VM <VM-name>
for($i=0; $i -lt $vmImpl.CustomFields.Count; $i++){
  Write-Host $vmImpl.CustomFields.Keys[$i] "equals" $vmImpl.CustomFields.Values[$i]
}

Or you could use Hal's


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
Shigura
Contributor
Contributor
Jump to solution

Thanks Luc

I'm just starting out, could you give me some advise on how to run this as part of a script which

goes to my VC, runs against all VM's and then lists the custom fields results under each VM, all contained in one HTML file!!

Not asking much heh Smiley Happy

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Sure, most of it is already built in into PowerShell.

The straightforward method, which will produce a rather bland HTML page, goes like this.

$report =@()
get-vm | % {
	for($i = 0; $i -lt $_.CustomFields.Count; $i ++ ){
		$row = "" | Select VMname, FieldKey, FieldValue
		$row.VMname = $_.Name
		$row.FieldKey = $_.CustomFields.Keys[$i]
		$row.FieldValue = $_.CustomFields.Values[$i]
		$report += $row
	}
}
$report | ConvertTo-Html -title "Custom fields" | Set-Content "c:\Temp\Test.html"
Invoke-Item "C:\Temp\Test.html"

We loop through all the guests and for each guest we collect the guest's name and it custom fields.

Then we use the Convertto-Html cmdlet to translate the object array we created into an HTML page.

You can also construct your HTML page yourself.

That gives you complete control over the resulting HTML page but requires a certain HTML knowledge (which I unfortunately do not have Smiley Happy )

$page = @()
$page += "<html>"
$page += "<head>"
$page += "<title>Custom fields</title>"
$page += "</head>"
$page += "<body>"
get-vm | % {
    $page += "<H2>"
    $page += $_.Name
	$page += "</H2>"
    $page += "<table>"
    $page += "<colgroup>"
    $page += "<col>"
    $page += "<col>"
    $page += "</colgroup>"
    $page += "<tr><th>Key</th><th>Value</th></tr>"
	for($i = 0; $i -lt $_.CustomFields.Count; $i ++ ){
		$page += "<tr><td>" + $_.CustomFields.Keys[$i] + "</td><td>" + $_.CustomFields.Values[$i] + "</td></tr>"
	}
	$page += "</table>"
}
$page += "</body>"
$page += "</html>"

$page | Set-Content "c:\Temp\Test.html"
Invoke-Item "C:\Temp\Test.html"

In this script we do not use the Convertto-Html cmdlet but we construct the HTML page ourselves.

We store all the HTML lines in an array ($page) and finally we write the array to a HTML file.

PS: I attached the 2nd script since the forum SW seems to have problems with HTML tags inside the code.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos