15
Sep/09
10

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

  • Share/Bookmark
23
Mar/09
3

Translate Vml to LUN Path with Powershell

While checking the vmkernel logs on our VMware ESX Servers today, I ran into some errors referencing luns using a vml string. It looks something like this: vml.827149017315617. I would like to know what lun this error is referencing, but I prefer the LUN Path notation, e.g.: vmhba1:2:137. So I wrote this Powershell VI Toolkit function that can translate the vml into the lun path:

# Function: Translate a VML (e.g.: vml.9364839746917650) to a Lun Path (e.g.: vmhba1:2:137)
 
function Translate-VmlToLunPath
	{
	param(
	[string]$VMHostName,
	[string]$Vml
	)
	Return (Get-VMHost $VMhostName | Get-ScsiLun | Where {$_.ConsoleDeviceName -match $Vml}).CanonicalName
	}

Feed it a host name and a vml string and it will return the lun path. Here’s an example script that uses this function when looking for LUNs with SCSI Reservation Errors:

# Example use in a script: Get LUNs with SCSI Reservation Conflicts
 
$VIServerName = "myVIServer"
$NumLines = 1000
 
$VC = Connect-VIServer $VIServerName
 
ForEach ($VMHost in Get-VMHost)
	{
	ForEach ($Log in ($VMHost | Get-Log -Key vmkernel -NumLines $NumLines))
		{
		$MatchedEntries = $Log.Entries | Where {$_ -match "reservation" -and $_ -match "vml.\d*"}
		ForEach ($VmlId in $matches.values)
			{
			$myObj = "" | Select VMHost, ErrorLun
			$myObj.VMHost = $VMHost.Name
			$myObj.ErrorLun = Translate-VmlToLunPath -VMHostName $VMHost.Name -Vml $VmlId
			Return $myObj
			}
		}
	}
 
Disconnect-VIServer -Confirm:$False

Enjoy!
Hugo

  • Share/Bookmark