Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Windows 在批处理脚本中获取Powershell变量值_Windows_Powershell_Batch File_Cmd - Fatal编程技术网

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

Windows 在批处理脚本中获取Powershell变量值,windows,powershell,batch-file,cmd,Windows,Powershell,Batch File,Cmd,我希望在批处理脚本中获取导出的powershell变量的值。下面是示例脚本 示例1.ps1 蝙蝠样本 在执行sample.bat时,我总是得到以下错误 .value') was unexpected at this time. 但是,如果我在powershell中像下面这样执行,我就能够获得正确的输出 . "D:\sample.ps1"; (Get-Variable myname).value 我想知道如何在批处理脚本中转义Get Variable命令周围的括号。或者想知道在批处理脚本中获取

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

示例1.ps1 蝙蝠样本 在执行sample.bat时,我总是得到以下错误

.value') was unexpected at this time.
但是,如果我在powershell中像下面这样执行,我就能够获得正确的输出

. "D:\sample.ps1"; (Get-Variable myname).value

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

如果没有更多信息,我无法测试此方法,但请查看此方法是否有帮助:

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

移动了昨天从评论部分添加的答案

我已经解决了!!我不仅要避开右括号,还要避开分号。在为右括号添加转义字符后,我遇到了另一个错误

SET VAL=Get-Variable : Cannot find a variable with name 'myname'. 
工作命令如下

FOR /F "delims=" %%i IN ('powershell . "D:\sample.ps1"^; (Get-Variable myname^).value') DO SET VAL=%%i

@foxidrive感谢您的回答,它也起了作用。

看看这篇文章是否是您想要的@谢谢你的链接。但在这种情况下,他们将获得powershell命令的输出,比如
get date
,并读取批处理脚本中的值。此外,还尝试按如下所示转义右括号,但仍然得到错误
对于/F“delims=“%%i IN('powershell.”D:\sample.ps1;(获取变量myname^).value'),请设置VAL=%%i echo%VAL%
如果我错了,请更正我,但BAT脚本是否将\识别为转义字符?@Vish根据我的理解\将仅用作正则表达式模式的转义字符。请检查Hi@foxidrive,您能告诉我我们是否可以处理从powershell返回/导出的多个值,并将其保存在批处理文件中的多个变量中吗
SET VAL=Get-Variable : Cannot find a variable with name 'myname'. 
FOR /F "delims=" %%i IN ('powershell . "D:\sample.ps1"^; (Get-Variable myname^).value') DO SET VAL=%%i