5. ….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
6. 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
}
7. Function Recommendations
1)
Name Your Functions Verb-Noun
Get-VMHostLicense & Set-VMHostLicense
Get-Verb | Sort Verb
….will give you a list of approved verbs
13. Function Flexibility
You need to be able to cater for pipeline
support:
Get-VMHost | Get-VMHostSomething
as well as
Get-VMHostSomething –VMHost ESXi01,ESXI02
14. Function Templates
Build up templates for Get-Something and SetSomething, then to create say GetVMHostiSCSIBinding, all you need is this:
15. ….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
16. 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
profileDocumentsWindowsPowerShellModules and
○
C:Windowssystem32WindowsPowerShellv1.0Mo
dules - use this one for deployment!
27. 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 use
Scripts
Automation and Orchestration systems
28. Backup / Source Control
Internal File Share
Dropbox or other….
Git / Atlassian Stash
Free Atlassian SourceTree client for Windows and
OSX. Works with all Git repositories (not just
Stash), including GitHub
31. Template Functions
Will be available on my blog after this presentation
○ Get-Something
○ Set-Something
○ Get-VMSomething
○ Set-VMSomething
○ Get-VMHostSomething
○ Set-VMHostSomething