Top Banner
POWERCLI IN THE ENTERPRISE: BREAKING THE MAGICIAN’S CODE @jonathanmedd
34

PowerCLI in the Enterprise Breaking the Magicians Code original

May 24, 2015

Download

Technology

jonathanmedd
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: PowerCLI in the Enterprise Breaking the Magicians Code   original

POWERCLI IN THE ENTERPRISE: BREAKING THE MAGICIAN’S CODE

@jonathanmedd

Page 2: PowerCLI in the Enterprise Breaking the Magicians Code   original

PowerShell and PowerCLI

Other programming languages are available………

…but the principles in this session can typically be applied to most of them.

Page 3: PowerCLI in the Enterprise Breaking the Magicians Code   original

…move your code from the equivalent of this….

…to this………

Page 4: PowerCLI in the Enterprise Breaking the Magicians Code   original

From Command to Script…..

Page 5: PowerCLI in the Enterprise Breaking the Magicians Code   original

….to Function

Reasons for functions:

Primary: Break up code to manageable, reusable chunks

Also:

1. No cmdlet exists for a task

2. Existing cmdlet doesn’t behave like you want it to

3. String multiple cmdlets together

Page 6: PowerCLI in the Enterprise Breaking the Magicians Code   original

Functions…….

function Get-Something {

Take some input

Do something with it

Produce some output

}

function Set-Something {

Take some input

Change the configuration of something

}

Page 7: PowerCLI in the Enterprise Breaking the Magicians Code   original

Function Recommendations

1) Name Your Functions Verb-Noun

Get-VMHostLicense & Set-VMHostLicense

Get-Verb | Sort Verb

….will give you a list of approved verbs

Page 8: PowerCLI in the Enterprise Breaking the Magicians Code   original

Function Recommendations

2) Use Built-In Help

Page 9: PowerCLI in the Enterprise Breaking the Magicians Code   original
Page 10: PowerCLI in the Enterprise Breaking the Magicians Code   original

Function Recommendations

3) Generalise everything!

No hardcoded data in functions (or scripts)! Data should always come from an external source…CSV, XML, SQL database….etc

Page 11: PowerCLI in the Enterprise Breaking the Magicians Code   original

Function Recommendations

4) Parameters

Page 12: PowerCLI in the Enterprise Breaking the Magicians Code   original

Function Recommendations

5) Pipeline Support

ValueFromPipeline = $true

and

Page 13: PowerCLI in the Enterprise Breaking the Magicians Code   original

Function Flexibility

You need to be able to cater for pipeline support:

Get-VMHost | Get-VMHostSomething

as well as

Get-VMHostSomething –VMHost ESXi01,ESXI02

Page 14: PowerCLI in the Enterprise Breaking the Magicians Code   original

Function Templates

Build up templates for Get-Something and Set-Something, then to create say Get-VMHostiSCSIBinding, all you need is this:

Page 15: PowerCLI in the Enterprise Breaking the Magicians Code   original

….to Module

A method to organise your functions into collections of functions

Easy to distribute via file copy / XCOPY Easy to access all functions via:

Import-Module MyModule

Tip: Use a Module Manifest to make your module professional: New-ModuleManifest

Page 16: PowerCLI in the Enterprise Breaking the Magicians Code   original

Modules

Typically will contain at least 2 files*.psm1 - containing your functions*.psd1 – the manifest

$env:PSModulePath contains the path Import-Module will look for modules○ Typically this is your user profile\Documents\

WindowsPowerShell\Modules and○ C:\Windows\system32\WindowsPowerShell\v1.0\

Modules\ - use this one for deployment!

Page 17: PowerCLI in the Enterprise Breaking the Magicians Code   original

Modules

Page 18: PowerCLI in the Enterprise Breaking the Magicians Code   original

Modules: PSM1

Difficult to find individual functions

Hassle to maintain to keep them in a particular order

Page 19: PowerCLI in the Enterprise Breaking the Magicians Code   original
Page 20: PowerCLI in the Enterprise Breaking the Magicians Code   original

Nested Modules

Break each module down into Nested Modules each containing a single function

Reference the Nested Modules in the *.psd1

Page 21: PowerCLI in the Enterprise Breaking the Magicians Code   original
Page 22: PowerCLI in the Enterprise Breaking the Magicians Code   original

Import-Module vSphereTools

Page 23: PowerCLI in the Enterprise Breaking the Magicians Code   original
Page 24: PowerCLI in the Enterprise Breaking the Magicians Code   original

Module: Search

Search for string patterns

dir * -Recurse | Select-String 'PSBoundParameters'

Page 25: PowerCLI in the Enterprise Breaking the Magicians Code   original

Module: Search

Search for metadata

Search-ModuleTag -Module vSphereTools -Tag ‘iscsi’

Page 26: PowerCLI in the Enterprise Breaking the Magicians Code   original

Modules: Considerations

Module Dependencies – One module may requires functions from another module

Generic module with standard tools for other modules

Page 27: PowerCLI in the Enterprise Breaking the Magicians Code   original

Flexibility Now you have code broken down into

Functions and Modules you can pick out various functions or entire modules to plug into:Command line useScriptsAutomation and Orchestration systems

Page 28: PowerCLI in the Enterprise Breaking the Magicians Code   original

Backup / Source Control

Internal File Share Dropbox or other….

Git / Atlassian StashFree Atlassian SourceTree client for Windows and

OSX. Works with all Git repositories (not just Stash), including GitHub

Page 29: PowerCLI in the Enterprise Breaking the Magicians Code   original

Source Control – Version History

Page 30: PowerCLI in the Enterprise Breaking the Magicians Code   original
Page 31: PowerCLI in the Enterprise Breaking the Magicians Code   original

Template Functions

Will be available on my blog after this presentation

○ Get-Something○ Set-Something○ Get-VMSomething○ Set-VMSomething○ Get-VMHostSomething○ Set-VMHostSomething

Page 32: PowerCLI in the Enterprise Breaking the Magicians Code   original

Template SnippetsAnd also these….Ctrl-J in PowerShell ISE

Page 33: PowerCLI in the Enterprise Breaking the Magicians Code   original

Tweet Poll Results

Page 34: PowerCLI in the Enterprise Breaking the Magicians Code   original