SlideShare uma empresa Scribd logo
1 de 34
@jonathanmedd
PowerShell and PowerCLI
Other programming languages are
available………
…but the principles in this session can
typically be applied to most of them.
…move your code from
the equivalent of this….

…to this………
From Command to Script…..
….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
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
}
Function Recommendations
1)

Name Your Functions Verb-Noun

Get-VMHostLicense & Set-VMHostLicense

Get-Verb | Sort Verb
….will give you a list of approved verbs
Function Recommendations
2)

Use Built-In Help
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
Function Recommendations
4)

Parameters
Function Recommendations
Pipeline Support
ValueFromPipeline = $true
and
5)
Function Flexibility
You need to be able to cater for pipeline
support:
Get-VMHost | Get-VMHostSomething

as well as
Get-VMHostSomething –VMHost ESXi01,ESXI02
Function Templates
Build up templates for Get-Something and SetSomething, then to create say GetVMHostiSCSIBinding, all you need is this:
….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
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!
Modules
Modules: PSM1
Difficult to
find
individual
functions
 Hassle to
maintain to
keep them
in a
particular
order

Nested Modules
Break each module down into
Nested Modules each
containing a single function
 Reference the Nested
Modules in the *.psd1

Import-Module vSphereTools
Module: Search


Search for string patterns
dir * -Recurse | Select-String 'PSBoundParameters'
Module: Search


Search for metadata
Search-ModuleTag -Module vSphereTools -Tag ‘iscsi’
Modules: Considerations


Module Dependencies – One module may
requires functions from another module



Generic module with standard tools for other
modules
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
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
Source Control – Version History
Template Functions
Will be available on my blog after this presentation
○ Get-Something
○ Set-Something
○ Get-VMSomething

○ Set-VMSomething
○ Get-VMHostSomething
○ Set-VMHostSomething
Template Snippets
And also these….Ctrl-J in PowerShell ISE
Tweet Poll Results
PowerCLI in the Enterprise Breaking the Magicians Code   original

Mais conteúdo relacionado

Mais procurados

4. python functions
4. python   functions4. python   functions
4. python functions
in4400
 
Hack your db before the hackers do
Hack your db before the hackers doHack your db before the hackers do
Hack your db before the hackers do
fangjiafu
 

Mais procurados (20)

Unit Testng with PHP Unit - A Step by Step Training
Unit Testng with PHP Unit - A Step by Step TrainingUnit Testng with PHP Unit - A Step by Step Training
Unit Testng with PHP Unit - A Step by Step Training
 
Command pattern
Command patternCommand pattern
Command pattern
 
4. python functions
4. python   functions4. python   functions
4. python functions
 
Professional Help for PowerShell Modules
Professional Help for PowerShell ModulesProfessional Help for PowerShell Modules
Professional Help for PowerShell Modules
 
PowerShell Workshop Series: Session 2
PowerShell Workshop Series: Session 2PowerShell Workshop Series: Session 2
PowerShell Workshop Series: Session 2
 
Functions
FunctionsFunctions
Functions
 
Python libraries
Python librariesPython libraries
Python libraries
 
Unit Testing in WordPress
Unit Testing in WordPressUnit Testing in WordPress
Unit Testing in WordPress
 
Python functions
Python functionsPython functions
Python functions
 
Automation patterns on practice
Automation patterns on practiceAutomation patterns on practice
Automation patterns on practice
 
Functions and modules in python
Functions and modules in pythonFunctions and modules in python
Functions and modules in python
 
Python Programming - Functions and Modules
Python Programming - Functions and ModulesPython Programming - Functions and Modules
Python Programming - Functions and Modules
 
Hack your db before the hackers do
Hack your db before the hackers doHack your db before the hackers do
Hack your db before the hackers do
 
Java Docs
Java DocsJava Docs
Java Docs
 
Functions in Python
Functions in PythonFunctions in Python
Functions in Python
 
Software design patterns ppt
Software design patterns pptSoftware design patterns ppt
Software design patterns ppt
 
Functions in Python
Functions in PythonFunctions in Python
Functions in Python
 
Oracle PL/SQL exception handling
Oracle PL/SQL exception handlingOracle PL/SQL exception handling
Oracle PL/SQL exception handling
 
Javadoc guidelines
Javadoc guidelinesJavadoc guidelines
Javadoc guidelines
 
Built in function
Built in functionBuilt in function
Built in function
 

Semelhante a PowerCLI in the Enterprise Breaking the Magicians Code original

