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
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
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
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
Thanks! exactly what I was looking for.
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
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
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
Thanks it worked for me.. Much Appreciated
Luc,
If I have to import the same values back in to the same virtual center what do I have to do?
Thanks
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
Thanks it works.. took some time but i was able to import this information in less then 20 minutes for 60 VM's
This is a great script but I'm missing the NOTES field...!?
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
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
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
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
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
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
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: ( , 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: ( , InvalidOperationException
+ FullyQualifiedErrorId : InvalidOperationException,Microsoft.PowerShell.Commands.NewObjectCommand
Joerg Knoerchen
IT Datacenter Services & Sharepoint Support Specialist EMEA
BCD Travel
Oops, forgot to ask, are you using PowerShell v2 ?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference