vmworld 2016: getting started with powershell and powercli for your vmware enviornment

51
Getting Started with PowerShell and PowerCLI for Your VMware Environment Kyle Ruddy, VMware, Inc Chris Wahl, Rubrik INF8038 #INF8038

Upload: vmworld

Post on 07-Jan-2017

175 views

Category:

Technology


1 download

TRANSCRIPT

Page 1: VMworld 2016: Getting Started with PowerShell and PowerCLI for Your VMware Enviornment

Getting Started with PowerShell and PowerCLI for Your VMware EnvironmentKyle Ruddy, VMware, IncChris Wahl, Rubrik

INF8038

#INF8038

Page 2: VMworld 2016: Getting Started with PowerShell and PowerCLI for Your VMware Enviornment

CONFIDENTIAL 2

• This presentation may contain product features that are currently under development.

• This overview of new technology represents no commitment from VMware to deliver these features in any generally available product.

• Features are subject to change, and must not be included in contracts, purchase orders, or sales agreements of any kind.

• Technical feasibility and market demand will affect final delivery.

• Pricing and packaging for any new technologies or features discussed or presented have not been determined.

Disclaimer

Page 3: VMworld 2016: Getting Started with PowerShell and PowerCLI for Your VMware Enviornment

Who is Kyle Ruddy?

WriterGitHub

PodcastTwitter

@ ThatCouldBeAProblem.com@ github.com/kmruddy@ vBrownBag.com@kmruddy

Senior Technical Marketing Engineer, VMware

Page 4: VMworld 2016: Getting Started with PowerShell and PowerCLI for Your VMware Enviornment

Who is @ChrisWahl?

WriterHost

InstructorEvangelistMicrosoft

VMware

@ WahlNetwork.com@ DatanautsPodcast.com@ Pluralsight.com@ Rubrik.comMVP (PowerShell)VCDX (DCV & NV)

Page 5: VMworld 2016: Getting Started with PowerShell and PowerCLI for Your VMware Enviornment

5

Agenda

• What are PowerShell and PowerCLI?• The Lingo Dictionary• Setup and Configuration• Starting to Code• Writing Logic Statements

Page 6: VMworld 2016: Getting Started with PowerShell and PowerCLI for Your VMware Enviornment

What are PowerShell and PowerCLI?

6

Page 7: VMworld 2016: Getting Started with PowerShell and PowerCLI for Your VMware Enviornment

7

“Windows PowerShell is a task automation and configuration management framework from

Microsoft, consisting of a command-line shell and associated scripting

language built on the .NET Framework.”https://en.wikipedia.org/wiki/Windows_PowerShell

Page 8: VMworld 2016: Getting Started with PowerShell and PowerCLI for Your VMware Enviornment

8

PowerShell

• A simple and straight-forward path to automation– Already installed on all modern Windows Operating

Systems– Integrated and rich help system

• Alpha build available for Linux and MacOS– https://github.com/PowerShell/PowerShell

Page 9: VMworld 2016: Getting Started with PowerShell and PowerCLI for Your VMware Enviornment

9

PowerShell

• Modular and object-oriented– The best of a programming language melded with a

scripting language– True portability of code via modules (and snap-ins)

Page 10: VMworld 2016: Getting Started with PowerShell and PowerCLI for Your VMware Enviornment

10

VMware vSphere PowerCLI

• VMware’s command-line and scripting tool built on Windows PowerShell

• Features more than 500 cmdlets for managing and automating vSphere, vCloud, and Horizon environments

• One of the most robust and complete PowerShell deployments in the world

Page 11: VMworld 2016: Getting Started with PowerShell and PowerCLI for Your VMware Enviornment

The Lingo Dictionary

Page 12: VMworld 2016: Getting Started with PowerShell and PowerCLI for Your VMware Enviornment

12

What is a Module?

• Set of related Windows PowerShell functionalities, grouped together as a convenient unit

• Usually saved in a single directory• Example Locations:

– Documents\WindowsPowerShell\Modules– Program Files\WindowsPowerShell\Modules– Windows\system32\WindowsPowerShell\v1.0\Modules

Page 13: VMworld 2016: Getting Started with PowerShell and PowerCLI for Your VMware Enviornment

13

What is a Module?

• No installation required– Just plop it into a path recognized by $env:PSModulePath

Page 14: VMworld 2016: Getting Started with PowerShell and PowerCLI for Your VMware Enviornment

14

What is a Module Manifest?

• PSD1 extension• Describes how the module was constructed• Identifies functions / cmdlets / variables / aliases

to export• DSC resources• PowerShell Gallery data

Page 15: VMworld 2016: Getting Started with PowerShell and PowerCLI for Your VMware Enviornment

15

Page 16: VMworld 2016: Getting Started with PowerShell and PowerCLI for Your VMware Enviornment

Module Manifests

16

