在批处理脚本中获取Powershell变量值 [英] Getting Powershell variable value in batch script

查看:1235
本文介绍了在批处理脚本中获取Powershell变量值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want get value of an exported powershell variable in a batch script. Below are the sample scripts.

$myname="user1"



sample.bat



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时,我总是出现错误。

While executing sample.bat, I am always getting below error.

.value') was unexpected at this time.

但是,如果我在powershell中执行下面的命令,我能够得到正确的输出。

But, if I execute like below in powershell I am able to get proper output.

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



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

I want to know how to escape the parenthesis around Get-Variable command in batch script. Or would like to know any other way to get the value of exported powershell variable in batch script.

推荐答案

这没有更多的信息,但看看这是否有助于:

I can't test this without more information, but see if this helps:

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

这篇关于在批处理脚本中获取Powershell变量值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