Search icon CANCEL
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
PowerCLI Essentials

You're reading from   PowerCLI Essentials Simplify and automate server administration tasks with PowerCLI

Arrow left icon
Product type Paperback
Published in Apr 2016
Publisher Packt
ISBN-13 9781785881770
Length 194 pages
Edition 1st Edition
Arrow right icon
Author (1):
Arrow left icon
Chris Halverson Chris Halverson
Author Profile Icon Chris Halverson
Chris Halverson
Arrow right icon
View More author details
Toc

Scripting with the intention to reuse

Changing gears from storing the scripts is the ability to repurpose them, or use portions of a script in multiple areas. Functions, workflows, and modules are critical to this end. Each will be described in detail in this section of the book.

Functions, functions, functions – the fun with functions

What is a function? The help file defines it as a named block of code that performs an action. So writing a function would look like this:

Function Get-PS {get-process PowerShell}

Here, the output would be nothing until the function is called like this:

Function Get-PS {get-process PowerShell}
Get-PS

The output of this is the same as Get-Process PowerShell but it is reusable and could be called at any point in the script. There is a simple and useful function that I personally use in many of the developed scripts to get a decision and return true or false:

Function Get-Decision ($Decision) {
  Switch ($Decision.toupper()) {
    "Y" {$Decision =...
lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime