Category Archives: PowerCLI 4.0

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)

Find LUN ID’s in VMware with Powershell

Determining the LUN ID for a specific LUN in your VMware Infrastructure used to be simple. It was listed as one of the properties of the datastore you selected in the VI Client. Nowadays, more often than not, I dont see the LUN ID in the vSphere Client. Instead, I see some sort of identifier like “EMC Fibre Channel Disk (sym.12673548127)”.
Even more unfortunate is the fact that all my scripts show the same identifier, where they used to show the LUN ID. So I decided to create a script that can translate the identifier (sometimes referred to as Canonical Name) back to a LUN ID.
By the way: in the vSphere Client, you can still find the LUN ID by opening the datastore’s properties window and clicking Manage Paths. Or you could write down the canonical name, switch to the devices view and look up the device there. That is essentially what my script does for you.
Here we go:
Continue reading

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