PowerCLI 101
Why Automate
Save Time Reduce Risk Scale
Agenda
Introduction to PowerShell vCenter management Storage management Automating VM lifecycle
PowerShell 101 (Terminology)
Cmdlet (pronounced command-lets) are compiled .NET class that perfom a single action on an object. They are named in the <verb>-<singular noun> format.
Objects: A collection of properties and methods with a unique type name.
Pipeline: Passes whole .NET objects from one cmdlet to another enabling clean powerful syntax.
PowerShell Pipeline
Get-
VM
Where
-Obje
ct
Sta
rt-V
M
Form
at-
Table
Pipeline in Action
Get-VM | Where-Object {$_.PowerState –eq ‘Powered Off’} | Start-VM | Format-Table
PowerShell 101 ( Key Cmdlets)
Get-Command Get-Help Get-PSDrive Get-Member Format-List
Grow Environment!Live Demo
Needs & Environment
Tasks:• Create new datacenter• Add existing Host and VMs to new vCenter
instance.• Create a new cluster• Add new Host to the new cluster
Cmdlets used:• Connect-VIServer• New-Datacenter• Add-VMHost• New-Cluster• Set-Cluster• Move-VMHost
Grow Environment
# Add host to vCenterConnect-VIServer vCenter01.sea-tm.netapp.com
# Create Datacenter$Datacenter = New-Datacenter -Name Bellevue -Location (Get-Folder)
# Add the first hostAdd-VMHost -Name vSphere1.sea-tm.netapp.com -Location $Datacenter ` -Force
# Create new cluster$Cluster = New-Cluster -Name VMworld -Location $Datacenter
# Set the HA/DRS Settings for our clusterSet-Cluster -cluster $Cluster -HAEnabled $True ` -HAIsolationResponse DoNothing -DrsEnabled $True -Confirm:$false
# Move our existing host into the new clusterMove-VMHost -VMHost vSphere1* -Destination $Cluster
# Add an additional host to the clusterAdd-VMHost -Name vSphere2.sea-tm.netapp.com -Location $Cluster ` -Force -RunAsync
ZOMG I Needs Storage?!Live Demo
Needs & Environment
Tasks:• Find and remove any large snapshots.• Provision new storage on Array• Add datastore to hosts• Move VMs to new storage.
Cmdlets used:• Get-VM• Get-Snapshot• Get-VMHost• New-Datastore• Sort-Object• Format-Table• Remove-Snapshot• Set-NaQtree• Set-NaNfsExport
• Move-VM• Get-Datastore• Connect-
NaController• New-NaVol• Set-NaSis• Enable-NaSis• Start-NaSis• Get-NaQtree
ZOMG I Needs Storage?!
# Find any large snapshots.Get-VM|Get-Snapshot|Sort-Object SizeMB -Descending |Format-Table VM,Name,SizeMB
# Delete that sucka!Get-Snapshot -VM SQL01 -Name updates | Remove-Snapshot -RunAsync
# we're still a little tight... Create a new datastore!# Import the NetApp DataONTAP PowerShell ToolkitImport-Module DataONTAP
# Connect to the NetApp ControllerConnect-NaController 10.58.92.213
# Create a new volume, and configure dedupeNew-NaVol -Name vmdata1 -Aggregate aggr1 -SpaceReserve none -Size 10t | Set-NaSis -Schedule auto | Enable-NaSis | Start-NaSis | Get-NaQtree | Set-NaQtree -SecurityStyle unix
# Create the NFS exportSet-NaNfsExport -Persistent -Path /vol/vmdata1 -Root '172.16.3.0/24' ` -ReadWrite '172.16.3.0/24'
# Add datastoreGet-Cluster VMworld | Get-VMHost | New-Datastore -Name vmdata1 -Nfs ` -Path /vol/vmdata1 -NfsHost 172.16.3.213
# Move a VM to make some more room in our oversubscribed datastore.Get-VM Exch* | Move-VM -Datastore (Get-Datastore vmdata1) -RunAsync
VM ProvisioningLive Demo!
Needs & Environment
Tasks:• Clone an existing VM.• Create a new VM.• Convert VM to VM Template.• Deploy multiple VMs from Template.• Delete a VM.
Cmdlets used:• Get-Cluster• Get-Datacenter• Get-VM• Get-VMHost• New-VM• Remove-VM • Set-VM• ForEach-Object
VM Provisioning
# We'll use this more than once!$vmhost = Get-Cluster -Name VMworld | Get-VMHost | Select -first 1
# Clone a VM for the ops team to test a patch.New-VM -Name SQL03_Clone -VM SQL03 -VMHost $VMHost -RunAsync
# Create a new VM$VM = New-VM -Name "AwesomeVM01" -VMHost $vmhost -DiskMB 61440 ` -MemoryMB 4196
# Now Make a template of that fool$template = $VM | Set-VM -ToTemplate -Name "VMworld Template"
# Add a force multiplier1..6| ForEach-Object { New-VM -Name "VM$_" -Template $template -VMHost $vmhost -RunAsync }
# Cleanup old VMGet-VM SQL03_Clone | Remove-VM -DeletePermanently
Resources Books
Resources Blogs
Resources Blogs
Resources Forums
Resources Forums
Resources Forums
Where to go from here?
Books:• Learn Windows PowerShell in a Month of Lunches• VMware vSphere PowerCLI Reference
Blogs:• http://www.lucd.info/• http://www.virtu-al.net/
Forums:• http://www.vmware.com/go/PowerCLI• http://powershellcommunity.org• http://
social.technet.microsoft.com/Forums/en/ITCG/threads
Twitter: Use the hashtag #PowerShell #PowerCLI to get help.
Top Related