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

//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js

(adsbygoogle = window.adsbygoogle || []).push({});

Navy put the option on hold, in a directive saying it was inappropriate “as the Nation is at war.”NAVY: Keenan Reynolds bucks trends as Navy’s freshman leaderThat changed in April. Roll the chilled gingerbread dough out to the thickness of a 1 coin and cut out gingerbread man shapes you will have more than you need. If I don’t roll then more lime comes to the surface and the balls roll a little slower.. And when they invited me to do a cover, I was excited and thrilled. Maintaining good relations with the public is a very important part of marketing.. For people who don’t like beer, cheap nfl jerseys a good Chardonnay wine or Merlot will typically fit the bill. However, immunoglobulin A levels were much higher on people who already had tattoos. In our experiments, expression of Chx10 became apparent at D18 and was maintained until at least D34, when RGCs and wholesale nfl jerseys axons developed after attachment of the EB. 1 from No. Over time, the body becomes immune to the effects of normal insulin levels and pumps out more insulin to do the job a condition called insulin resistance. Chris, always Authentic Jerseys Sale the gentleman, lets the lady go first and shoots her. This is called pass rushing.. Click “I’m looking for someone,” type a name, and Person Finder will provide a list of people. Instead of wholesale football jerseys china up off your heel like you would have a driver, we want it back about a ball length or so; still tilted head over the right leg. Oakley Sunglasses Outlet If this is the case, then ViaSat’s patents may be more valuable than the company’s current network. It’s a lot like fiscal debt: gradually accumulated and a bastard to get rid of. Corporate America has a tendency to vacillate between centralized and decentralized structures, usually flipping from one to the other in reaction to threats and/or disappointing www.cheap-raybans-sunglasses.com results. Offensively Cheap Football Jerseys I suggest the same thing. A state bill that’s been introduced would require professional sports teams to recognize cheerleaders as employees, and to pay them at least minimum wage for their practice time, promotional appearances, as well as their game performances.. For instance .. Leftover beads from a jewelry making class, can be strung or glued onto the doll, and made to look like the doll is wearing jewelry. FIFA World Cup Fourteen top FIFA officials were indicted in May of 2015 for their involvement in a 24 year corruption scheme that saw at least two generations of world soccer’s governing body pocketing millions of dollars in bribes and kickbacks for swaying World Cup bids and marketing commissions.

11 thoughts on “Add Vmx Path to VI Client using Powershell”

  1. Pingback: ben.neise.co.uk » Script to add VMX path as a custom attribute
  2. 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

    1. 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
      For the PowerCLI (former VI Toolkit; Powershell snapins for VMware), it’s my curiosity that brought me the knowledge 😉 The beauty of Powershell is that you can explore and learn about it’s possibilities once you know the basics.
      Hugo

      »crosslinked«

  3. 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. 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

    1. 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.

Leave a Reply