PowerShell Scripting and Modularization (TechMentor Fall 2011)
PowerShell Scripting and Modularization (TechMentor Fall 2011)PowerShell Scripting and Modularization (TechMentor Fall 2011)
PowerShell Scripting and Modularization (TechMentor Fall 2011)
Concentrated Technology
 
Sergei Stryukov.Drush.Why it should be used.DrupalCamp Kyiv 2011
Sergei Stryukov.Drush.Why it should be used.DrupalCamp Kyiv 2011Sergei Stryukov.Drush.Why it should be used.DrupalCamp Kyiv 2011
Sergei Stryukov.Drush.Why it should be used.DrupalCamp Kyiv 2011
camp_drupal_ua
 

Semelhante a PowerCLI in the Enterprise Breaking the Magicians Code original (20)

Kernel module programming
Kernel module programmingKernel module programming
Kernel module programming
 
An Introduction to PowerShell Modules
An Introduction to PowerShell ModulesAn Introduction to PowerShell Modules
An Introduction to PowerShell Modules
 
PowerShell for Penetration Testers
PowerShell for Penetration TestersPowerShell for Penetration Testers
PowerShell for Penetration Testers
 
Node.js
Node.jsNode.js
Node.js
 
Unit 6 Operating System TEIT Savitribai Phule Pune University by Tushar B Kute
Unit 6 Operating System TEIT Savitribai Phule Pune University by Tushar B KuteUnit 6 Operating System TEIT Savitribai Phule Pune University by Tushar B Kute
Unit 6 Operating System TEIT Savitribai Phule Pune University by Tushar B Kute
 
Node Session - 2
Node Session - 2Node Session - 2
Node Session - 2
 
PowerShell Scripting and Modularization (TechMentor Fall 2011)
PowerShell Scripting and Modularization (TechMentor Fall 2011)PowerShell Scripting and Modularization (TechMentor Fall 2011)
PowerShell Scripting and Modularization (TechMentor Fall 2011)
 
Readme
ReadmeReadme
Readme
 
Puppet quick start guide
Puppet quick start guidePuppet quick start guide
Puppet quick start guide
 
Power shell training
Power shell trainingPower shell training
Power shell training
 
Dn D Custom 1
Dn D Custom 1Dn D Custom 1
Dn D Custom 1
 
Dn D Custom 1
Dn D Custom 1Dn D Custom 1
Dn D Custom 1
 
Kernel Module Programming
Kernel Module ProgrammingKernel Module Programming
Kernel Module Programming
 
PowerShell for Cyber Warriors - Bsides Knoxville 2016
PowerShell for Cyber Warriors - Bsides Knoxville 2016PowerShell for Cyber Warriors - Bsides Knoxville 2016
PowerShell for Cyber Warriors - Bsides Knoxville 2016
 
Sergei Stryukov.Drush.Why it should be used.DrupalCamp Kyiv 2011
Sergei Stryukov.Drush.Why it should be used.DrupalCamp Kyiv 2011Sergei Stryukov.Drush.Why it should be used.DrupalCamp Kyiv 2011
Sergei Stryukov.Drush.Why it should be used.DrupalCamp Kyiv 2011
 
PowerShell - Be A Cool Blue Kid
PowerShell - Be A Cool Blue KidPowerShell - Be A Cool Blue Kid
PowerShell - Be A Cool Blue Kid
 
Terraform training - Modules 🎒
Terraform training - Modules 🎒Terraform training - Modules 🎒
Terraform training - Modules 🎒
 
Chapter - 4.pptx
Chapter - 4.pptxChapter - 4.pptx
Chapter - 4.pptx
 
Python Modules, executing modules as script.pptx
Python Modules, executing modules as script.pptxPython Modules, executing modules as script.pptx
Python Modules, executing modules as script.pptx
 
Managing Perl Installations: A SysAdmin's View
Managing Perl Installations: A SysAdmin's ViewManaging Perl Installations: A SysAdmin's View
Managing Perl Installations: A SysAdmin's View
 

Último

Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 

Último (20)

Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot ModelNavi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Navi Mumbai Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
A Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source MilvusA Beginners Guide to Building a RAG App Using Open Source Milvus
A Beginners Guide to Building a RAG App Using Open Source Milvus
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu SubbuApidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
Apidays Singapore 2024 - Modernizing Securities Finance by Madhu Subbu
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 

PowerCLI in the Enterprise Breaking the Magicians Code original