PeetersOnline.nl
Add Vmx Path to VI Client using Powershell
The following script is a request from David Gontie, who was kind enough to comment on a previous post.
He’d like to add the location of his vm’s to a custom field. This is especially handy if you store all the files for a vm in a single datastore.
Here you go David:
############################## # Script created by Hugo Peeters # # http://www.peetersonline.nl # ############################## # Variables $VCServerName = "MYVCSERVER" $CustomFieldName = "VMX" $ManagedObjectType = "VirtualMachine" # Script $VC = Connect-VIServer $VCServerName $SI = Get-View ServiceInstance $CFM = Get-View $SI.Content.CustomFieldsManager $myCustomField = $CFM.Field | Where {$_.Name -eq $CustomFieldName} If (!$myCustomField) { # Create Custom Field $FieldCopy = $CFM.Field[0] $CFM.AddCustomFieldDef($CustomFieldName,$ManagedObjectType,$FieldCopy.FieldDefPrivileges,$FieldCopy.FieldInstancePrivileges) $myCustomField = $CFM.Field | Where {$_.Name -eq $CustomFieldName} } # Fill Custom Fields $VMs = Get-VM ForEach ($VM in $VMs) { $VMView = $VM | Get-View $VMXPath = $VMView.Config.Files.VMPathName # Compare value to current value If ($VMXPath -ne ($VMView.CustomValue | ?{$_.Key -eq $myCustomField.Key}).Value) { # Set Custom Value $VMView.setCustomValue($CustomFieldName,$VMXPath) } Clear-Variable VMXPath -ErrorAction SilentlyContinue Clear-Variable VMView -ErrorAction SilentlyContinue } Disconnect-VIServer -Confirm:$False |
Enjoy!
Hugo
11 Responses to Add Vmx Path to VI Client using Powershell
Tags
Active Directory API bind order cleanup cluster CPU Custom Fields datastores description device management directory tree errors Event Log file name filter Fun function HA IT known issues License Server LUN multipath NIC objects Oneliner portgroups PowerCLI PowerShell profile recursive Registry Scripts security session share snapshots SQL Stat VI Toolkit VMware vSphere WMI WSUS ZenArchives
- July 2012
- July 2011
- February 2011
- January 2011
- December 2010
- May 2010
- October 2009
- September 2009
- August 2009
- July 2009
- June 2009
- May 2009
- April 2009
- March 2009
- February 2009
- January 2009
- December 2008
- November 2008
- October 2008
- September 2008
- August 2008
- July 2008
- June 2008
- May 2008
- April 2008
- March 2008





[...] Add Vmx Path to VI Client using Powershell | PeetersOnline.nl. [...]
»crosslinked«
Hi Hugo,
Excellent script, will test it tomorrow first thing tomorrow.
Wish I had your PS knowledge!
Gr,
David
Hi Hugo,
I just tested your script and it works fine. This is also a nice extra attribute which I did not think of but definitely will use.
The attribute I wanted to add is the foldername in vcenter where the vm is stored in hierarchy. So if you in vcenter to Vm’s and templates, then you have foldernames overthere to group your vm’s.
With this script I’ll definitely will find it, except if you know the syntax how to extract the foldername immediately
Another question, did you follow a PS course or do you know if a PS course exists only for esx?
Cheers,
David
Ah, I see I misunderstood your request. I’ll think about the vm folders when I have the time.
I did not follow any course. I started here: http://www.microsoft.com/technet/scriptcenter/hubs/msh.mspx
The beauty of Powershell is that you can explore and learn about it’s possibilities once you know the basics.
For the PowerCLI (former VI Toolkit; Powershell snapins for VMware), it’s my curiosity that brought me the knowledge
Hugo
Hugo,
Ik wou je gerust in het NL mailen, maar aangezien je veel engelse posts krijgt wou ik het voor iedereen makkelijker maken
.
In ieder geval bedankt al voor de hulp, eens onze migratie hier achter de rug is ga ik mij ook wat proberen te verdiepen in powershell.
Gr,
David
Hugo,
Ben er ondertussen spijtig genoeg nog niet in geslaagd om de foldername ergens terug te vinden. Heb jij dit toevallig al gevonden?
Gr,
David
Very Nice Script.
But, how can i deactivate this again in the ViClient??
Thanks
Use the Edit Settings or Edit Annotations option on the Summary page in the VI Client / vSphere Client to remove the Custom Field.
No doubt the $CFM.Field object also has a Remove method to script this.
Thanks for your Help.
Great Work.
Can i put all Informations in the Custom Fields that i can request over the Powershell?? For example CPU Ready??
Anything!
Nice
Thanks a lot