VMware: A (incomplete) list with some PowerCLI Scripts for vSphere

Hi,

for VMware vSphere a very powerfull Scripting engine is available which is called PowerCLI. PowerCLI based on, the also very powerfull, Microsoft powershell.

The Basics

Get the PowerCLI package from the VMware website and install it on your Windows Computer or on the VMware vCenter Server. Ok, lets start.

Open  PowerCLI Command prompt by clicking “VMware vSphere PowerCLI (32-Bit)” Icon. Note: The 64Bit Version(both the 32Bit and 64Bit versions were installed) did support all commands. For example when you clone a computer by the New-VM Command-Let and specifiy the OSCustomizationSpec parameter the follwing error was thrown.

New-VM -VM "ComputerTemplate" -Location "Windows Clients" -ResourcePool Resources -OSCustomizationSpec "Windows XP Client"  -Datastore esxdatastore0 -Name "COMPUTER1"
New-VM : 26.06.2013 23:37:42    New-VM         is not a valid Win32 application. (Exception from HRESULT: 0x800700C1)

However start the 32Bit Version :-). First you have to connect to your vCenter. If your Windows Account, with which you logged on, have the appropriate rights to connect to vCenter simply call

Connect-VIServer NameOfYourvCenter.domain.com

If logon successeds 2 global object variables are set to which all following commands refers to.

global:DefaultVIServer         NameOfYourvCenter.domain.com
global:DefaultVIServers        {NameOfYourvCenter.domain.com}

Alternatively you can assign the vCenter Object to a variable and append the -Server <VIServer[]> parameter to each command. This is usefull if you have multiple instances of vCenter.

$oSecondVCenter=Connect-VIServer NameOfYourvCenter.domain.com
Set-Vm -VM "OldName" -name "New-Name" -Server $oSecondVCenter

By default, all command are executed synchronous. This means the command prompt returned not until the tasks is finished. To start jobs asynchronus append the -RunAsync parameter to each command line.

Lets start with some example scripts

VM Tools

Update VMware tools for a single host without rebooting a virtual machine

Update-Tools "Windows 8 Client" -NoReboot

Combine this with an other command-let demonstrates the power of PowerCLI and Powershell.
The following “one liner” updates the vmware tools on all Windows Guest which are powered on and have already installed an on old version of the vmware tools.

get-vm|where-object{$_.Guest.State -eq "Running" -and $_.ExtensionData.Guest.ToolsStatus -eq "toolsOld" -and $_.ExtensionData.Guest.GuestFamily -eq "windowsGuest"}|Update-Tools -NoReboot

Host Storage

Get all Storage LUNs of an ESX Host

Get-ScsiLun -VMHost ESXHost.domain.com -CanonicalName *

Get Information of a specific LUN, store in a variable

$oStorLUN=Get-ScsiLun -CanonicalName naa.600000e00d1100003245789294528900 -VmHost ESXHost.domain.com

To Show the properties pipe it to Format-list

Get-ScsiLun -CanonicalName naa.600000e00d1100003245789294528900 -VmHost ESXHost.domain.com|Format-List

Set the MultiPath Policy to Round Robin

Set-ScsiLun -SCSILun $oStorLUN -MultipathPolicy RoundRobin

And another one liner, set the MultipathPolicy for all LUNs of Type “disk”, with current policy “MostRecentlyUsed” and which are not local to MultipathPolicy RoundRobin

Get-ScsiLun -CanonicalName * -VmHost ESXHost.domain.com|where-Object{$_.MultipathPolicy -eq "MostRecentlyUsed" -and $_.LunType -eq "disk" -and $_.IsLocal -eq $false}|Set-ScsiLun -MultipathPolicy RoundRobin

All pathes for a LUN

Get-ScsiLunPath -ScsiLun $oStorLUN

Get all FibreChannel WWN

Get-VMhosthba|Where-Object{$_.Type -eq  "FibreChannel" -and $_.Status -eq "online"}|Select-Object device,NodeWorldWideName,PortWorldWideName,VMHost|%{New-Object PSObject -Property @{device=$_.device;NodeWorldWideName="{0:X0}" -f $_.NodeWorldWideName;PortWorldWideName="{0:X0}" -f $_.PortWorldWideName;VMHost=$_.VMHost;}}

ReScan HBAs

Get-VMHost | Get-VMHostStorage -RescanAllHba

Copy the local file d:\temp\test.txt to folder C:\TEMP of the virtual machine(VMware tools requiered) named VMComputer

Copy-VMGuestFile -Source "d:\temp\test.txt" -Destination "C:\TEMP" -VM "VMComputer" -LocalToGuest

To reverse the operation use GuestToLocal instead of LocalToGuest

Guests Network(VMware tools on the virtual machine requiered)
Get network configuration of all interfaces at virtual machine VMComputer

Get-VMGuestNetworkInterface -VM "VMComputer"

Network
Get Status of Host Firewall

Get-VMHostFirewallException -VMHost ESXHost.domain.com

Get a specific Firewall Exception

Get-VMHostFirewallException -VMHost ESXHost.domain.com -Name SSH-Client

To be continued….early:-)

Michael

Leave a Reply