[英]batch multiline powershell command
I am trying to parse appsettings.json file to extract the connection string using a batch script.我正在尝试解析 appsettings.json 文件以使用批处理脚本提取连接字符串。
I am spreading the command on multiple lines as it is quite long and hard to maintain:我在多行上传播命令,因为它很长且难以维护:
set ps_GetConnectionString=(Get-Content 'appsettings.json' -Raw)^
| ForEach-Object { $_ -replace '(?ms)/\*.*?\*/' -replace '[\s\t]+//.*' }^
| ConvertFrom-Json^
| Select -expand ConnectionStrings | Select default^
| ConvertTo-Csv -Delimiter ~
When I run this, I get an error: 'ForEach-Object' is not recognized as an internal or external command
.运行此命令时,出现错误:
'ForEach-Object' is not recognized as an internal or external command
。
How do we properly break this command into multiple lines, store in a variable in order to make the powershell -command
call much shorter?我们如何正确地将这个命令分成多行,存储在一个变量中,以便使
powershell -command
调用更短?
@LotPings hinted to the solution which was to escape everything that batch can trip on. @LotPings 暗示了解决方案,即逃避批处理可能绊倒的一切。
So I end up with the following:所以我最终得到以下结果:
set ps_GetConnectionString=(Get-Content 'appsettings.json' -Raw)^
^| ForEach-Object { $_ -replace '(?ms)/\*.*?\*/' -replace '[\s\t]+//.*' }^
^| ConvertFrom-Json^
^| Select -expand ConnectionStrings ^| Select default^
^| ConvertTo-Csv -Delimiter ~
In some situations, I also had to escape the parenthesis (especially inside a block)在某些情况下,我还必须转义括号(尤其是在块内)
set ps_GetConnectionString=^(Get-Content 'appsettings.json' -Raw^)^
^| ForEach-Object { $_ -replace '(?ms)/\*.*?\*/' -replace '[\s\t]+//.*' }^
^| ConvertFrom-Json^
^| Select -expand ConnectionStrings ^| Select default^
^| ConvertTo-Csv -Delimiter ~
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.