必须只使用一行来调用PowerShell脚本吗?

Jef*_*art 8 parameters powershell command-line

我有一些PowerShell脚本可以接受很多长参数,比如

myScript.ps1 -completePathToFile "C:\...\...\...\file.txt" -completePathForOutput "C:\...\...\...\output.log" -recipients ("me@me.com") -etc.
Run Code Online (Sandbox Code Playgroud)

我似乎无法让PowerShell运行这样的脚本,除非所有参数都在一行上.有没有办法像这样调用脚本?

myScript.ps1
  -completePathToFile "C:\...\...\...\file.txt"
  -completePathForOutput "C:\...\...\...\output.log"
  -recipients (
    "me@me.com",
    "him@him.com"
   )
  -etc
Run Code Online (Sandbox Code Playgroud)

缺乏可读性让我疯狂,但脚本确实需要这个参数化.

Kei*_*ill 13

PowerShell认为命令在行尾完成,除非它看到某些字符,如管道,打开的paren或开放的卷曲.只需在每行的末尾添加一个行继续符"``,但要确保该延续字符后面没有空格:

myScript.ps1 `
  -completePathToFile "C:\...\...\...\file.txt" `
  -completePathForOutput "C:\...\...\...\output.log" `
  -recipients (
    "me@me.com", `
    "him@him.com" `
   ) 
Run Code Online (Sandbox Code Playgroud)

如果您使用的是PowerShell 2.0,您还可以将这些参数放在哈希表中并使用splatting,例如:

$parms = @{
    CompletePathToFile   = 'C:\...\...\...\file.txt'
    CompletPathForOutput = 'C:\...\...\...\output.log'
    Recipients           = 'me@me.com','him@him.com'
}
myScript.ps1 @parms
Run Code Online (Sandbox Code Playgroud)