[英]batch multiline powershell command
我正在尝试解析 appsettings.json 文件以使用批处理脚本提取连接字符串。
我在多行上传播命令,因为它很长且难以维护:
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 ~
运行此命令时,出现错误: 'ForEach-Object' is not recognized as an internal or external command
。
我们如何正确地将这个命令分成多行,存储在一个变量中,以便使powershell -command
调用更短?
@LotPings 暗示了解决方案,即逃避批处理可能绊倒的一切。
所以我最终得到以下结果:
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 ~
在某些情况下,我还必须转义括号(尤其是在块内)
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.