Providence VMUG
PowerCLIJonathan Frappier, @jfrappier
www.virtxpert.com
Providence VMUG | PowerCLI
• Who am I (aka why you should listen to me)• Why automate?• Why PowerCLI?• PowerCLI 101: Get-AllTHEcmdlets!• Combining PowerCLI cmdlets into scripts• Live demo (because that’s always a good idea)• Next steps challenge• Community resources• Q&A
Providence VMUG | PowerCLI
Who am I?• 15+ Years in technology• VMware Certified Advanced Professional – Data Center
Design (VCAP5-DCD)• EMC Proven Cloud Infrastructure and Services
(EMCCIS)• Community dude – Twitter, blogger, podcaster
@vbrownbag, VMware vExpert 2013-2014, EMC Elect 2014
• Author
• I have struggled with automation, I have struggled with business that didn’t understand the value, I taught those companies why automation was important
Providence VMUG | PowerCLI
Why automate?• NOT a time saver…at first
Providence VMUG | PowerCLI
Why automate?Automation is the path to ITaaS.
Automation enables Standardization, Standardization leads to Security
Providence VMUG | PowerCLI
Why automate?1. Understand business requirements
2. Documentation
3. Standardization
4. Automation
5. Security
Providence VMUG | PowerCLI
Why PowerCLI?
Providence VMUG | PowerCLI
Providence VMUG | PowerCLI
Why PowerCLI?• Free, extremely powerful tool with over 370 cmdlets• Ability to control installation, management and configuration
of vSphere components (vSphere, vCenter, ESXi, Networking, Storage, VMs), vCloud Director, VMware View
• Designed for system administrators, low learning curve• Important tool used for custom ESXi images and Auto
Deploy• Use seamlessly with standard Windows PowerShell
cmdlets/functions• Many community resources• Did I mention free?
Providence VMUG | PowerCLI
Why PowerCLI - Example
• Even basic vSphere tasks can be a very lengthy process
• Creating a new VM requires ~ 33 mouse clicks
• 1 line in PowerCLI can do the same thing as 33 mouse clicks!
Providence VMUG | PowerCLI
Get-Started (not a real cmdlet)• On 64-bit systems, launch PowerShell and PowerShell x86
consoles and run– Set-ExecutionPolicy RemoteSigned
• New- & Set-OSCustomizationSpec cmdlets are 32-bit only• Run PowerShell and PowerCLI as Administrator (even if you are an
administrator)
Providence VMUG | PowerCLI
PowerCLI 101 – Get cmdlets• Allows you to retrieve information (e.g. “get”)• If you remember only 1 Get cmdlet, make it Get-Help Get-*• You use Get-Help to get information about a specific cmdlet: Get-
Help Get-VM (-examples)
Providence VMUG | PowerCLI
PowerCLI 101 – Example - Get-VMHost
Providence VMUG | PowerCLI
PowerCLI 101 – Display Output• Format-Wide, Format-List, Format-Table (Default)
Providence VMUG | PowerCLI
PowerCLI 101 – Filtering• Select-Object (select)
– Get-VMHost | Get-VMHostNetworkAdapter | Select-Object Mac
• Where-Object– Get-VMHost | Get-VMHostNetworkAdapter | Where-Object –eq
Mac– -eq, -like, -contains, -notcontains, -gt, -ge, -lt, -le
• Out-GridView– Get-VMHost | Get-VMHostNetworkAdapter | Out-GridView
Providence VMUG | PowerCLI
PowerCLI 101 – Set cmdlets• Allows you to configure settings (e.g. “set”)• If you remember only 1 Get cmdlet, make it Get-Help• Most (All?) Get cmdlets have a corresponding Set cmdlet• In many cases you need to Get something before you can Set
something
Providence VMUG | PowerCLI
PowerCLI 101 – Example
Set-VMHostNetworkAdapter• In many cases you need to Get something before you can Set
something– Get-VMHost host | Get-VMHostNetwork Adapter –VMKernel | Set-
VMHostNetworkAdapter parameters
Providence VMUG | PowerCLI
PowerCLI 101-whatif• Allows you to see the result of running a cmdlet or script• Useful for testing
Providence VMUG | PowerCLI
PowerCLI 101Add-, New-, Remove- cmdlets• Add-VMHost – Adds a host to be managed by vCenter• Add-VMHostNtpServer – Adds the specified NTP server to ESXi• New-Cluster – Creates a new cluster• New-VM – Creates a new VM• Remove-Cluster – Removes the cluster• Remove-VM – Removes the VM from vCenter
Providence VMUG | PowerCLI
What is wrong with this command?
Providence VMUG | PowerCLI
Why PowerCLI – Prompt for Input
Instead of:
You can ask for user input:
Providence VMUG | PowerCLI
Why PowerCLI – Read from CSV
Instead of:
You can provide data in a CSV:
Reference post from Sebastian Barylo via www.vmwaremine.com http://goo.gl/cRlbwp
Providence VMUG | PowerCLI
Providence VMUG | PowerCLI
Tips• #Comment #Everything #Hashtag• Use full cmdlets (e.g. Select-Object instead of Select)• Indent, Indent, Indent (This is for you Kanji)
– Indent once after every bracket– De-indent(?) after every closed bracket
• PowerShell console shortcuts:– http://technet.microsoft.com/en-us/magazine/ff678293.aspx
Providence VMUG | PowerCLI
Next steps challenge• Keep PowerCLI open, start with Get-Cmdlets• Review the vSphere Hardening Guide ~100 PowerCLI
examples for both Get- and Set- cmdlets• Find a PowerCLI/PowerShell editor you like: PowerGUI,
PowerShell ISE, PowerShell Plus, NotePad++ and plenty of commercial offerings
• 30/60/90 day service desk reports: What are you repeating (KISS)? Document it, automate it.
• Share your scripts: Blog, GitHub, Twitter
Providence VMUG | PowerCLI
PowerCLI Ninja Challenge?
Providence VMUG | PowerCLI
Community Resources• Twitter: @PowerCLI, @PowerCLIbot, @PowerTip,
@Powerscripting, @AlanRenouf, @JeffHicks, @vNoob• Blogs/Sites:
– http://www.virtu-al.net/ | http://www.virtu-al.net/vcheck– https://www.vmware.com/support/developer/PowerCLI/– http://professionalvmware.com/?s=powercli– http://www.vmware.com/security/hardening-guides
• Book: Learning PowerCLI– https://smile.amazon.com– https://charitywater.org– https://charitywater.org/birthdays
Providence VMUG | PowerCLI
Q&A• Twitter: @jfrappier, @vBrownBag
(hashtag: #vBrownBag)
• G+: +JonathanFrappier
• LinkedIn: /in/jonathanfrappier
• http://virtxpert.com
• http://professionalvmware.com
• Book: VMware vSphere Resource Management Essentials http://amzn.to/1fAWGzk
Top Related