【问题标题】:How can I make PowerShell wait until a command is complete before proceeding?如何让 PowerShell 等到命令完成后再继续?
【发布时间】:2014-12-17 00:14:41
【问题描述】:

我正在使用以下行根据其产品 ID 卸载 office 2007

Start-Process C:\Windows\System32\msiexec.exe -ArgumentList "/uninstall {90120000-0030-0000-0000-0000000FF1CE}"

我想在卸载完成后强制重新启动,但是使用 -Wait 或将结果传送到 Out-Null 不要等到卸载完成后再处理下一行,即重新启动。我也试过用 cmd 卸载,结果一样。

cmd /c "msiexec.exe /uninstall {90120000-0030-0000-0000-0000000FF1CE}"

有没有办法强制powershell等到卸载完成后再处理Restart-Computer命令?我在想可能写一些东西来检测 setup.exe 进程何时停止,然后再继续重新启动?

【问题讨论】:

  • 你试过Start-Process -Wait吗?
  • 在这种情况下可以使用 msiexec 的 /forcerestart 选项吗?
  • Matt - 成功了,使用cmd /c "msiexec.exe /uninstall {{90120000-0030-0000-0000-0000000FF1CE} /forcerestart"
  • @Matt 如果您不介意,我已经更新了我的答案以包含您的解决方案

标签: powershell powershell-2.0


【解决方案1】:

Start-Process 有一个等待参数:

Start-Process C:\Windows\System32\msiexec.exe -ArgumentList "/uninstall {90120000-0030-0000-0000-0000000FF1CE}" -wait

misexec.exe 卸载后重新启动的解决方案是将/forcerestart 参数添加到 msiexec 调用中,而不是尝试在 powershell 中重新启动(感谢 Matt):

Start-Process C:\Windows\System32\msiexec.exe -ArgumentList @("/uninstall {90120000-0030-0000-0000-0000000FF1CE}", "/forcerestart")

【讨论】:

  • 如果您需要使用文件名而不是 GUID 并且该文件名中包含空格:Start-Process -FilePath "C:\Windows\System32\msiexec.exe" -ArgumentList '/i "C:\Temp\SQLSysClrTypes 15.0.2000.5.msi" /qr' -NoNewWindow -Wait
【解决方案2】:

我对此的建议是实际从 Microsoft 获取 Office 删除工具并从中提取 VBS 脚本。使用 -wait 参数在 Start-Process 中运行它,然后重新启动。它不仅会像您一样尝试使用 msiexec 优雅地删除 Office,它还会返回并清理任何散乱的文件或注册表项,以防应用程序损坏并且无法很好地卸载。

【讨论】:

    【解决方案3】:

    最简单的解决方法:管道传输结果。这将强制等待,直到该过程完成。

    不需要start-processcmd

    您可以通过管道传输到out-defaultout-fileout-nullout-host,具体取决于您希望如何处理输出。 (如果您不关心输出,只需使用out-null。)

    & msiexec.exe /uninstall "{90120000-0030-0000-0000-0000000FF1CE}" | Out-Null    
    

    【讨论】:

    • 这应该是答案。然而。我会通过管道发送到Write-Verbose,以便您可以在需要时检查输出。
    猜你喜欢
    • 2011-07-04
    • 1970-01-01
    • 2019-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-07
    • 2022-10-31
    • 2013-04-25
    相关资源
    最近更新 更多