command-line - 必须仅使用一行来调用 PowerShell 脚本吗?

标签 command-line powershell parameters

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

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

除非所有参数都在一行上,否则我似乎无法让 PowerShell 运行此类脚本。有没有办法更像这样调用脚本?
myScript.ps1
  -completePathToFile "C:\...\...\...\file.txt"
  -completePathForOutput "C:\...\...\...\output.log"
  -recipients (
    "me@me.com",
    "him@him.com"
   )
  -etc

缺乏可读性让我发疯,但脚本确实需要具有这种参数化。

最佳答案

PowerShell 认为该命令在行尾完成,除非它看到某些字符,如管道、打开括号或打开 curl 。只需在每行的末尾放置一个行继续符```,但要确保在该连续符之后没有空格:

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

如果您使用的是 PowerShell 2.0,您还可以将这些参数放在哈希表中并使用 splatting,例如:
$parms = @{
    CompletePathToFile   = 'C:\...\...\...\file.txt'
    CompletPathForOutput = 'C:\...\...\...\output.log'
    Recipients           = 'me@me.com','him@him.com'
}
myScript.ps1 @parms

关于command-line - 必须仅使用一行来调用 PowerShell 脚本吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2057631/

相关文章:

command-line - Java 执行命令在代码中不起作用

command-line - PowerShell - 启动进程和命令行开关

powershell - 在同一行上获取日期和时间

.net - 在PowerShell中仅使用WinSCP下载新文件

ruby - 用 YARD 记录 "splatted"参数的最佳方法?

c# - 如何告诉代码契约(Contract)指定为参数的委托(delegate)是纯的?

node.js - 在运行测试的命令中传递 .env 变量时浏览器的不同行为

linux - 调用 gdb 时是否可以从命令行参数中获取 'set print thread-events off'?

groovy - Jenkins 管道将所有参数转换为小写

linux - GNU 排序命令的行为(带有非字母 ASCII 字符,例如点或分号)