我想在批处理脚本中获取导出的PowerShell变量的值。以下是示例脚本。
sample.ps1
$myname="user1"
sample.bat
@echo on
FOR /F "delims=" %%i IN ('powershell . "D:\sample.ps1"; (Get-Variable myname).value') DO SET VAL=%%i
echo %VAL%
pause
在执行sample.bat时,我总是收到以下错误。
.value') was unexpected at this time.
但是,如果我在PowerShell中执行以下命令,就能够获得正确的输出。
. "D:\sample.ps1"; (Get-Variable myname).value
我想知道如何在批处理脚本中避免Get-Variable命令周围的括号。或者想知道在批处理脚本中获取导出的PowerShell变量值的其他方法。