VMworld 2013
Luc Dekens, Eurocontrol
Alan Renouf, VMware
Learn more about VMworld and register at http://www.vmworld.com/index.jspa?src=socmed-vmworld-slideshare
5. 5
Background
We’re back for the 3rd time in a row
• You just cant get enough of PowerCLI!
No 1 unique solution for a task/problem.
Some are better
• More performant
• More flexible
This session shows a number of these, so you don’t have to find
them out yourselves
6. 6
Common PowerCLI Questions
The PowerCLI Community is huge!
This session will make your life easier
(and also full of famous people)
9. 9
PowerCLI Overview
Why PowerCLI?
• PowerShell is universal, easy to use and works with objects
Why universal?
• The standard on Windows platforms
• Reuse your PowerShell knowledge in different applications
Easy to use?
• The verb-noun construct in plain English
• Help is available
• Start with minimal set of cmdlets, and grow
What’s with these objects?
• Think of it as unit that has data and functions
• Objects flow through the pipeline to the next cmdlet
10. 10
PowerCLI Overview – Simple Examples
Simple to read/write
New-VM –Name VM1 –VMHost MyEsx
Easy to extend for big(ger) environments
1..10 | Foreach {New-VM –Name VM$_ –VMHost MyEsx}
Clear, understandable minimal code
Get-Datastore | Select Name,Type,FileSystemVersion
Access to API covers what is not (yet) there
Get-VMHost |
Select Name,
@{N=“Reboot”;E={$_.ExtensionData.Summary.RebootRequired}}
12. 12
PowerCLI Snap-in’s
Core
• Managing vSphere
Image Builder
• Working with Image Profiles
Auto Deploy
• Deploying ESXi via PXE
License
• Working with vSphere Licensing
VDS
• Working with vSphere Distributed Switch
Cloud
• vCloud Director Providers
Tenant
• vCloud Director Tenants
Core
Image
Builder
Auto
Deploy
License
Cloud
Tenant
VDS
13. 13
PowerCLI 5.5 R1 - Overview
New Cmdlets to manage vSphere Tags
Manage Tags in the vSphere environment
Assign Tags to vSphere Objects
Remove Tag assignments from vSphere Objects
VDS
Easy VDS Migration
Manage Private VLANs
Manage VDS Policies
Manage VDS Ports
VM Console
Open a console for vSphere/vCloud VM
Open multiple consoles at once
Output the URL to open VM Consoles
vCloud Director 5.5 Support
Support for vCD 5.5
Host Licensing
Assign Host Licensing
15. 15
Best Practices
Store results in variables
• Speed
• Re-use
• Filter on the variables
Use Get-Help and the Examples –ShowWindow
Break things down into functions – Don’t forget splatting
Try/Catch/Throw
Filter on the left
• Get-Event | Sort Date | Where { $_.Something –eq “5” }
• Get-Event | Where { $_.Something –eq “5” } | Sort Date
Steal from the best (Lucd.info) and write the rest
Do not use Write-Host as output, only for output to console
• Use Write-Verbose or Write-Warning
17. 17
What Is the Best Way to Run My Script?
PS1 file / dot source / functions - what does this mean?
Console (PowerCLI Prompt) / Editor
Scheduled Task in Windows (add-pssnapin
VMware.vimautomation*)
vCenter Alert action
• VM Creation – Secure a VM
• Host added to vCenter
Robust PS Sessions (See demo later)
WebCommander (Future VMware Fling)
20. 20
Why is PowerCLI so Slow?
Why is PowerCLI slow to launch?
• Load environment
• PowerShell v1, v2, v3 – Caching
How can I make it even faster?
• Remote session
• Run the Connect-VIServer
• Run a cmdlet
• Disconnect/Reconnect from the session (see demo later)
Why is PowerCLI slower than a direct API call or other SDK?
• Abstracted Object
• Easier to read code
• Easier to work with / Pipeline Results
• Formulated objects
21. 21
Why is PowerCLI so Slow?
Making results faster
Finding the properties you need
• ExtensionData
• New-VIProperty
• Use the Property parameter with Get-View
Creating a new object
• What to read in the SDK
• How to create it
• Creating the bare frame and filling in what's needed
Onyx
• How it is helpful
• How it is not helpful
23. 23
Advanced Reporting
CSV/TXT/XML
• Use whatever is relevant and saves time
• Think about how you can use the information later
Export-XLS / Word
• Export-XLS is a function available on LucD.Info which exports straight to Excel
• Including multiple sheets and Graphs
vCheck
• Community driven script to give you the information you need
• Plugins to add your own checks or remove others for speed
• Change the Style easily
• FREE
• Get Involved!
24. 24
Performance: Advanced, and Fun, Reporting
The performance data is there, use it!
How to find the available counters?
VMware Tools gives access to guest OS metrics
Data where you don’t expect it, like for example through CIM
And a picture still says more than 1K words
• Replace your CSV files with nice graphs
• Several options available (XLS, MS Chart…)
• Combining metrics in a graph will bring a point across
Mapping of vDisks to OS Partitions (The holy grail)
27. 27
Use the PowerShell v3 Features
Use the new CIM cmdlets
• A wealth of information
• Find out what is there (CIM classes)
Robust sessions
• See the SIW demo later on
Calling SOAP/REST methods became a lot easier
Ordered objects
29. 29
CIM/SMASH
See VMware CIM SMASH/Server Management API for ESXi
Documentation
Result in PNG/PDF/…
30. 30
Server In Waiting
PowerCLI is great !
…but do some of these sound familiar ?
• Slow Connect-VIServer
• Slow first cmdlet
• Aligning PowerCLI snapin versions
• Exceptions in big environments
We did, and we were fed up with those
31. 31
PS Remoting I
Use PS Remoting with robust sessions !
A “work horse” box
Lots of CPU power & memory (if needed)
Start a remote session
• Load PowerCLI
• Connect-VIServer
• Execute any PowerCLI cmdlet (JIT)
• Disconnect the remote PSSession
32. 32
PS Remoting II
Discover with Get-PSSession
Use Connect-PSSession !
• Connects to robust session
• PowerCLI environment is ready
Some drawbacks
• Reconnect same account
• Sessions tend to get bloated (leaks)
• Some GUIs have a problem (PSP, PowerGUI)
41. 41
Events – Filtering I
Get-VIEvent has limited filters
Use a Where-clause on all returned objects
• Filtering done on your machine !
$esx = Get-VMHost -Name ESX1
$start = (Get-Date).AddDays((-1)
Get-VIEvent -MaxSamples ([int]::MaxValue) -Entity $esx -Start $start |
Where {$_ -is [VMware.Vim.VmDeployed]}
43. 43
Events – Filtering II
Use EventHistoryCollector
• Filtering on vSphere Server
• More filtering options
$esx = Get-VMHost -Name ESX1
$start = (Get-Date).AddDays((-1)
Get-VIEventPlus -Entity $esx -Start $start -EventType “VmDeployed”
Up to 80 times faster !
48. 48
Integration Next Steps
PowerShell has many Snapins/Modules and doesn’t care what the
technology is
• Microsoft
• CIM Cmdlets / Active Directory Cmdlets / Windows Cmdlets
• 3rd Party
• NetApp / PernixData
Rest API
• Invoke-RestMethod
• Invoke-Webrequest
Read from files
• XML
• CSV
• TXT
49. 49
Further Automation of your Day to Day Tasks
Do you do the same thing every day?
• Snapshot a VM to make sure its ok
Do you love documentation?
• PowerGUI Export / PowerGUI Reports / Export to Word / Export-XLS
• CIM Demo
Module for vShield
• REST API
Other VMware products with Automation (Automation Triangle)
Built-in with policies 1st choice
Orchestration/workflows integration
Scripts quick solutions
Andy Troup blog article: http://url.vmware.com/j2g5
51. 51
Where Do I Go Now
Community
Official PowerCLI Blog
External PowerCLI Blogs
New Developer Network
Books
Trainsignal Videos
52. 52
Take Aways
1. PowerCLI, a quick and easy tool for automating your “vTasks”
2. Take PowerCLI and Automation to the next level
3. Automating the components you didn’t think were possible
4. Combine everything you have learned
53. 53
PowerCLI Summary
Additional Resources and references
PowerCLI Blog - http://blogs.vmware.com/vipowershell/
PowerCLI Download - http://vmware.com/go/PowerCLI
PowerCLI Documentation - http://vmware.com/support/developer/PowerCLI/
PowerShell Performance - http://powershell.com/cs/blogs/donjones/archive/2012/05/07/powershell-
performance-it-s-the-dlr.aspx
Detailed PowerCLI cmdlet reference with examples -
http://www.vmware.com/support/developer/PowerCLI/PowerCLI501/html/index.html
PowerCLI Community -
http://communities.vmware.com/community/vmtn/server/vsphere/automationtools/powercli?view=discussions
vSphere Automation Blog - http://blogs.vmware.com/vsphere/automation/
Luc Dekens Blog – http://Lucd.info
Alan Renoufs Blog – http://virtu-al.net
Google ”PowerCLI”
55. 55
What Else Do You Want from PowerCLI ?
Before you ask,
no I don’t know who this is!
@alanrenouf and @LucD22
#VSVC4944
Q&A
Winners will be tweeted
back by me during VMworld
Tweet a comment about this session to win!
56. 56
Other VMware Activities Related to This Session
HOL:
HOL-SDC-1307
vCloud Automation Solutions
Group Discussions:
VSVC1005-GD
PowerCLI and Automation with Alan Renouf