powershell - 如何从powershell获取返回值并将其放入批处理文件中?

标签 powershell batch-file

我正在尝试使用命令从批处理文件中执行 powershell:
Powershell .\namefile.ps1

PowerShell 返回一些值 1, 4, 0 和 -1 。如何从批处理中获取这些值?当我使用 %errorlevel% 时,它只返回 0(这意味着脚本没问题)。我也尝试过在 PowerShell (Exit 4) 中使用 Exit 命令,但它不起作用。
你能帮助我吗?

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

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

最佳答案

如果您需要在 bat 中使用此值环境使用FOR /F :

@echo off
for /f "delims=" %%a in ('powershell .\test.ps1') do Set "$Value=%%a"

Echo Value received from Powershell : %$Value%

关于powershell - 如何从powershell获取返回值并将其放入批处理文件中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34451444/

相关文章:

batch-file - 如何在 DOS 中回显字符串“off”

.net - Powershell 中的点网表单未以 3D 形式显示

http - 如何在 PowerShell 中向 VMware ESX Server 执行 HTTP PUT 上传?

powershell - PowerShell 中由 null dsquery 引发的错误处理

windows - 如何在启动程序后关闭CMD

Windows:没有主目录的用户

powershell - 如何使用 Powershell 更改现有 URL 快捷方式的图标?

powershell - 组合 Group-Object 和 ForEach-Object?

batch-file - 用于比较文件夹的 Bat 文件

batch-file - 在 Windows 批处理文件中读取 "for"命令中的空值