Tag Archives: PowerCLI

Poor Man’s Virtual Machine Backup with PowerCLI

Are you still performing old-school in-guest VM backups with a backup agent? Not ready to buy a backup product that does complete VM backups through the vStorage APIs? Then you will need to rebuild all your VM’s with the original configuration before you can start restoring your data.

As always: PowerCLI to the rescue! This script will connect to vCenter Servers on your primary site and secondary (recovery) site. Continue reading

»crosslinked«

Calculate vSphere 5 Licenses with Powershell (UPDATED)

VMware has announced the next generation of vSphere yesterday. Besides lots of new features, vSphere 5 also comes with a new licensing structure. For every licensed physical CPU, you get a certain amount of vRAM, which you will be able to allocate to virtual machine. Only the running VM’s will count towards your license limit.
Curious how the vSphere 5 licensing model will impact your license cost? Want to know how many vSphere 5 liceses you will need? The following script calculates exactly how many licenses you need for the different editions and how much overhead you will have left.

UPDATE 04-AUG-2011: vmware has increased vRAM entitlements for all editions of vSphere 5! I have updated the script (shown below and the attached script). The screenshot is still the old one. Please read the new Licensing PDF here for all the details.

NOTE: Please note that I add up all your pCpu’s, ignoring current license types. I also add up all your current vRAM usage (RAM assigned to powered on VM’s). I then show you different scenarios if you purchase license type x for ALL your pCpu’s. If your environment consists of a mix of vSphere editions, check out Alan Renouf’s script to analyse your environment.


Continue reading

Remove vmware memory limits with Powershell

Using Memory Limits in vmware vSphere can cause severe performance issues. The guest operating system assumes it can use all of the allocated memory, but vSphere will make sure the vm cannot use more than the memory limit. It does this by inflating a memory balloon using the balloon driver included with the vmware tools. The performance chart in the vSphere Client will show the virtual machine is ballooning. I never recommend using memory limits in vmware.
The following script will remove all memory limits in your vSphere infrastructure, preventing the problems described above. Continue reading

Set vApp Guest Shutdown

Annoyed by the default setting for VM’s inside a vApp? I was, because when you power down the vApp, the VM’s are powered down instead of being shutdown cleanly. It can be a tedious task to check and correct this setting for all your vApps. This little script solves that problem for you. Enjoy!

$VC = Connect-VIServer "MyvCenter.local"
 
ForEach ($Vapp in Get-VApp)
{	
	$VAppView = $VApp | Get-View
	ForEach ($Entity in $VAppView.VAppConfig.EntityConfig)
	{
		If ($Entity.StopAction -ne "guestShutdown")
		{
		$VAppConfigSpec = New-Object VMware.Vim.VAppConfigSpec
		$EntityConfig = New-Object VMware.Vim.VAppEntityConfigInfo
			$EntityConfig.Key = (Get-View $Entity.Key).MoRef
			$EntityConfig.StopAction = "guestShutdown"
		$VAppConfigSpec.EntityConfig = $EntityConfig
 
		$VAppView.UpdateVAppConfig($VAppConfigSpec)
		}
	}
}
 
Disconnect-VIServer -Confirm:$false

Download script: Set-VAppGuestShutdown (rename to .ps1)

Set VMware Snapshot Location with Powershell

Snapshots are m*th3rfcukers. If you’re not careful, they will mass-murder your vms. Yet they allow you to time-travel! You want to use them, but how to prevent a massacre? Here’s how: relocate the delta files.

When you create a snapshot, the current state of the vm is preserved by leaving the disk files alone. All changes since the moment of creating the snapshot are written to delta files. The delta files are stored in the vm’s working directory. The working directory is – by default – the location where the vmx and other config files reside. If that datastore runs out of free space – especially if it also contains disk files – you’re in a bit of a kerfuffle. Vms not booting or being frozen as if they stared into Medusa’s reptoid eyes.

So you can do two things: reserve overhead in your datastores and stay afraid some overactive snapshot might destroy your environment, or set the working directory of your vms to some big-ass datastore you don’t use for anything else and let the snapshots enjoy themselves. If they fill up the datastore, they only kill all vms with snapshots, not the rest.

But how, you ask? You can edit the vmx files of you vms manually - which requires your vms to be powered down – and add the line: workingDir = “/vmfs/volumes/<insanely long guid you need to somehow find>/”

Or, you run my script and change the working location on the fly:

Continue reading

Create a vSwitch Port Overview with Powershell

WARNING: VMware vmotion does not check wether there are sufficient ports available on the virtual switch on the destination host. Migrating a vm to a host with insufficient ports will cause the vmotion to complete without warnings, yet the virtual NIC will be disconnected! This issue is descripbed in this KB article.
The solution to this problem is to create vSwitches with sufficient ports, obviously. Do you want to know how many ports are currently being used on every vSwitch in your environment? vSphere PowerCLI to the rescue! Try the following script:

Continue reading

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