如何从Powershell获取返回的值并将其保存在批处理文件中? [英] How to get a returned value from powershell and get it in a batch file?

查看:149
本文介绍了如何从Powershell获取返回的值并将其保存在批处理文件中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Commande从批处理文件执行Powershell: Powershell.\ nameoffile.ps1

I'm trying to execute a powershell from a batch file with the commande: Powershell .\nameoffile.ps1

PowerShell返回一些值1,4,0和-1.如何从批处理中获取这些值?当我使用%errorlevel%时,它仅返回0(这意味着脚本可以).我也尝试在PowerShell(出口4)中使用退出"命令,但是它不起作用. 你能帮我吗?

The PowerShell returns some values 1, 4, 0 and -1 . How can I get these values from the batch? When I use %errorlevel% it only returns 0 (which means that the script is okay). I have also tried using the Exit command in PowerShell (Exit 4) but It does not work. Can you help me?

编辑 如果有人感兴趣,我已经找到了解决方案.

EDIT I have found a solution if someone is interested.

powershell "&{.\test.ps1 %* ;exit $LastExitCode}" set code=%errorlevel%

推荐答案

powershell "&{.\test.ps1 %* ;exit $LastExitCode}" set code=%errorlevel%

这篇关于如何从Powershell获取返回的值并将其保存在批处理文件中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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