VMware Cloud Community
AGFlora
Enthusiast
Enthusiast
Jump to solution

How to retrieve data objects

HI

Does anyone have an example of code to display a custome value? I've searched the web but could not come up with any examples.

This is the info i've got from the Get-Member cmdlet:

TypeName : VMware.Vim.VirtualMachine

Name : CustomValue

MemberType : Property

Definition : VMware.Vim.CustomFieldValue[] CustomValue {get;}

The follwoing is from the PowerGUI script editor:

VMware.Vim.CustomFieldStringValue

To my knowledge you cannot load data objects directly. You have to access data objects via managed objects. While I understand this in theory I have a hard time putting it together. I've tried using the MOB, and the API documentation but just don't know where to start. Any example given would be greatly appreciated.

Thanks in advance

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

If you just want to retrieve (read) custom values you don't need to go to the SDK.

The custom fields are present in the object that the Get-Vm cmdlet returns as a hash array.

You can do

$vm = Get-Vm <VM-name>
$vm.CustomFields.GetEnumerator()
$vm.CustomFields.Keys
$vm.CustomFields.Values
$vm.CustomFields[<keyname>]


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

View solution in original post

0 Kudos
23 Replies
LucD
Leadership
Leadership
Jump to solution

If you just want to retrieve (read) custom values you don't need to go to the SDK.

The custom fields are present in the object that the Get-Vm cmdlet returns as a hash array.

You can do

$vm = Get-Vm <VM-name>
$vm.CustomFields.GetEnumerator()
$vm.CustomFields.Keys
$vm.CustomFields.Values
$vm.CustomFields[<keyname>]


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

0 Kudos
DougBaer
Commander
Commander
Jump to solution

This may help

#Attach to the CustomFieldsManager
$si = Get-View ServiceInstance
$cfmRef = $si.Content.CustomFieldsManager
$CustomFieldsManager = Get-View $cfmRef

#Show the names of the custom fields
$CustomFieldsManager.Field | select Name

#Set a field to a specific value
$CustomFieldsManager.SetField($VM.MoRef,$key_MyField,"theValueGoesHere")

note: the $key_MyField is the integer representing the custom field...

