Last active
July 28, 2021 11:03
-
-
Save ChaseFlorell/4ced2f4638c626fd32e6 to your computer and use it in GitHub Desktop.
Passing switches to child functions within PowerShell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# this is the final (child) function being called | |
function Invoke-Foo { | |
[cmdletbinding()] | |
param([switch] $Custom) | |
Write-Host 'Hello world' | |
Write-Verbose 'I''m a verbose message' | |
Write-Debug 'I''m a debug message' #this will pause execution | |
if($Custom.IsPresent){ | |
Write-Host 'I''m a custom message' | |
} | |
Write-Host #just here to add a line break | |
} | |
# this is the test function used to pass switch parameters | |
function Test-Helper { | |
[cmdletbinding()] | |
param([switch] $Custom) | |
# notice I'm only passing custom switches to Invoke-Foo | |
Invoke-Foo -Custom:$Custom | |
} | |
# --------------------------------------- | |
# calling the test helper to demonstrate passing switches | |
# without any switches | |
Test-Helper | |
# with custom switch | |
Test-Helper -Custom | |
# with -verbose switch | |
Test-Helper -Verbose | |
# with -debug switch | |
Test-Helper -Debug |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This is the output after running Execute.ps1 | |
============================================== | |
Hello world | |
Hello world | |
I'm a custom message | |
Hello world | |
VERBOSE: I'm a verbose message | |
Hello world | |
DEBUG: I'm a debug message |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Good!