Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Command line 必须仅使用一行调用PowerShell脚本吗?_Command Line_Powershell_Parameters - Fatal编程技术网

Command line 必须仅使用一行调用PowerShell脚本吗?

Command line 必须仅使用一行调用PowerShell脚本吗?,command-line,powershell,parameters,Command Line,Powershell,Parameters,我有一些PowerShell脚本可以接受很多长参数,比如 myScript.ps1 -completePathToFile "C:\...\...\...\file.txt" -completePathForOutput "C:\...\...\...\output.log" -recipients ("me@me.com") -etc. 我似乎无法让PowerShell运行这样的脚本,除非所有参数都在一行上。有没有更像这样调用脚本的方法 myScript.ps1 -completePat

我有一些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认为命令在行尾完成,除非它看到某些字符,如管道、打开的paren或打开的curly。只需在每行末尾添加一行连续字符````,但请确保该连续字符后面没有空格:

myScript.ps1 `
  -completePathToFile "C:\...\...\...\file.txt" `
  -completePathForOutput "C:\...\...\...\output.log" `
  -recipients (
    "me@me.com", `
    "him@him.com" `
   ) 
如果您使用的是PowerShell 2.0,还可以将这些参数放入哈希表中并使用Splating,例如:

$parms = @{
    CompletePathToFile   = 'C:\...\...\...\file.txt'
    CompletPathForOutput = 'C:\...\...\...\output.log'
    Recipients           = 'me@me.com','him@him.com'
}
myScript.ps1 @parms

我发现逗号运算符足以使Powershell继续下一行,只要它实际将其作为运算符而不是以“,”结尾的字符串参数进行解析,谢谢。我相信还有其他角色向PowerShell表明还有更多。我不知道有没有记录在案的清单。基思,谢谢你告诉我,这里有一个奢华的地方。我读了之后就忘了。现在我看到它为如何使用脚本/函数参数带来了很多可能性。