Slide 1

Slide 1 text

AUTOMATING AND MANAGING VMWARE WITH POWERSHELL Guy Leech (@guyrleech) Citrix CTP, Microsoft MVP, VMware vExpert Chicago VMUG UserCon, August 2020

Slide 2

Slide 2 text

WHY AUTOMATE? • Consistency • Speed • Time of day (or night) • Lazy • Someone probably has done what you want already (but check script!) • Better things to do than do admin stuff • Improve skills • Easier to hand over • Less comeback NOT CONFIDENTAIL IN THE SLIGHTEST – SHARE WITH EVERYONE 2

Slide 3

Slide 3 text

POWERSHELL BENEFITS? • Can run interactively (it’s a shell like in *nix) • Persistent command history searching via Ctrl r • But leaves a trace with persistent profile • Lots of 3rd party scripts available • But check they aren’t malicious, either accidentally or deliberately • Tab completion of commands, arguments and parameters • Huge number of cmdlets • Get-Command to search commands • Get-Help to get per cmdlet help with examples • Get-Member to see properties and methods • Many 3rd party modules available, e.g. VMware, Citrix • Repeatable – same s**t, different cmdlet • Aliases to make typing quicker (but don’t use in scripts) • Easy to export to CSV, XML, JSON for reporting/sharing/comparing (& import)

Slide 4

Slide 4 text

INSTALLING & MAINTAINING POWERCLI • Online Installation • Install-Module VMware.PowerCLI • Run elevated unless –Scope CurrentUser • Get-Module –Name VMware.* -ListAvailable • Update-Module • Offline installation • Save-Module • Copy to a folder in $env:PSModulePath (“-split ‘;’” to easily visualize) • Ensure read-only & not blocked (Unblock-File) NOT CONFIDENTAIL IN THE SLIGHTEST – SHARE WITH EVERYONE 4

Slide 5

Slide 5 text

CONNECTING • Just import the module(s) you need, e.g. VMware.VimAutomation.Core • Credentials • Pass thru • Explicit – DO NOT pass clear text passwords on command line • Get-Credential • -SaveCredential • New-VICredentialStoreItem/Get-VICredentialStoreItem/Remove-VICredentialStoreItem • Can retrieve clear text password but only as the user who stored it on the same machine • Connect-VIServer • vCenter or ESXi Host (multiple in one call) • -AllLinked • -Force • Set-PowerCLIConfiguration • $global:DefaultVIServers and $global:DefaultVIServer • Connecton duration timeout • Disconnect - optional • Beware simultaneous connections to vCenter & ESXi hosts – duplicate objects NOT CONFIDENTAIL IN THE SLIGHTEST – SHARE WITH EVERYONE 5

Slide 6

Slide 6 text

WHAT CAN WE DO? • Show/Delete VMs • Get snapshot information - delete old, large, etc • Power Operations • Clone • Datastores • Reconfigure VMs, networks, advanced settings, etc • Anything you can do in console • Reporting – produce csv, html, etc & email • Poor man’s monitoring/alerting • In guest operations (Invoke-VMscript but PowerShell remoting may be better) • … NOT CONFIDENTAIL IN THE SLIGHTEST – SHARE WITH EVERYONE 6

Slide 7

Slide 7 text

POWERSHELL DRIVES • Access datastores & inventories like you would a local file system • Vis:\ • Vmstores:\ • Can be slow, e.g. dir and tab completion • /folder web interface faster but read-only • Beware thin to fat implicit conversions • Some limitations, e.g. not all properties/methods implemented like NTFS • Useful for maintenance and size checking NOT CONFIDENTAIL IN THE SLIGHTEST – SHARE WITH EVERYONE 7

Slide 8

Slide 8 text

HORIZON • VMware.VimAutomation.HorizonView • Only three cmdlets • Once connected use View API service returned when connected • Connect-HVServer • VMware.Hv.Helper module • Now has a REST API • Invoke-RestMethod • No VMware PowerShell module required • Good samples on GitHub • $global:DefaultHVServers NOT CONFIDENTAIL IN THE SLIGHTEST – SHARE WITH EVERYONE 8

Slide 9

Slide 9 text

RUNNING SCRIPTS • Have a single machine with all consoles & PowerShell modules • Scheduled tasks • What account? • VMware Credentials - Secure that password! • Watch for $true/$false arguments & flattened arrays • Send email or other alert/message? • Manually • What account? • Add a GUI? • Character based • Windows Presentation Foundation (WPF) NOT CONFIDENTAIL IN THE SLIGHTEST – SHARE WITH EVERYONE 9

Slide 10

Slide 10 text

DOCUMENTATION • F1 in PowerShell_ISE • Get-Help • -ShowWindow • -Online • Get-Command • gcm -Module vmware* -noun *network* -verb Get • https://code.vmware.com/doc/preview?id=6330/doc/Overview.html • VMware samples in Github • Web search (not Bing ) NOT CONFIDENTAIL IN THE SLIGHTEST – SHARE WITH EVERYONE 10

Slide 11

Slide 11 text

MISCELLANEOUS • Hyper-V also has Get-VM • Module qualify VMware.VimAutomation.Core\Get-VM • Import-Module -Prefix • Remove-Module • Get-Command (fast as no searching required, use in loops) • Out-GridView • Great for quick data visualisation, filtering & sorting • -PassThru to use data elsewhere, e.g. paste to notepad, use as GUI selector • Get-View NOT CONFIDENTAIL IN THE SLIGHTEST – SHARE WITH EVERYONE 11

Slide 12

Slide 12 text

RESOURCES • https://github.com/vmware/PowerCLI-Example-Scripts/tree/master/Scripts • https://www.controlup.com/script-library/ • https://github.com • https://communities.vmware.com/community/vmtn/automationtools/powercli • Web search • Twitter • Luc Dekens @LucD22 • Andy Morgan @andyjmorgan • Guy Leech @guyrleech • Ton de Vreede @WillCode4Cheese • You? • …

Slide 13

Slide 13 text

EXIT( 0 ) • Thank you

Slide 14

Slide 14 text

GUY LEECH • Independent consultant, developer, trainer, adviser, troubleshooter, “comedian” • @guyrleech • [email protected] • guyrleech.wordpress.com • linkedin.com/in/guyrleech/ • github.com/guyrleech • pastebin.com/u/guyrleech