Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Automating and Managing VMware with PowerShell

Automating and Managing VMware with PowerShell

This session will show you how easy it is to get up and running with various VMware products via PowerShell to make your life easier with demonstrations and examples to take away.

Presented at the Chicago VMUG usercon - video is at https://youtu.be/LY62rPdyP7k

Guy Leech

August 13, 2020
Tweet

More Decks by Guy Leech

Other Decks in Technology

Transcript

  1. AUTOMATING AND MANAGING VMWARE WITH POWERSHELL Guy Leech (@guyrleech) Citrix

    CTP, Microsoft MVP, VMware vExpert Chicago VMUG UserCon, August 2020
  2. 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
  3. 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)
  4. 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
  5. 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
  6. 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
  7. 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
  8. 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
  9. 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
  10. 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
  11. 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
  12. 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