Skip to content

Instantly share code, notes, and snippets.

@jhorsman
Last active February 27, 2018 08:14
Show Gist options
  • Select an option

  • Save jhorsman/425baff83e22e1b90cdc20ccbdfc04af to your computer and use it in GitHub Desktop.

Select an option

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 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 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