【问题标题】:Remote execution to install a software update using powershell远程执行以使用 powershell 安装软件更新
【发布时间】:2017-02-22 09:17:28
【问题描述】:

我已经浏览了与此问题相关的所有可用问题和答案,但我尝试过的任何方法都没有得到所需的,这就是我尝试过的,

这段代码在我正在调用的模块中,如下所示:

Invoke-Command -ScriptBlock {EnsurePowerShellV5 -WMF50Path "D:\wmf50"} @param

$path = "D:\Win8.1AndW2K12R2-KB3134758-x64.msu"
Invoke-Command -ScriptBlock { & "$($env:WINDIR)\system32\wusa.exe" /c "$path /qn" }

我尝试过的其他方式:

Invoke-Command -ScriptBlock {
  $pinfo = New-Object System.Diagnostics.ProcessStartInfo
  $pinfo.FileName = "$($env:WINDIR)\system32\wusa.exe"
  $pinfo.RedirectStandardError = $true
  $pinfo.RedirectStandardOutput = $true
  $pinfo.UseShellExecute = $false
  $pinfo.Arguments = "$path /quiet"

  $p = New-Object System.Diagnostics.Process
  $p.StartInfo = $pinfo
  $p.Start() 
  $p.WaitForExit()
  $stdout = $p.StandardOutput.ReadToEnd()
  $stderr = $p.StandardError.ReadToEnd()

  Write-Verbose $stderr
  Write-Verbose $stdout
}

我使用详细时遇到的错误

System.Management.Automation.RemoteException:[错误] PowerShell 5.0 没有被检测到。请安装并重试。在 System.Management.Automation.ExceptionHandlingOps.CheckActionPreference(FunctionContext funcContext,异常异常)在 System.Management.Automation.Interpreter.ActionCallInstruction`2.Run(InterpretedFrame 帧)在 System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame 帧)在 System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame 框架)

【问题讨论】:

  • 哪个变种会触发错误输出?
  • 我怀疑这会导致问题$pinfo.Arguments = "$path /quiet",因为当我删除它时,进程正在启动但未执行

标签: powershell


【解决方案1】:

您可以尝试使用PSWindowsUpdate。按照PowerShell Gallery的说明安装它,然后尝试命令:

$WUInstallScript = { Import-Module C:\Path\PSWindowsUpdate.psm1; Get-WUInstall -AcceptAll -AutoReboot}
Invoke-WUInstall -ComputerName $hostname -Script $WUInstallScript

另见link

希望对您有所帮助。

【讨论】:

  • 可以传参数吗?
猜你喜欢
  • 2017-01-13
  • 1970-01-01
  • 2019-03-19
  • 2015-12-08
  • 2021-06-14
  • 2021-04-27
  • 2015-07-06
  • 1970-01-01
  • 2018-01-27
相关资源
最近更新 更多