何时可以在Powershell命令中使用换行符(换行符)来提高可读性
如何知道何时可以在Powershell脚本中使用新的换行符/回车符?搜索此答案时,所有搜索结果都指向输出。在这种情况下,我不关心输出。我对格式化Powershell脚本以提高可读性的能力更感兴趣何时可以在Powershell命令中使用换行符(换行符)来提高可读性,powershell,syntax,newline,code-formatting,Powershell,Syntax,Newline,Code Formatting,如何知道何时可以在Powershell脚本中使用新的换行符/回车符?搜索此答案时,所有搜索结果都指向输出。在这种情况下,我不关心输出。我对格式化Powershell脚本以提高可读性的能力更感兴趣 $1 = "--file 'C:\Program Files\ArangoDB3 3.3.3\usr\bin\tstImportJSON.json'" $2 = "--type json" $3 = "--collection users" $4 = "--progress true" $5 = "--
$1 = "--file 'C:\Program Files\ArangoDB3 3.3.3\usr\bin\tstImportJSON.json'"
$2 = "--type json"
$3 = "--collection users"
$4 = "--progress true"
$5 = "--overwrite true"
$6 = "--server.username root"
$7 = "--server.password password"
$Arguments = "$1 $2 $3 $4 $5 $6 $7"
& 'C:\Program Files\ArangoDB3 3.3.3\usr\bin\arangoimp.exe' $Arguments
比如说。下面是Powershell命令行的两个版本。一个工作,一个不工作。在这种情况下,命令的作用并不重要。关键是我需要知道什么时候我可以创建一条新的生产线,什么时候不可以
此命令行的工作原理是,它只是一条很长的单行:
& 'C:\Program Files\ArangoDB3 3.3.3\usr\bin\arangoimp.exe' --file 'C:\Program Files\ArangoDB3 3.3.3\usr\bin\tstImportJSON.json' --type json --collection users --progress true --overwrite true --server.username root --server.password password
此命令行不起作用,因为脚本中间有一条新行。
& 'C:\Program Files\ArangoDB3 3.3.3\usr\bin\arangoimp.exe' --file
'C:\Program Files\ArangoDB3 3.3.3\usr\bin\tstImportJSON.json'
--type json --collection users --progress true --overwrite true
--server.username root --server.password password
在我的例子中,我只是在添加换行符后运行同一命令行的不同版本,看看它们是否有效。我知道当使用IF语句时,我可以开始一个新行。我也可以在管道对象|时使用新线。我的假设是,在某个地方有一个Powershell脚本规则列表。我想我曾经在某个地方见过它们,当时我刚开始进入Powershell,但不知道它现在在哪里。您可以使用
`
作为换行符[1]
在一行的最末尾(后面甚至不允许有空格),以便将其分散到多行
下面是一个简化的示例:
& cmd.exe /c echo `
hi `
there
这相当于&cmd.exe/c echo hi there
,并产生hi there
注:
-
< > >强> >由于<代码> 字符在视觉上都是微妙的,而且语法很容易被意外地放置字符之后打破,请考虑使用数组作为替代物——见下文。< /强>
- 单个命令必须在一行上,因此,如果要将它们分散到多行上,则需要
,如上所示`
- 但是,在管道中,您可以以
结束该行,然后继续下一行,而不需要|
;e、 g:`
Get-Date | # Because the line ends with |, parsing continues on the next line. Select-Object Day
- 在PowerShell[Core]v7+中,您也可以将
放在下一行的开头:|
- 在PowerShell[Core]v7+中,您也可以将
- 此外,如果单个参数在中创建新的解析上下文-例如固有的多行功能
表达式或传递给cmdlet的脚本块((…)
)-您还可以将表达式跨多行传播;e、 g:{…}
1, 2, 3 | ForEach-Object { # { starts a multiline-aware context $_ + 1 }
- 混合大小写是一个数组文字参数,它允许在内部元素的
分隔符之后打断命令:,
Get-Date | Select-Object -Property Day, Year
- 以表达式模式开始的语句始终允许跨多行扩展(尽管嵌入的命令模式语句受到通常的限制):
<强>可选地考虑使用数组< /强>(2)传递参数,这允许使用多行表达式模式语法预先将参数定义为单独的数组元素:
注意:数组元素hi-there
由PowerShell作为“hi-there”
传递:它采用自动双引号,以确保目标程序将参数识别为单个参数
[1]
`
是PowerShell的通用转义字符。放在一行的末尾,它的功能有着微妙的不同:它没有转义后面的换行符(这意味着将其保留为文字),而是有效地告诉PowerShell删除它,并将下一行视为当前行的延续
[2] 此答案的早期形式建议基于数组,然后指出,对于调用外部程序,按原样使用数组就足够了。虽然也可以使用splatting,但如果其中一个数组元素是停止解析符号
-->%
,则其语义会略有不同(简而言之:只有当splatting有其特殊含义时,-->%
。但是,在调用PowerShell命令时,Splating是一种非常有用的技术,主要是以哈希表的形式调用(请参见上一链接)。为了增加可读性,请稍微分解脚本
$1 = "--file 'C:\Program Files\ArangoDB3 3.3.3\usr\bin\tstImportJSON.json'"
$2 = "--type json"
$3 = "--collection users"
$4 = "--progress true"
$5 = "--overwrite true"
$6 = "--server.username root"
$7 = "--server.password password"
$Arguments = "$1 $2 $3 $4 $5 $6 $7"
& 'C:\Program Files\ArangoDB3 3.3.3\usr\bin\arangoimp.exe' $Arguments
您的输出将如下所示
C:\Program Files\ArangoDB3 3.3.3\usr\bin\arangoimp.exe --file 'C:\Program Files\ArangoDB3 3.3.3\usr\bin\tstImportJSON.json' --type json --collection users --progress true --overwrite true --server.username root --server.password password
通常情况下,我会将这样一个更丑的野兽移动到它自己的功能中。实际上,它不是一个脚本。这只是一个命令行。简单命令行不允许有换行符。如果使用Powershell cmdlet,如
启动进程
,则可以在逗号、semikola、左括号或大括号后使用换行符。。。可以这样尝试:Start Process-FilePath'C:\Program Files\ArangoDB3.3.3\usr\bin\arangoimp.exe'-ArgumentList'-file'C:\Program Files\ArangoDB3 3.3.3\usr\bin\tstImportJSON.json'“--type json”、“--collection users”、“--progress true”、“--overwrite true”、“--server.username root”、“--server.password
当然,您可以在管道“|”之后使用换行符@Olaf:在行尾使用`
确实允许您将命令跨多行传播<代码>启动进程有不同的用途,不能替代控制台程序的直接调用。如果你想避免复杂的行尾语法,`的话,飞溅就是答案。这一点很好。我发布的只是一个命令行,我正在将它保存到一个脚本中,我正在通过任务调度器运行该脚本。附录:虽然基于数组的splatting是一个选项,@PetSerAl正确地指出,您可以直接传递数组(当调用外部程序时)。实际上backtick不是一个行延续字符。这是一个埃萨卡普·查拉赫特。你逃避的“角色”是一个断线,它作为一个
$1 = "--file 'C:\Program Files\ArangoDB3 3.3.3\usr\bin\tstImportJSON.json'"
$2 = "--type json"
$3 = "--collection users"
$4 = "--progress true"
$5 = "--overwrite true"
$6 = "--server.username root"
$7 = "--server.password password"
$Arguments = "$1 $2 $3 $4 $5 $6 $7"
& 'C:\Program Files\ArangoDB3 3.3.3\usr\bin\arangoimp.exe' $Arguments
C:\Program Files\ArangoDB3 3.3.3\usr\bin\arangoimp.exe --file 'C:\Program Files\ArangoDB3 3.3.3\usr\bin\tstImportJSON.json' --type json --collection users --progress true --overwrite true --server.username root --server.password password