小编var*_*eda的帖子

在批处理脚本中获取Powershell变量值

我想在批处理脚本中获取导出的powershell变量的值。以下是示例脚本。

样本.ps1

$myname="user1"
Run Code Online (Sandbox Code Playgroud)

sample.bat

@echo on
FOR /F "delims=" %%i IN ('powershell . "D:\sample.ps1"; (Get-Variable myname).value') DO SET VAL=%%i
echo %VAL%
pause
Run Code Online (Sandbox Code Playgroud)

在执行sample.bat时,我总是遇到错误。

.value') was unexpected at this time.
Run Code Online (Sandbox Code Playgroud)

但是,如果我在powershell中像下面那样执行,则可以得到正确的输出。

. "D:\sample.ps1"; (Get-Variable myname).value
Run Code Online (Sandbox Code Playgroud)

我想知道如何在批处理脚本中转义围绕Get-Variable命令的括号。或者想知道任何其他方法来获取批处理脚本中导出的powershell变量的值。

windows powershell cmd batch-file

4
推荐指数
1
解决办法
5534
查看次数

标签 统计

batch-file ×1

cmd ×1

powershell ×1

windows ×1