Automate Any Size vSphere Environment with vSphere PowerCLI
VMware vSphere PowerCLI is a command line interface for managing and automating any size vSphere environment, allowing you to perform the same operations as the vSphere Client vSphere PowerCLI ships with over 200 ready-to-use commands (commandlets) providing Windows Administrators the ability to create simple, and robust scripts to automate tasks such as, but not limited to, Network, Storage, Virtual Machine, and Guest OS Operations.
Code Samples
Below are some examples of how easy it is to read and customize your PowerCLI scripts. Please refer to your PowerCLI documentation for complete installation and set up instructions. NOTE: All scripts samples assume you have authenticated and connected to your servers, e.g.
connect-viserver –server "MyvCenter.MyDomain.Local" -user Administrator -password pa$$w0rd
ESX and ESXi Server Management / Automation
Server set up and management can be time intensive without automation. If you have to perform the same management tasks more than 3 times per week you should consider automating your tasks.
Here are just a few examples of how PowerCLI can help with managing your hosts.
Rescanning your Host Bus Adaptor (HBA) for all hosts
Get-VMHost | Get-VMHostStorage -RescanAllHBA
Creating Virtual Switches and PortGroups for a single host
Get-VMHost "MyHost.MyDomain.Local" | New-VirtualSwitch -name "MyNewSwitch" -nic vmnic1
Get-VMHost "MyHost.MyDomain.Local" | Get-VirtualSwitch -name "MyNewSwitch" | New-VirtualPortGroup –Name "MyNewPortGroup" -vlanid 100
Configuring NTP on your ESX and ESXi Servers and setting the Time Zone
Add-VMHostNtpServer –VMHost "MyHost.MyDomain.Local" –NtpServer "ntp.MyDomain.Local"
Set-VMHost –VMHost "MyHost.MyDomain.Local" –TimeZone "UTC"
Reporting
Reporting is critical to management of any size environment, since it will allow your team to measure trends and provide internal charge back within your organization.
This script will create a tabular report of your Virtual Machines, including their number of vCPU’s, Memory, Memory reservation and the number of virtual disks:
New-VIProperty -Name MemReservation -ObjectType VirtualMachine `
-Value {
param($vm)
$vm.ExtensionData.Summary.Config.memoryReservation
} -Force
New-VIProperty -Name NumVirtualDisks -ObjectType VirtualMachine `
-Value {
param($vm)
$vm.ExtensionData.Summary.Config.NumVirtualDisks
} -Force
Get-VM | Select-Object Name, MemoryMB , NumCpu, MemReservation, NumVirtualDisks | Format-Table
Virtual Machine Management
VMware vSphere PowerCLI can help increase your organization’s Virtual Machine-to-Administrator ratio by automating common tasks and letting your team focus on strategic projects.
These are just a few examples of how PowerCLI can help with managing your Virtual Machine lifecycle.
Move all VMs from an old Port Group to a new Port Group
$OldPortGroup = "OldLAN"
$NewPortGroup = "NewLAN"
Get-VM | Get-NetworkAdapter | Where {$_.NetworkName –eq $OldPortGroup } | Set-NetworkAdapter –NetworkName $NewPortGroup
Set VM power-on options, such as configuring tools to automatically upgrade
$vm = Get-VM VMName | Get-View
$vmConfigSpec = New-Object VMware.Vim.VirtualMachineConfigSpec
$vmConfigSpec.Tools = New-Object VMware.Vim.ToolsConfigInfo
$vmConfigSpec.Tools.ToolsUpgradePolicy = "UpgradeAtPowerCycle"
$vm.ReconfigVM($vmConfigSpec)
Set resources, such as limits and reservations, on a VM
Get-VM VMName | Get-VMResourceConfiguration | Set-VMResourceConfiguration –CPUSharesLevel "High"
Determine the version of VMware Tools my VMs are using?
New-VIProperty -Name ToolsVersion -ObjectType VirtualMachine `
-ValueFromExtensionProperty ‘config.tools.ToolsVersion’ `
-Force
Get-VM | Select-Object Name, ToolsVersion
