PowerShell 获取提升的进程退出代码。

huangapple go评论67阅读模式
英文:

Powershel Get Elevated process ExitCode

问题

我正在尝试以静默方式安装应用程序,但由于我正在以提升的权限运行,无法获得正确的退出代码。

请注意,$Credetial 正试图使用另一个具有管理员权限的本地用户进行安装。

$tab = "C:\Tab Server"
$tabInstaller = "D:\TabInstaller\TabServer-64bit.exe"
$tablog = "C:\Temp\Tableaulog\TabInstaller.log"

$command = "-noprofile -command ""Start-Process cmd.exe -Verb RunAs -ArgumentList '/c ""$tabInstaller"" /silent /install ACCEPTEULA=1 ACTIVATIONSERVICE=0 ""INSTALLDIR=$($drive)"" /log $tablog'"""

[int]$result = (Start-Process "powershell.exe" -Credential $credential -ArgumentList $command -NoNewWindow -Wait -Passthru).ExitCode

我尝试通过在命令提示符中传递 echo %ERROLEVEL% 来获取退出代码,但未成功。

英文:

I am trying to install an application silently but I could not get the proper exit code since I am running elevated privilege.

Note $Credetial trying to install with another local user with admin privilages.

$tab = "C:\Tab Server"
$tab = "D:\TabInstaller\TabServer-64bit.exe"
$tablog = "C:\Temp\Tableaulog\TabInstaller.log"

$list = "-noprofile -command ""Start-Process cmd.exe -Verb RunAs -ArgumentList '/c ""$tab"" /silent /install ACCEPTEULA=1 ACTIVATIONSERVICE=0 """"""INSTALLDIR=$($drive)"""""" ""/log $tablog""'"""

[int]$result = (Start-Process "powershell.exe" -Credential $credential -ArgumentList $list -NoNewWindow -Wait -Passthru).ExitCode

I tried to get the exit code by trying to pass echo %ERROLEVEL% in the command prompt but it did not work.

答案1

得分: 0

You're using -Wait and -PassThru combined with .ExitCode in the outer Start-Process call, but you must also apply it to the inner one, so as to make the outer call relay the inner's exit code, via an exit statement:

$list = "-noprofile -command ""exit (Start-Process -Wait -PassThru -Verb RunAs cmd.exe -ArgumentList '/c ""$tab"" /silent /install ACCEPTEULA=1 ACTIVATIONSERVICE=0 """"""INSTALLDIR=$($drive)"""""" ""/log $tablog""'").ExitCode"""

(Note: I've translated the text within the code block as requested, but the code itself remains in its original form.)

英文:

You're using -Wait and -PassThru combined with .ExitCode in the outer Start-Process call, but you must also apply it to the inner one, so as to make the outer call relay the inner's exit code, via an exit statement:

$list = "-noprofile -command ""exit (Start-Process -Wait -PassThru -Verb RunAs cmd.exe  -ArgumentList '/c ""$tab"" /silent /install ACCEPTEULA=1 ACTIVATIONSERVICE=0 """"""INSTALLDIR=$($drive)"""""" ""/log $tablog""'").ExitCode"""

huangapple
  • 本文由 发表于 2023年5月21日 17:16:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76299141.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定