gpt4 book ai didi

powershell - 如何让powershell等待exe安装?

转载 作者:行者123 更新时间:2023-12-04 17:37:10 24 4
gpt4 key购买 nike

所以我已经阅读了与这个问题相关的每一个答案,但似乎没有一个有效。

我在脚本中有这些行:

$exe = ".\wls1033_oepe111150_win32.exe"
$AllArgs = @('-mode=silent', '-silent_xml=silent_install.xml', '-log=wls_install.log"')
$check = Start-Process $exe $AllArgs -Wait -Verb runAs
$check.WaitForExit()

运行后,我对已安装的文件进行了正则表达式检查,以替换某些特定字符串,但无论我尝试做什么,它都会在程序安装时继续运行正则表达式检查。

我怎样才能让下一行在完成安装 exe 之前不执行?我也试过管道到 Out-Null 没有运气。

最佳答案

我创建了一个执行以下操作的测试可执行文件

    Console.WriteLine("In Exe start" + System.DateTime.Now);
System.Threading.Thread.Sleep(5000);
Console.WriteLine("In Exe end" + System.DateTime.Now);

然后编写了这个powershell脚本,它按预期等待exe完成运行,然后输出文本“ps1结束”和时间
push-location "C:\SRC\PowerShell-Wait-For-Exe\bin\Debug";
$exe = "PowerShell-Wait-For-Exe.exe"
$proc = (Start-Process $exe -PassThru)
$proc | Wait-Process

Write-Host "end of ps1" + (Get-Date).DateTime

下面的 powershell 也正确地等待 exe 完成。
$check = Start-Process $exe $AllArgs -Wait -Verb runas
Write-Host "end of ps1" + (Get-Date).DateTime

添加 WaitForExit 调用会给我这个错误。
You cannot call a method on a null-valued expression.
At line:2 char:1
+ $check.WaitForExit()
+ ~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull

然而这确实有效
$p = New-Object System.Diagnostics.Process
$pinfo = New-Object System.Diagnostics.ProcessStartInfo("C:\PowerShell-Wait-For-Exe\bin\Debug\PowerShell-Wait-For-Exe.exe","");
$p.StartInfo = $pinfo;
$p.Start();
$p.WaitForExit();
Write-Host "end of ps1" + (Get-Date).DateTime

我想也许您将 Start-Process powershell 命令与 .NET 框架 Process 对象混淆了

关于powershell - 如何让powershell等待exe安装?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14721184/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com