Capture output from a PowerShell command in a batch file's for /f loop

前端 未结 2 2091
我寻月下人不归
我寻月下人不归 2021-01-17 01:35

This PowerShell code selects the correct value for var5 from this string. The desired result it \"Alert Raised\".

PS C:\\src\\t> $s = \'Statu         


        
2条回答
  •  不思量自难忘°
    2021-01-17 02:06

    You don't need the for loop in the batch file.

    @ECHO OFF
    SET "S=Status 58   var5=Alert Raised on: March"
    powershell -NoLogo -NoProfile -Command " '%S%' | Where-Object { $_ -match '.*var5=(.*)\s+\w+:' } | ForEach-Object { $Matches[1] } "
    

    The above gives the expected result.

提交回复
热议问题