Page 17: VMworld 2016: Getting Started with PowerShell and PowerCLI for Your VMware Enviornment

17

What is a snap-in?

• For our intents and purposes …• Precursor to Modules

– Must be written in .NET– Must be available as an Assembly– Installed (not copied)– Puts keys in the registry

• Try to avoid them!

Page 18: VMworld 2016: Getting Started with PowerShell and PowerCLI for Your VMware Enviornment

18

What is a Function?

• List of Windows PowerShell statements that has a name that you assign

• Can be called by name– Define a function named “Get-SquarePants”

• Load it in a module or dot source it– Dot sourcing = . .\Get-SquarePants.ps1– Now you can type Get-SquarePants and it will execute

Page 19: VMworld 2016: Getting Started with PowerShell and PowerCLI for Your VMware Enviornment

19

Function – Descriptive Details

<#.SYNOPSIS <Overview of script>.DESCRIPTION <Brief description of script>.PARAMETER <Parameter_Name> <Brief description of parameter input required. Repeat this attribute if required>.INPUTS <Inputs if any, otherwise state None>.OUTPUTS <Outputs if any, otherwise state None - example: Log file stored in C:\Windows\Temp\<name>.log>.NOTES Version: 1.0 Author: <Name> Creation Date: <Date> Purpose/Change: Initial script development .EXAMPLE <Example goes here. Repeat this attribute for more than one example>#>

Page 20: VMworld 2016: Getting Started with PowerShell and PowerCLI for Your VMware Enviornment

20

Function – The Meat and Potatoes

Function <FunctionName>{ Param() Begin{ Do this stuff first } Process{ Do this stuff as much as desired } End{ Do this stuff last }}

Page 21: VMworld 2016: Getting Started with PowerShell and PowerCLI for Your VMware Enviornment

21

What is a Script?

• A much less fancy version of a function– Just the “Do this stuff as much as desired” part

• Cannot be called by name like a function can• Handy for ad-hoc work• Suggestion!

– Focus on writing functions, not scripts– Use scripts in the ISE for testing code snipets

Page 22: VMworld 2016: Getting Started with PowerShell and PowerCLI for Your VMware Enviornment

Setup and Configuration

Page 23: VMworld 2016: Getting Started with PowerShell and PowerCLI for Your VMware Enviornment

23

Setting up PowerShell

• Native to all modern Windows deployments• Latest version is 5.0

– Use $PSVersionTable to see what you’re running

Page 24: VMworld 2016: Getting Started with PowerShell and PowerCLI for Your VMware Enviornment

24

Update using Windows Management Framework 5.0

• https://www.microsoft.com/en-us/download/details.aspx?id=50395

Page 25: VMworld 2016: Getting Started with PowerShell and PowerCLI for Your VMware Enviornment

Console The command line interface Microsoft.PowerShell_profile.ps1

ISE Integrated Scripting Environment Microsoft.PowerShellISE_profile.p

s1 Console + ISE

profile.ps1

Profiles make life easier and happier

Set up your profile

Page 26: VMworld 2016: Getting Started with PowerShell and PowerCLI for Your VMware Enviornment

Current User: %USERPROFILE%\Documents\WindowsPowerShell\

All Users: %WINDIR%\System32\WindowsPowerShell\v1.0\

Profiles make life easier and happier

Drop in your profile

Page 27: VMworld 2016: Getting Started with PowerShell and PowerCLI for Your VMware Enviornment

Set-ExecutionPolicy Bypass

Set-Location C:\Dropbox\Code

if ($psISE)

{

Start-Steroids

Clear-Host

Write-Host 'BEAST MODE ( °□°)╯ ╯ ︵ ┻━┻ '

}

Write yourself fun messages

Self motivation for the win!

Page 28: VMworld 2016: Getting Started with PowerShell and PowerCLI for Your VMware Enviornment

Set-ExecutionPolicy Bypass

Set-Location C:\Dropbox\Code

if ($psISE)

{

Start-Steroids

Clear-Host

Write-Host 'BEAST MODE ( °□°)╯ ╯ ︵ ┻━┻ '

}

Write yourself fun messages

Self motivation for the win!

Page 29: VMworld 2016: Getting Started with PowerShell and PowerCLI for Your VMware Enviornment

29

Where do I get PowerCLI from?

Start at: vmware.com/go/powercli

Page 30: VMworld 2016: Getting Started with PowerShell and PowerCLI for Your VMware Enviornment

30

What version of PowerShell and PowerCLI do I want?

• PowerShell– Version 5 recommended– Version 3 minimum

• PowerCLI– Newest version is always recommended

• Prerequisites– .NET Framework 4.5.x

Page 31: VMworld 2016: Getting Started with PowerShell and PowerCLI for Your VMware Enviornment

31

How do I install PowerCLI? What about updates?

• Download the newest version of VMware vSphere PowerCLI