If you just want to show the values (say, for example, the values assigned to VM objects, you can use the CustomFields property of the VM object:

[vSphere PowerCLI] C:\> $vm = get-vm TestVM
[vSphere PowerCLI] C:\> $vm.CustomFields

Key                                     Value
---                                     -----
MyCustomField1                          theValueGoesHere

Message was edited by: DougBaer to clean up theServiceInstance code

Doug Baer, Solution Architect, Advanced Services, Broadcom | VCDX #019, vExpert 2012-23
0 Kudos
AGFlora
Enthusiast
Enthusiast
Jump to solution

Thanks! exactly what I was looking for.

0 Kudos
AS80
Contributor
Contributor
Jump to solution

Hi,

I am trying to export the Annotations on all of VM's to CSV file.. is there any script that can help me get this information

In powercli I can use this command

(get-vm VCB110GH).customfields.getenumerator() | export-csv C:\export.csv

and it displays me the Asset ID and other information about the VM

is that possible I can get it for all VM's in a CSV file

Thanks

0 Kudos
AS80
Contributor
Contributor
Jump to solution

Hi,

I am trying to export the Annotations on all of VM's to CSV file.. is there any script that can help me get this information

In powercli I can use this command

(get-vm VCB110GH).customfields.getenumerator() | export-csv C:\export.csv

and it displays me the Asset ID and other information about the VM

is that possible I can get it for all VM's in a CSV file

Thanks

0 Kudos
LucD
Leadership
Leadership
Jump to solution

You could do something like this

$report = @()
get-vm| %{
	$vmName = $_.Name
	$_.CustomFields.getenumerator() | %{
		$row = "" | Select Name, Key, Value
		$row.Name = $vmName
		$row.Key = $_.Key
		$row.Value = $_.Value
		$report += $row
	}
}
$report | Export-Csv "C:\export.csv" -noTypeInformation


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

0 Kudos
AS80
Contributor
Contributor
Jump to solution

Thanks it worked for me.. Much Appreciated

0 Kudos
AS80
Contributor
Contributor
Jump to solution

Luc,

If I have to import the same values back in to the same virtual center what do I have to do?

Thanks

0 Kudos
LucD
Leadership
Leadership
Jump to solution

This is not the fastest method but it should work

Import-Csv "C:\export.csv" | %{
	Set-CustomField -Entity (Get-VM $_.Name) -Name $_.Key -Value $_.Value
}


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

0 Kudos
AS80
Contributor
Contributor
Jump to solution

Thanks it works.. took some time but i was able to import this information in less then 20 minutes for 60 VM's

0 Kudos
Yogie
Contributor
Contributor
Jump to solution

This is a great script but I'm missing the NOTES field...!?

Joerg Knoerchen IT Datacenter Services & Sharepoint Support Specialist EMEA BCD Travel
0 Kudos
LucD
Leadership
Leadership
Jump to solution

The Notes field is another property.

Retrieve it with

Get-Vm | Select Name, Notes

To change it use

Get-Vm <vmname> | Set-Vm -Description "My notes field"

Or did you want to export this to an CSV and import it back again ?


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

0 Kudos
Yogie
Contributor
Contributor
Jump to solution

Hi,

well for me it would be great to get all annotations including the notes field exported (to CSV) for report and backup of the annotation fields and of course reimporting these informations back would be great to have!


Joerg Knoerchen

IT Datacenter Services & Sharepoint Support Specialist EMEA

BCD Travel

Joerg Knoerchen IT Datacenter Services & Sharepoint Support Specialist EMEA BCD Travel
0 Kudos
LucD
Leadership
Leadership
Jump to solution

I would propose to go for two CSV files, one for the Notes field and one for the annotations.

The reason, if you put all in one CSV file, you will have to repeat the Notes property on each Annotation line.


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

0 Kudos
Yogie
Contributor
Contributor
Jump to solution

Hi LucD,

I'm not familiar with the PowerCLI right now how would the loop look like when I would like to have a CSV with

"VMName";"NOTES"


Joerg Knoerchen

IT Datacenter Services & Sharepoint Support Specialist EMEA

BCD Travel

Joerg Knoerchen IT Datacenter Services & Sharepoint Support Specialist EMEA BCD Travel
0 Kudos
LucD
Leadership
Leadership
Jump to solution

You can do something like this

$report = @()
get-vm | %{
	$report += New-Object PSObject -Property @{
		"vmName" = $_.Name
		"note"   = $_.Description
	}
}
$report | Export-Csv "C:\export.csv" -noTypeInformation -UseCulture


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

0 Kudos
Yogie
Contributor
Contributor
Jump to solution

Thank you so much!

Would this work then for Import?

Import-Csv "D:\VMNotes.csv" | %{
	Set-Vm (Get-VM $_.Name) -Description $_.Description
}

Joerg Knoerchen

IT Datacenter Services & Sharepoint Support Specialist EMEA

BCD Travel

Joerg Knoerchen IT Datacenter Services & Sharepoint Support Specialist EMEA BCD Travel
0 Kudos
Yogie
Contributor
Contributor
Jump to solution

You can do something like this

> $report = @()
> get-vm | %{
> 	$report += New-Object PSObject -Property @{
> 		"vmName" = $_.Name
> 		"note"   = $_.Description
> 	}
> }
> $report | Export-Csv "C:\export.csv" -noTypeInformation -UseCulture
> 

Sorry but the code throws an error here:

New-Object : Member "note" not found for the given Net object.

At D:\Program Files\VMware\Infrastructure\vSphere PowerCLI\Scripts\ReportNotes.ps1:4 char:23

+ $report += New-Object <<<< PSObject -Property @{

+ CategoryInfo : InvalidOperation: (Smiley Happy , InvalidOperationException

+ FullyQualifiedErrorId : InvalidOperationException,Microsoft.PowerShell.Commands.NewObjectCommand

New-Object : Member "vmName" not found for the given Net object.

At D:\Program Files\VMware\Infrastructure\vSphere PowerCLI\Scripts\ReportNotes.ps1:4 char:23

+ $report += New-Object <<<< PSObject -Property @{

+ CategoryInfo : InvalidOperation: (Smiley Happy , InvalidOperationException

+ FullyQualifiedErrorId : InvalidOperationException,Microsoft.PowerShell.Commands.NewObjectCommand

Joerg Knoerchen

IT Datacenter Services & Sharepoint Support Specialist EMEA

BCD Travel

Joerg Knoerchen IT Datacenter Services & Sharepoint Support Specialist EMEA BCD Travel
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Oops, forgot to ask, are you using PowerShell v2 ?


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

0 Kudos