簡體   English   中英

批處理多行powershell命令

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM