Last active
February 27, 2018 08:14
-
-
Save jhorsman/425baff83e22e1b90cdc20ccbdfc04af to your computer and use it in GitHub Desktop.
The wrong way to call a dodgy PowerShell script with an array of arguments
This file contains hidden or 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 a somewhat dodgy way of using arguments in PowerShell | |
| # it would be much nicer to use proper Powershell parameters | |
| Write-Host "The script args are..." | |
| foreach($arg in $args) { | |
| Write-Host $arg | |
| } |
This file contains hidden or 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 an alternative way of calling .\echo.ps1 --hello=World --bonjour=Monde | |
| # also see https://octopus.com/blog/dynamic-argument-list-when-calling-executable-from-powershell | |
| $script = ".\echo.ps1" | |
| $args = @() | |
| $args += "--hello" | |
| $args += "World" | |
| $args += "--bonjour" | |
| $args += "Monde" | |
| # the result is that echo.ps1 will only get one argument :-( | |
| & $script $args |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment