从批处理中调用PowerShell,并检索脚本中设置的临时环境变量的新值?
我希望标题简洁,但以防万一:
我正在从批处理文件调用 PowerShell 脚本。我希望 PowerShell 脚本设置环境变量的值,并在 PowerShell 脚本完成时在批处理文件中提供该新值。
我知道可以在 PowerShell 中使用 $env 设置环境变量,但当 PowerShell 脚本终止时该值不会保留。我想这可能是因为 PowerShell 在单独的进程中执行。
我知道我可以返回退出代码并使用 %ErrorLevel%,但这只会给我数字,并且会发生冲突,因为 1 表示 PowerShell 异常而不是有用的数字。
现在,需要注意的是:我不希望环境变量持续存在。也就是说,我不希望它为用户或系统定义,因此我希望它在批处理文件退出后立即不可用。最终,我只是想将结果从 PowerShell 脚本传回调用批处理文件。
这可能吗?
预先感谢:)
尼克
I hope the title is concise, but just in case:
I am calling a PowerShell script from a batch file. I want the PowerShell script to set the value of an environment variable, and for that new value to be available in the batch file when the PowerShell script finishes.
I know that it is possible to set an environment variable using $env in PowerShell, but the value does not persist when the PowerShell script terminates. I imagine this is probably because PowerShell gets executed in a separate process.
I am aware that I can return an exit code and use %ErrorLevel%, but that will only give me numbers, and there will be a conflict, since 1 indicates a PowerShell exception rather than a useful number.
Now, here's the caveat: I don't want the environment variable to persist. That is, I don't want it to be defined for the user or system, and therefore I want it to be unavailable as soon as the batch file exits. Ultimately, I simply want to communicate results back from a PowerShell script to the calling batch file.
Is this possible?
Thanks in advance :)
Nick
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
为了让 Keith 的使用 stdout 的想法发挥作用,您可以从批处理脚本中调用 powershell,如下所示:
有点尴尬,但它有效:
To get Keith's idea of using stdout to work, you can invoke powershell from your batch script like this:
A little awkward, but it works:
我知道回答这个问题有点晚了,但我想尝试一下,以防有人需要更详细的解决方案。那么,就这样吧。
我创建了一个批处理函数,它将为您执行 ps 脚本并返回一个值,如下所示:
现在,作为使用它的示例:
这将显示在控制台屏幕上您明白了
作为一个更复杂的示例,我将添加:
让我们假设我们要检查虚拟机是Up还是Down,即它是打开还是关闭,所以我们可以执行以下操作:
现在,如何使用 :CheckMachineUpOrDown 函数?
只需按照以下示例操作即可:
如果虚拟机已打开,则显示“Up”;如果计算机关闭,则显示“Down”。
希望这有帮助。
谢谢
I know it's a little bit late to answer this question, but I would like to give it a try just in case any one needs more detailed solution. So, here it goes.
I created a batch function that would execute ps script for you and return a value, something like this:
Now, as an example to use it:
This will display on console screen You got it
As a more complicated example, I would add:
Let's assume that we want to check if a VM is Up or Down, meaning if it's powered on or off, so we can do the following:
Now, how to use :CheckMachineUpOrDown function?
just follow this example:
This will display Up if the VM is Powered On or Down if the machine is Off.
Hope this is helpful.
Thanks
从 PowerShell 捕获结果的最直接方法是在 PowerShell 中使用 stdout。例如,这会将日期保存到 cmd.exe 中的 d 环境变量
The most straight forward way to capture results from PowerShell is to use stdout in PowerShell. For example, this saves the date to the d env var in cmd.exe