Você está na página 1de 48

vSphere PowerCLI: The best tool for automating vSphere

Alton Yu, Technologist, ISV Alliances

This presentation may contain third party information regarding products that are not owned or
controlled by VMware. VMware has no control over, and assumes no responsibility for, the content,
pricing, privacy policies, or practices of any third party. VMware does not endorse or make any
representations about third party information presented, nor does the inclusion of any third party
presentation imply such an endorsement
© 2009 VMware Inc. All rights reserved
Introduction.
 About me.
• Technical Alliance Manager – Infrastructure

http://vmware.com/go/powercli
The Purpose of PowerCLI.
 Virtualization leads to explosive growth in the number of assets
(network, app, etc) to manage.
 PowerCLI is a tool for automating all aspects of vSphere
management and administration.
 But PowerCLI is different because it’s purpose-built for use by
administrators, not developers.
 Power Shell allows non-programmers automate.

http://vmware.com/go/powercli
You need automation to survive the virtual world.
 Virtual assets are extremely easy to create.
 Some people call it “virtual sprawl”.
 But virtualization lets us do so much more – more complexity is
inevitable.
 Tools and skill sets must be upgraded to match this rise in
complexity.

http://vmware.com/go/powercli
How to get the most out of PowerCLI.
 Remember that PowerCLI can automate anything in vSphere.
• Think about how these possibilities can change the nature of the things you
do every day.
 Focus on increasing your productivity.
• Stop doing things manually.
• Start focusing on building automation tools
you can use again and again.

http://vmware.com/go/powercli
The most important PowerCLI resource

TI
PS
 The PowerCLI Community.

 http://vmware.com/go/powercli
 More than 500 scripts.
 Search a huge and growing knowledgebase.

 Ask questions and get answers.


http://vmware.com/go/powercli
3 Action items
 Install Power Shell and the PowerCLI snap-in
 Bookmark PowerCLI resources
• www.vmware.com/go/powercli
• www.virtu-al.net
• www.powergui.org
 Think scripting first.

http://vmware.com/go/powercli
Getting to know you.
 Where are you wasting your time?
• Provisioning?
• Change management?
• Troubleshooting?
 What business pressures do you face?
• Supporting a wider variety of applications?
• Improving availability?
• Other things?

http://vmware.com/go/powercli
4 Things about PowerShell
 The cmdlets.
 The pipeline.
 The platforms.
 The products.

http://vmware.com/go/powercli
PowerShell: The cmdlets.
 PowerShell provides almost 200 commands, called cmdlets,
natively.
 Close to 1,000 more commands are available through various
“snap-ins” (e.g. PowerCLI).
 Within PowerShell, type “Get-Command” to see a listing of
commands.

http://vmware.com/go/powercli
PowerCLI: The Cmdlets.
 229 cmdlets in version 4.0 U1.
 Built on the vSphere Web Services API.
 Two ways to automate:
• Simplified cmdlet approach.
• Advanced approach using Get-View (API).
 More coming every 6 months or so.
• PowerCLI release philosophy is to release twice a year and be very
responsive to user feedback.
 Long-term goal to make Get-View
unnecessary.
http://vmware.com/go/powercli
PowerCLI: Cmdlet Tetris.
 PowerShell commands follow the pattern Verb-Noun or Verb-
Object.
 For a given Object you will commonly see these four commands:
• New-Object, Get-Object, Set-Object, Remove-Object
 Look for this pattern to manage the full lifecycle of a virtual object.

http://vmware.com/go/powercli
PowerShell: The Pipeline.
 The pipeline is how you compose commands in PowerShell.
 Outputs of one command are automatically made inputs of the
next, based on type.
 Example:
• Get-Process notepad | Stop-Process
• Get-VM myvm | Start-VM

http://vmware.com/go/powercli
PowerShell: The Platforms.
 PowerShell runs on all versions of Windows since XP and up.
 Requires .NET 2.0.
 PowerShell is installed by default in Windows 7 and Windows 2008
R2.
 All future versions of Windows will pre-install it.

http://vmware.com/go/powercli
PowerShell: The Products.
 PowerShell support is required for all Microsoft server
applications.
• Exchange.
• Sharepoint.
• IIS.
• Many more.
• And, of course, VMware.

http://vmware.com/go/powercli
PowerCLI: The Products.
 vCenter:
• 2.5, 4.0, 4.1
 ESX / ESXi
• 3.0, 3.5, 4.0, 4.1
 VMware Update Manager
 VMware View

http://vmware.com/go/powercli
PowerShell tools you should use.
 VESI (Virtualization EcoShell) PowerGUI.
• VMware Community PowerPack (Virtu-Al.Net vSphere PowerPack).
 PowerShell Plus.
 PowerShell WMI Explorer.

http://vmware.com/go/powercli
Where do you feel the most pain?
 Configuring and maintaining ESX hosts?
 VMs?
 Reporting?
 Performance analysis?
 Troubleshooting?

