[英]How to Install Windows Updates on Remote Computer with PowerShell
I'm trying to install Windows Updates on a Remote Computer with this command:我正在尝试使用以下命令在远程计算机上安装 Windows 更新:
$InstallSplat = @{
AcceptAll = $true
SendReport = $true
IgnoreReboot = if ($Reboot) { $false } else { $true }
PSWUSettings = @{
SmtpServer = "my mail server"
From = "myfrom <myfrom@myfrom.com>"
To = "myto <myto@myto.com>"
Port = 25
}
}
Invoke-Command -ComputerName $_ -Credential $cred -AsJob -ArgumentList $InstallSplat -ScriptBlock {
param([hashtable]$InstallSplat)
Import-Module PSWindowsUpdate
Install-WindowsUpdate @InstallSplat
$Error | out-file C:\install\installwinupdate.log -Append
}
I pass a credential Object with domain admin privileges in $cred
but I still always get this error我通过$cred
中具有域管理员权限的凭据 Object 但我仍然总是收到此错误
Install-WindowsUpdate : Access denied (Ausnahme von HRESULT: 0x80070005 (E_ACCESSDENIED)) In Zeile:4 Zeichen:25
+ Install-WindowsUpdate @InstallSplat
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Get-WindowsUpdate], UnauthorizedAccessException
+ FullyQualifiedErrorId : System.UnauthorizedAccessException,PSWindowsUpdate.GetWindowsUpdate
The Command Install-WindowsUpdate
itself does not have a credential parameter I could use.命令Install-WindowsUpdate
本身没有我可以使用的凭据参数。 The Command needs to run in an elevated PowerShell, but I use an elevated PowerShell when starting this command on my Computer.该命令需要在提升的 PowerShell 中运行,但在我的计算机上启动此命令时,我使用提升的 PowerShell。
I Also tried creating a New-PSSession
with my $cred
and run Invoke-Command -Session $session
instead of Invoke-Command -ComputerName $_
with the same result.我还尝试用我的$cred
创建一个New-PSSession
并运行Invoke-Command -Session $session
而不是Invoke-Command -ComputerName $_
,结果相同。
Does anybody know what's happening here?有人知道这里发生了什么吗? Why do I get Access denied?为什么我会被拒绝访问?
It can't have anything to do with passing the $InstallSplat
because the same thing happens if I don't pass any parameter at all and write the parameters and their Values directly at the command instead of splatting.它与传递$InstallSplat
没有任何关系,因为如果我根本不传递任何参数并将参数及其值直接写入命令而不是飞溅,则会发生同样的事情。
The Problem was, that you can't Download or Install Updates on a machine from another remote machine.问题是,您无法从另一台远程计算机上下载或安装更新。 Here's a list what you can or can't do remotely when it comes to Windows Updates 这是一个列表,当涉及到 Windows 时,您可以远程执行或不可以执行的操作
The solution is, to create a scheduled task on each server you want to install updates from a remote script, and start that task.解决方案是,在要从远程脚本安装更新的每台服务器上创建计划任务,然后启动该任务。
luckily, when you use the PSWindowsUpdate module, you don't have to do that yourself, you can just use Invoke-WUJob
(formerly Invoke-WUInstall
) which does the trick for you.幸运的是,当您使用PSWindowsUpdate模块时,您不必自己这样做,您只需使用Invoke-WUJob
(以前称为Invoke-WUInstall
)就可以了。
I used it like so ( $ServerData.Value
contains a list of my Servers) and it works like a charm.我像这样使用它( $ServerData.Value
包含我的服务器列表),它就像一个魅力。 It creates a scheduled task on each server, and runs them immediately, if you add the -RunNow
Parameter.如果您添加-RunNow
参数,它会在每台服务器上创建一个计划任务并立即运行它们。
invoke-WUJob -ComputerName $ServerData.Value -Script { Import-Module PSWindowsUpdate ; Install-WindowsUpdate -AcceptAll -SendReport -IgnoreReboot -PSWUSettings @{From='xy';Port=25;SmtpServer='xy';To='xy'} | Out-File C:\install\PSWindowsUpdateLog.txt -Append} -Confirm:$false -verbose -RunNow
Note that what you specify as a script block in -Script
will be pasted to -Command " <here> "
in your scheduled task, so you should work with '
inside -Script
.请注意,您在-Script
中指定为脚本块的内容将粘贴到计划任务中的-Command " <here> "
,因此您应该在-Script
中使用'
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.