VMworld 2013: PowerCLI Best Practices – A Deep Dive #VSVC4944

My first scheduled session was with Alan Renouf and Luc Dekens, the PowerCLI gurus of VMware and always busy writing scripts and code to make the life of a VMware professional easier.

DSC03006

Alan Renouf and Luc Dekens slicing the PowerCLI cake.

During this session, Alan and Luc were sharing their best practices when using PowerCLI 5.5 Release 1 and PowerShell v3. I basically wrote down a list of these best practices and specifically the ones that come in handy during my activities. Hopefully it will come to good use for others.

  • Use tags (introduced in vSphere 5.1). Tags can be read and written, which was not possible in earlier releases of PowerCLI
  • vDS support. Now you can configure distributed switches
  • Open your VM console using PowerCLI and share the URL with administrators to enable them to directly login to the VM console without logging into vSphere Web Client.
  • vCloud Director 5.5 support
  • Configure ESXi host licenses
  • Make sure when processing data, that you filter the search before filtering the results to speed up the processing of your command or script.
  • Don’t use Write-Host, instead use Write-Verbose or Write-Warning
  • Use venter Alert Actions to automate in a very easy way. Let’s say after deploying a VM, you want to automatically run a script in the VM that installs AntiVirus software, or your CMDB gets updated automatically. There are numerous amount of alerts available in vCenter which you can use out-of-the-box
  • WebCommander, a VMware fling which presents PowerCLI scripts, for any kind of product (View, vCloud Director, vSphere etcetera) in a web-based view available to you, the scripter/programmer and also your users. It’s even possible to give users a direct link to your scripts. Looks like an App store for PowerCLI scripts 🙂
  • Check out PS-Remoting to re-use PowerCLI sessions open to your vCenter Server. This decreases load on your vCenter server and increases the speed of your scripts, since the initial load only happens once, when you open up the session.
WebCommander, a fling by VMware

WebCommander, a fling by VMware

Thanks to Alan and Luc for their session and dose of humour 🙂

Get-vSocket PowerCLI script

I just created a PowerCLI script for exporting all your VMs with vCPU and vSocket count to a CSV file.
You can download the script using the link below.
Script download

#General information
#Date: June 6th, 2013
#Author: Rene Bos
#URL: http://blog.stormdesigns.nl
#Script version: 0.1

#Script summary
#Tested on vCenter Server and ESXi 5.1
#This script will retreive the vCPUs and vSockets for each VM and will export this information in a table-like format to a CSV file.
#You need VMware PowerCLI installed on your machine and an active connection to one or more vCenter servers.

#Disclaimer
#Please use this script at your own risk and test it out in your testlab first before using it in production
#When using my script, please leave the general information in place. And let me know if my scripts needs improvement!

#Customizations
$CsvPath = "C:TempGet-vSocket.csv"

#Variables
$VMs = Get-VM

foreach ($VM in $VMs)
{
Write-Host "Getting information for $VM.name"
$VMview = Get-VM $VM | Get-View

$a = [PSCustomObject]@{
Hostname = $VM.name
NumCPU = $VMview.Config.Hardware.NumCPU
NumCoresPerSocket = $VMview.Config.Hardware.NumCoresPerSocket
}
$a | Export-CSV -Path $CsvPath -Append
}
Write-Host "CSV file is ready"