• Execute the downloaded PowerCLI installer file• If upgrading:

– Acknowledge an earlier version of PowerCLI exists, click OK

Page 32: VMworld 2016: Getting Started with PowerShell and PowerCLI for Your VMware Enviornment

32

How do I install PowerCLI? What about updates?

• On the Welcome page, click Next.• Accept the license agreement terms, click Next.• On the Custom Setup page:

– Select the PowerCLI components you want to install.– (Optional) To change the default install location for VMware

vSphere PowerCLI, click Change and select a different Destination Folder.

– Click Next.

Page 33: VMworld 2016: Getting Started with PowerShell and PowerCLI for Your VMware Enviornment

33

How do I install PowerCLI? What about updates?

• On the Ready to Install the Program page, click Install to proceed with the installation.

• Click Finish to complete the installation process.

Page 34: VMworld 2016: Getting Started with PowerShell and PowerCLI for Your VMware Enviornment

34

Initialization of PowerCLI

• Shortcuts vs native PowerShell console• PCLI Initialization script

Page 36: VMworld 2016: Getting Started with PowerShell and PowerCLI for Your VMware Enviornment

Starting to Code

Page 37: VMworld 2016: Getting Started with PowerShell and PowerCLI for Your VMware Enviornment

37

Identify safe cmdlets and begin pulling data from vSphere

• Safe = Does not modify data

Page 38: VMworld 2016: Getting Started with PowerShell and PowerCLI for Your VMware Enviornment

38

Safe vs Non-safe cmdlets

• Suggest starting with cmdlets that pull or display data– These are “Safe” in that they are unable to modify data

• This is helpful for learning the PowerShell syntax– Never ending journey

Page 39: VMworld 2016: Getting Started with PowerShell and PowerCLI for Your VMware Enviornment

39

WhatIf and Confirm

• Extra safeguards for you to use!• Whatif

– Shows you what WOULD happen without actually modifying data

– Switch• Confirm

– Asks you to confirm before any changes are made– Boolean ($true or $false)

Page 40: VMworld 2016: Getting Started with PowerShell and PowerCLI for Your VMware Enviornment

40

WhatIf and Confirm

Page 41: VMworld 2016: Getting Started with PowerShell and PowerCLI for Your VMware Enviornment

41

Common Verbs

• Cmdlets use properly formatted verbs– Use Get-Verb to see the available options

• Most features follow a very simple pattern– Get = Gather data– Set = Change data– New = Create data– Remove = Delete data

Page 42: VMworld 2016: Getting Started with PowerShell and PowerCLI for Your VMware Enviornment

Writing Logic Statements

Page 43: VMworld 2016: Getting Started with PowerShell and PowerCLI for Your VMware Enviornment

43

Sample Use Case

• Cluster configuration– What values are currently configured?– Let’s change a few of them to match our desired state

• DRS Settings– HA is Enabled– DRS Automation Level is Fully Automated

Page 44: VMworld 2016: Getting Started with PowerShell and PowerCLI for Your VMware Enviornment

44

Gather the Data

# Variables$cluster = 'Demo'

# Gather Cluster Data$clusterConfig = Get-Cluster -Name $cluster

Page 45: VMworld 2016: Getting Started with PowerShell and PowerCLI for Your VMware Enviornment

45

Results are Stored as Objects

Page 46: VMworld 2016: Getting Started with PowerShell and PowerCLI for Your VMware Enviornment

46

Decision Making Process

• Add logic to test these values!

if ($clusterConfig.DrsAutomationLevel -ne 'FullyAutomated') { Write-Warning -Message 'DRS Automation Level is wrong!'}

Page 47: VMworld 2016: Getting Started with PowerShell and PowerCLI for Your VMware Enviornment

47

Decision Making Process

• Add logic to test these values!

if ($clusterConfig.DrsAutomationLevel -ne 'FullyAutomated') { Write-Warning -Message 'DRS Automation Level is wrong!' Set-Cluster -DrsAutomationLevel FullyAutomated}

Page 48: VMworld 2016: Getting Started with PowerShell and PowerCLI for Your VMware Enviornment

48

Reference Links

• The Complete Guide to PowerShell Punctuation– https://www.simple-talk.com/sysadmin/powershell/the-co

mplete-guide-to-powershell-punctuation/

• PowerCLI 6.3 R1 Reference Poster– http://vmware.com/go/posters

Page 49: VMworld 2016: Getting Started with PowerShell and PowerCLI for Your VMware Enviornment

Thank you!Kyle Ruddy – Tech Marketing Ninja, VMwareChris Wahl – Technical Evangelist, Rubrik

Page 50: VMworld 2016: Getting Started with PowerShell and PowerCLI for Your VMware Enviornment
Page 51: VMworld 2016: Getting Started with PowerShell and PowerCLI for Your VMware Enviornment