http://vmware.com/go/powercli
PowerCLI: A complete automation solution.

VM Network
60 cmdlets 25 cmdlets

Create and modify VMs. Create virtual switches.


Move
RunCreate
Find
VMs
scripts Count
old
virtual
Enable
inside Snapshot
tosnapshots
Configure
new
Run
VMs. Storage
VMs
Rescan
switches
VMotion
networks
groups
scripts VMotion
storage
multipath
onof
within
Optimize allVMs
VMsVMs
throughput.

01: $lastWeek
Get-VMHost=| (Get-Date).addWeeks(-1)
| Measure-Object
newEsx1
-Datastore
Get-ResourcePool
Get-VM Get-CDDrive
myhost
Win01 | || Get-VM
Get-ScsiLun
datastore1||
| Servers"
"Database
| |
02: Get-VM ||Get-Snapshot
Get-NetworkAdapter
Set-ScsiLun
Move-VM |-RescanAllHba
|-Name
Get-VM -Datastore
Set-CDDrive-MultipathPolicy
Storage
New-VirtualSwitch
Get-VMHostStorage
Invoke-VMScript datastore2
RoundRobin
-Connected:$false
"New
-ScriptText Switch” `
Get-HotFix Reporting
03: Where
where
-Nic{
Get-VM { $_.Created –lt
$_.NetworkName
New-Snapshot
|vmnic2,vmnic3
Get-FloppyDrive |$lastWeek
-eq
-Name "Weekly }
"Internal"}
Snapshot" |
20 cmdlets 59 cmdlets
04: Set-NetworkAdapter
Set-FloppyDrive-NetworkName External
-Connected:$false
Manage datastores. Track your environment.
Optimize performance. Detect problems early.

This presentation may contain third party information regarding products that are not owned or controlled by VMware. VMware has
no control over, and assumes no responsibility for, the content, pricing, privacy policies, or practices of any third party. VMware
19 does not endorse or make any representations about third party information presented, nor does the inclusion of any third party
presentation imply such an endorsement
Get-TheMost out of “Help”

TI
PS
 Help is PowerShell’s built-in help system.
 Everything you need to know is documented in the cmdlet itself.
 Supports wildcards “Help *VM*”
 All PowerCLI cmdlets have examples.
• “Help <cmdlet> -full”
• “Help <cmdlet> -detailed”
• “Help <cmdlet> -examples”

http://vmware.com/go/powercli
Get-TheMost out of “Get” cmdlets.

TI
PS
 Learn how to string Get-Cmdlets together.
• Example: Get-Cluster X | Get-VMHost | Get-VM to select all VMs in a cluster.
 PowerCLI cmdlets are designed to navigate similar to the vSphere
Client hierarchy.
• But PowerCLI can do things vSphere Client can’t.
 You can learn this from the help but easier
to just try it out.

http://vmware.com/go/powercli
3 ways to connect.

TI
PS
 Use your username and password.
• Warning: don’t do this except interactively.
 Use SSPI / Active Directory if you have it.
 Use Import-PSCredential if you don’t.
• See http://poshcode.org/501
 Key cmdlets:
Connect-VIServer
Get-VMHost
Get-VM

http://vmware.com/go/powercli
PowerCLI supports multiple connections.

TI
PS
 Starting in PowerCLI 4.0 U1 you can connect to multiple vCenter
or ESX servers at the same time.
 Now you can automate across multiple vCenters or across
multiple ESX even if vCenter is unavailable.

http://vmware.com/go/powercli
Large-scale provisioning.

V
M
S
 Whether you use templates or not, PowerCLI makes it easy to
provision.
 Even if you use static IP addresses PowerCLI can automate it.
 Large-scale provisioning and Import-CSV were virtually made for
each other.

 New-VM
 Get-Template
 Get-OSCustomizationSpec

http://vmware.com/go/powercli
VM Updates.

V
M
S
 Large-scale updates are easy with Set-VM.
 Master Get-* and Where cmdlets to target exactly the VMs you
want.
 If you’re worried about the impact, use –whatif.

 Get-VM
 Set-VM

http://vmware.com/go/powercli
Managing ESX with PowerCLI.

ES
X
 Provisioning (Host profiles or scripted)
 Change / Update.
• Storage.
• Network.
• Security, etc.
 Managing power and connection state.

http://vmware.com/go/powercli
2 ways to provision your ESX hosts.

ES
X
 Host Profiles.
• Provision one server just the way you like it.
• “Stamp out” the configuration to other servers.
 Home-grown scripts (covered later).
• Run a custom script to do complete configuration of a server.

http://vmware.com/go/powercli
3 Takeaways: Host Profiles.

ES
X
 Host Profiles simplify the process of creating identically
configured ESX hosts.
 The purpose of profiles is avoiding cost and complexity “I don’t
have to worry about a million things.”
 Host Profiles are a strategic direction for VMware and will continue
to improve.

 Get-VMHostProfile
 Apply-VMHostProfile
 Test-VMHostProfileCompliance
http://vmware.com/go/powercli
ST
Save money with thin provisioning.

O
RA
G
E
 Allocate space as it is used rather than all at once.
 Huge space savings at the cost of some performance.
 You can provision new VMs thin or convert existing with
PowerCLI.

 New-VM
 New-Harddisk
 Set-Harddisk

http://vmware.com/go/powercli
N
ET
Managing Networking with PowerCLI.

W
O
RK
 Adding or configuring VMKernel.
 Moving VMs between virtual switches.
 Configuring virtual switch policies.

http://vmware.com/go/powercli
N
ET
3 Takeaways: Virtual Switches.

W
O
RK
 Creating consistent virtual switches is key for many VMware
features such as DRS.
 Creating switches manually is insane, especially considering the
simplicity of PowerCLI.
 You can also set policy items like failover and load balancing.

 New-VirtualSwitch
 Get-VirtualSwitch

http://vmware.com/go/powercli
RE
Reporting with PowerCLI.

PO
RT
S
 Three major categories of PowerCLI reports.
• Inventory-based reporting.
• Statistic-based reporting (i.e. performance)
• Event-based reporting.

http://vmware.com/go/powercli
RE
Reporting with PowerCLI.

PO
RT
S
 What reporting tools do you use today? Do you think they are
good?
 Do you use reports more for planning or more for reacting to
problems?
 Where do you feel you are “in the dark”?

http://vmware.com/go/powercli
This report will make your manager love

RE
PO
you (in a completely legal way).

RT
S
 vCheck Daily Report.
 Reports:
• Numbers of objects in inventory (VMs, hosts, etc)
• Datastores running out of space.
• Snapshots over X days old.
• Disconnected hosts.
• Lots more!
 All emailed to you every morning!

http://www.virtu-al.net/ http://vmware.com/go/powercli
RE
Inventory-based reporting.

PO
RT
S
 Relies on Get-* cmdlets as a starting point.
 Generally combined with Export-CSV or ConvertTo-Html.

http://vmware.com/go/powercli
RE
Statistic-based reporting.

PO
RT
S
 Relies on Get-Stat as a starting point.
 Commonly you will rank the results.
 This can feed into other script logic or be exported like an
inventory report would be exported.

http://vmware.com/go/powercli
RE
3 Takeaways: Reporting with Statistics.

PO
RT
S
 Querying statistics help you identify and solve performance
problems.
 Stats on vCenter are determined by stat level. The higher the level
the more stats you have.
 If you query ESX directly all stats are available but for short
periods (5 mins on 3.5, 1hr on 4.0)

http://vmware.com/go/powercli
RE
You can get your VM’s log file too.

PO
RT
S
 Get to know vmstore:
 Use Copy-DatastoreItem to bridge the gap between local and
remote.
 Retaining log files is critical for audit and compliance.
 Log files are rotated (a.k.a. deleted) regularly.
 The number of log files retained is customizable through
PowerCLI.

 Copy-DatastoreItem
http://vmware.com/go/powercli
VMware Support and Training for PowerCLI

 VMware vSphere Automation


• 2 day instructor led course, 40% Lecture, 60% hands on lab
• Available worldwide, use PSO credits
• http://vmware.com/go/vsphereautomation

 VMware Scripting and SDK Support


• Dedicated help when developing scripts / applications
• Expert API engineers happy to help
• http://vmware.com/go/sdksupport
The “PowerCLI Bible”

TI
PS
http://vmware.com/go/powercli
Lots of PowerCLI Samples.

TI
PS
http://vmware.com/go/powercli
6 Amazing Blogs to Follow:

TI
PS
Using “Project Onyx”.

A
PI
 Onyx is a code generator.
 Sits between vSphere Client and vCenter.
 Any UI click is automatically turned to code.

http://vmware.com/go/onyx

http://vmware.com/go/powercli
“Project Onyx” Architecture.

A
PI
ESX or vCenter
Why would somebody use it?

• PowerCLI introduces over 200+ “cmdlets”.


• Fast and easy to use.
• But don’t cover entire vSphere API.
• Get-View cmdlet
• Expose the vSphere API
• API scripting is not a trivial task
• Onyx
• Help users create complicated scripts using the vSphere API

This presentation may contain third party information regarding products that are not owned or controlled by VMware. VMware has
no control over, and assumes no responsibility for, the content, pricing, privacy policies, or practices of any third party. VMware
45 does not endorse or make any representations about third party information presented, nor does the inclusion of any third party
presentation imply such an endorsement
3 Takeaways: Onyx.

A
PI
 Onyx makes it simple to understand what calls vSphere Client
makes and when.
 From this you can understand the best practices and tested code
paths VMware uses.
 Warning: You will need to re-factor code generated by Onyx.

http://vmware.com/go/powercli
Get your PowerCLI 4.1 Poster
Download today:
http://vmware.com/go/powercli

http://vmware.com/go/powercli