查找用于通过 PowerShell 执行 cmd 命令的退出代码 [英] Find exit code for executing a cmd command through PowerShell

查看:31
本文介绍了查找用于通过 PowerShell 执行 cmd 命令的退出代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用静默安装命令来安装软件.我正在从 PowerShell 3.0 运行此命令.

I am using a silent installation command to install software. I am running this command from PowerShell 3.0.

$silentInstall = C:\Users\Admin\Documents\Setup-2.0.exe exe /s /v"EULAACCEPTED=\"Yes\" /l*v c:\install.log /qn"

Invoke-Expression $silentInstall

这会运行安装软件的命令,但它不会等待它完成并继续执行下一行代码.我想控制安装,以便我知道它是否已完成.

This runs the command which installs the software, but it does not wait for it to complete and goes ahead with the next lines of code. I want to have control over the installation so that I would know if it's completed or not.

我如何获得 Invoke-Expression cmdlet 以便我可以知道 cmd 是否成功执行?

How do I get an error code for the Invoke-Expression cmdlet so that I can get to know if the cmd executed successfully or not?

推荐答案

您似乎在运行 MSI 安装程序.从控制台运行时,当 MSI 派生一个新进程来运行安装程序时,控制权会立即返回.没有办法改变这种行为.

It looks like you're running an MSI installer. When running from the console, control is immediately returned while MSI forks a new process to run the installer. There is no way to change this behavior.

您可能需要做的是使用 Get-Process 找到一个名为 msiexec 的进程,然后等待它完成.总是有一个 msiexec 进程在运行,它处理启动新安装程序,因此您需要找到在安装开始后启动的 msiexec 进程.

What you'll probably need to do is use Get-Process to find a process named msiexec, and wait for it to finish. There is always an msiexec process running, which handles starting new installers, so you'll need to find the msiexec process that started after your install began.

$msiexecd = Get-Process -Name 'msiexec'
C:\Users\Admin\Documents\Setup-2.0.exe exe `
                                       /s `
                                       /v"EULAACCEPTED=\"Yes\" /l*v c:\install.log /qn"
$myMsi = Get-Process -Name 'msiexec' | 
             Where-Object { $_.Id -ne $msiexecd.Id }
$myMsi.WaitForExit()
Write-Verbose $myMsi.ExitCode

这篇关于查找用于通过 PowerShell 执行 cmd 命令的退出代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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