windows - 无法通过 PowerShell 脚本安装 Microsoft 更新(修补程序)-msu

标签 windows powershell scripting hotfix

我尝试通过下面的 PowerShell 脚本安装 MSU,但它没有安装。我不知道 PowerShell 脚本。请有人给我修改这个脚本的最佳方法。我很期待听到。

Start-Process "C:\Installer\windows10.0-kb4520062-x64_9f2a827f11f945d19bd26de6f113f611a38bb8a1.msu" -ArgumentList "/quiet /norestart"

最佳答案

为了能够将参数传递给通过 Start-Process 启动的进程,(可能是位置隐含的)-FilePath 参数必须是真正的可执行文件,而不仅仅是一个文档,这就是 . msu 文件是。

因此,您必须将.msu文件注册为显式处理的可执行文件传递给-FilePath,即wusa.exe(请注意 -ArgumentList 参数末尾的 /quiet/norestart):

Start-Process `
  -FilePath     "$env:SystemRoot\System32\wusa.exe" `
  -ArgumentList 'C:\Installer\windows10.0-kb4520062-x64_9f2a827f11f945d19bd26de6f113f611a38bb8a1.msu /quiet /norestart'

注意:

  • 为了等待安装完成,请添加 -Wait 开关。

  • 要另外检查进程的退出代码以确定安装是否成功,请添加 -PassThru 开关,在变量中捕获生成的进程信息对象,然后检查它的 .ExitCode 属性。

但是,有一个技巧可以简化上述过程:将 wusa.exe 调用通过管道传输到 Write-Output,这会强制它同步执行(作为 GUI-子系统应用程序,wusa.exe 否则会异步运行),并将其退出代码记录在自动 $LASTEXITCODE 变量中:

# Note the "| Write-Output" at the end of the line,
# which makes wusa.exe run synchronously and records its process
# exit code in $LASTEXITCODE.
& "$env:SystemRoot\System32\wusa.exe" C:\Installer\windows10.0-kb4520062-x64_9f2a827f11f945d19bd26de6f113f611a38bb8a1.msu /quiet /norestart | Write-Output

if ($LASTEXITCODE -ne 0)
  throw "wusa.exe indicates failure; exit code was: $LASTEXITCODE"
}

'Installation succeeded.'

关于windows - 无法通过 PowerShell 脚本安装 Microsoft 更新(修补程序)-msu,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72482039/

相关文章:

string - 如何在 Powershell 中运行 ffmpeg 命令并将变量传递给它?

bash - Nmap - RTTVAR 已经增长到超过 2.3 秒,下降到 2.0

c - 尝试构建 Com0Com - c0clog.h 缺少文件

java - Java "cross-platform"的真正含义是什么?

.net - Powershell 中的私有(private)范围和本地范围有什么区别?

linux - Bash 脚本循环在 If 语句上过早退出

bash - 如何使文件名中的空格成为 bash while 循环中的条件?

python - 将TBB与OpenCV和Python(Eclipse)结合使用

windows - 用于卸载显示适配器驱动程序的 PnPutil 命令不起作用

git - 发出 git log 命令时 PowerShell 挂起