vmworld 2013: powercli best practices - a deep dive
DESCRIPTION
VMworld 2013 Luc Dekens, Eurocontrol Alan Renouf, VMware Learn more about VMworld and register at http://www.vmworld.com/index.jspa?src=socmed-vmworld-slideshareTRANSCRIPT
PowerCLI Best Practices - A Deep Dive
Luc Dekens, Eurocontrol
Alan Renouf, VMware
VSVC4944
#VSVC4944
2
3
Who Are We?
Alan Renouf –
Sr. Technical Marketing Architect, VMware
• Automation: PowerCLI
• vExpert
• VMware Blog: http://blogs.vmware.com/vipowershell/
• Personal Blog: http://virtu-al.net/
• Twitter: @alanrenouf
Luc Dekens –
Systems Engineer, Eurocontrol Maastricht
• Personal Blog: http://lucd.info/
• vExpert & PowerShell MVP
• VMTN account: LucD
• Twitter: @Lucd22
4
Introduction I hope the coffee has kicked in…
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
Common PowerCLI Questions
The PowerCLI Community is huge!
This session will make your life easier
(and also full of famous people)
7
Common PowerCLI Questions
The PowerCLI Community is huge!
This session will make you an expert
8
Community Question
I sense a new
force in the galaxy,
what is this PowerCLI ?
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
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}}
11
Community Question
I already know
most things…
So what's new in
PowerCLI 5.5 R1?
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
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
14
Community Question
Quit your
jibber jabber, how can I apply
some PowerShell best practices
to PowerCLI, fool!
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
16
Community Question
How do I run these scripts?
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)
18
Launching the Code
DEMO
19
Community Question
Why is PowerCLI so slow?
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
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
22
Community Question
How do I export my
information?
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
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)
25
Advanced Reporting
DEMO
26
Community Question
How do the newer
versions of PowerShell
make a difference?
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
28
CIM Demo
DEMO
29
CIM/SMASH
See VMware CIM SMASH/Server Management API for ESXi
Documentation
Result in PNG/PDF/…
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
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
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)
33
PS Remoting III - Schematic
SIW
Session 3
Session 2
Session 1
Server 2012 Core
1
New-PSSession
Disconnect-PSSession
Invoke-Command # PowerCLI environment
2
Get-PSSession
Connect-PSSession
Enter-PSSession
Invoke-Command
34
PS Remoting IV - Gotchas
Set up PowerCLI in the PSSession
Pre W2K12 – mind the available memory
35
SIW Demo
DEMO
36
Comparison – Classic vs SIW
Classic SIW
Connect-VIServer 14.6 0
Get-
Datacenter(1st)
5.3 0.1
Get-Datacenter 0.1 0.1
37
Community Question
I'm all about Events,
but how can I use them?
38
Events - What
Used to record significant state changes of managed entities
Stored in the vCenter DB
Retention!
39
Events – Objects - Inheritance
Event VmEvent VmDeployed
Event
Template srcTemplate CreatedTime
…
vm
EventEx
ExtendedEvent
Arguments
eventTypeId
…
General
Event
message
data
eventTypeId
…
40
Events – Simple Retrieval
Get-VIEvent
Attention points
• MaxSamples: default is 100
• Entity: is not recursive
$esx = Get-VMHost -Name ESX1
$start = (Get-Date).AddDays((-1)
Get-VIEvent -MaxSamples ([int]::MaxValue) -Entity $esx -Start $start
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]}
42
Events Demo I
DEMO
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 !
44
Events Demo II
DEMO
45
Events – Which One ?
The ‘regular’ events are in the SDK Reference
Other, go via EventManager
Or, use the Event-O-Matic !
46
Events Demo III
DEMO
47
Community Question
This stuff is MAGIC
How do I take it further?
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
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
50
Community Question
Where do I go now?
51
Where Do I Go Now
Community
Official PowerCLI Blog
External PowerCLI Blogs
New Developer Network
Books
Trainsignal Videos
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
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”
54
VMware vSphere™ PowerCLI
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
Other VMware Activities Related to This Session
HOL:
HOL-SDC-1307
vCloud Automation Solutions
Group Discussions:
VSVC1005-GD
PowerCLI and Automation with Alan Renouf
VSVC4944
THANK YOU
PowerCLI Best Practices - A Deep Dive
Luc Dekens, Eurocontrol
Alan Renouf, VMware
VSVC4944
#VSVC4944