Skip to content

Instantly share code, notes, and snippets.

@ChaseFlorell
Last active July 28, 2021 11:03
Show Gist options
  • Save ChaseFlorell/4ced2f4638c626fd32e6 to your computer and use it in GitHub Desktop.
Save ChaseFlorell/4ced2f4638c626fd32e6 to your computer and use it in GitHub Desktop.
Passing switches to child functions within PowerShell
# 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 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
@dboc
Copy link

dboc commented Jan 10, 2019

Good!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment