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

Tagged with:
 

11 Responses to Add Vmx Path to VI Client using Powershell

  1. David says:

    Hi Hugo,

    Excellent script, will test it tomorrow first thing tomorrow.
    Wish I had your PS knowledge!

    Gr,

    David

  2. David says:

    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

  3. David says:

    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

  4. David says:

    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

  5. Wolfi says:

    Very Nice Script. ;-)

    But, how can i deactivate this again in the ViClient??

    Thanks

    • admin says:

      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.

  6. Wolfi says:

    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??

  7. Wolfi says:

    Nice ;-) Thanks a lot

Leave a Reply