views:

343

answers:

1

I have some PowerShell scripts that accept many long parameters, like,

myScript.ps1 -completePathToFile "C:\...\...\...\file.txt" -completePathForOutput "C:\...\...\...\output.log" -recipients ("[email protected]") -etc.

I can't seem to make PowerShell run such scripts unless all the parameters are on a single line. Is there a way to invoke the script more like this?

myScript.ps1
  -completePathToFile "C:\...\...\...\file.txt"
  -completePathForOutput "C:\...\...\...\output.log"
  -recipients (
    "[email protected]",
    "[email protected]"
   )
  -etc

The lack of readability is driving me nuts, but the scripts really do need to be this parametric.

+11  A: 

PowerShell thinks the command is complete at the end of the line unless it sees certain characters like a pipe, open paren or open curly. Just put a line continuation character ` at the end of each line but make sure there are no spaces after that continuation character:

myScript.ps1 `
  -completePathToFile "C:\...\...\...\file.txt" `
  -completePathForOutput "C:\...\...\...\output.log" `
  -recipients (
    "[email protected]", `
    "[email protected]" `
   ) 

If you're on PowerShell 2.0 you can also put those parameters in a hashtable and use splatting e.g:

$parms = @{
    CompletePathToFile   = 'C:\...\...\...\file.txt'
    CompletPathForOutput = 'C:\...\...\...\output.log'
    Recipients           = '[email protected]','[email protected]'
}
myScript.ps1 @parms
Keith Hill
I've found that the comma operator is enough to make Powershell continue to the next line, SO LONG AS it's actually parsing it as an operator and not a string parameter that ends in ','
Richard Berg
Thanks. I'm sure there other characters indicate to PowerShell there's more. Not sure there is a documented list of these.
Keith Hill
Keith, thanks for remininding me there is a splatting in Posh. I read it and then forget. Now I see that it brings a lot of possibilities of how to work with script/function parameters.
stej