使用powershell计划任务远程更新Windows安全性

使用powershell计划任务远程更新Windows安全性,powershell,powershell-remoting,windows-update,windows-security,Powershell,Powershell Remoting,Windows Update,Windows Security,我正在尝试使用powershell远程处理在远程计算机上安装windows安全修补程序。 这是我用来更新windows的函数 <# .SYNOPSIS This functiion will automatically install all avaialable windows updates on a device and will automatically reboot if needed, after reboot, windows updates will continue t

我正在尝试使用powershell远程处理在远程计算机上安装windows安全修补程序。 这是我用来更新windows的函数

<#
.SYNOPSIS
This functiion will automatically install all avaialable windows updates on a device and will automatically reboot if needed, after reboot, windows updates will continue to run until no more updates are available.
#>

function Install-WindowsUpdates

{
Install-Module -Name PSWindowsUpdate -RequiredVersion 2.1.0.1 -Force
Import-Module PSWindowsUpdate -Force
Get-WindowsUpdate -install -acceptall
}
运行此脚本时,出现以下错误:

Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))
    + CategoryInfo          : NotSpecified: (:) [Get-WindowsUpdate], UnauthorizedAccessException
    + FullyQualifiedErrorId : System.UnauthorizedAccessException,PSWindowsUpdate.GetWindowsUpdate
    + PSComputerName        : 10.0.0.7
我已经设置了loaclhost和远程机器进行远程处理,并且能够远程执行其他脚本。还启用了远程计算机上的WMI。 我还需要做哪些其他设置

使用计划任务: 我正在使用以下脚本启动计划任务:

param(
   [parameter(Mandatory = $true)]
   [string]$IPaddress
)
$PSModulePath = $env:PSModulePath
$SplittedModulePath = $PSModulePath.Split(";")
$ModulePath = $SplittedModulePath[0]
$secpasswd = ConvertTo-SecureString "Pass@12345678" -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential ("Admin02", $secpasswd)
#Create a Session. Replace host name with the host name of the remote machine.
$Session = New-PSSession -ComputerName $IPaddress -Credential $cred
$User= "Admin02"
$Action= New-ScheduledTaskAction -Execute "PowerShell.exe" -Argument "$env:ALLUSERSPROFILE\Install-WindowsUpdate.ps1"
$Trigger= New-ScheduledTaskTrigger -At 5:05am -Once
Invoke-Command -Session $Session -ScriptBlock { Register-ScheduledTask -TaskName "Install-Updates" -User $Using:User -Action $Using:Action -Trigger $Using:Trigger -RunLevel Highest –Force }
我已经在目标机器上复制了以下脚本,路径为$env:ALLUSERSPROFILE

<#
.SYNOPSIS
This functiion will automatically install all avaialable windows updates on a device and will automatically reboot if needed, after reboot, windows updates will continue to run until no more updates are available.
.PARAMETER computer
Use the Computer parameter to specify the Computer to remotely install windows updates on.
#>


Install-Module -Name PSWindowsUpdate -RequiredVersion 2.1.0.1 -Force
Import-Module PSWindowsUpdate -Force
Get-WindowsUpdate -install -acceptall


安装模块-名称PSWindowsUpdate-所需版本2.1.0.1-强制
导入模块PSWindowsUpdate-强制
获取WindowsUpdate-install-acceptall

在我安排任务后,什么都没有发生。我做错了什么?

这似乎是设计上不可能的:


远程连接的用户似乎不可能从互联网上下载东西

说到windows update,您有许多选择,如:

  • 使用psexec工具连接,然后运行wauclt/detectnow/updatenow

  • 如果您使用的是windows 10/server 2016,则这些工具将被更有效的USOClient.exe替换


  • 是的,我为此奋斗了几个星期,终于找到了一个好的解决办法。该解决方案实际上内置在PSWindowsUpdate模块中。内置的解决方案确实使用了windows任务,但它会立即启动,而且它实际上有助于跟踪其完成进度,并确保集成的安全性。我发现的问题是PSWindowsUpdate的文档很差。以下代码适用于我:

    Invoke-WUJob -ComputerName $svr -Script {ipmo PSWindowsUpdate; Get-WUInstall -AcceptAll -AutoReboot -Install | Out-File C:\PSWindowsUpdate.log } -Confirm:$false -Verbose -RunNow
    
    关于这个话题有很多零散的信息,所以请阅读。PSWindowsUpdate是目前为止最适合这项工作的库,虽然这对我来说是一个漫长的过程,但我相信上述解决方案对每个人都适用

    请记住,您运行上述scrip的计算机需要信任您尝试更新的计算机,您可以运行此脚本以信任该计算机:

    Set-Item WSMan:\localhost\Client\TrustedHosts -Value <ComputerName>
    
    设置项目WSMan:\localhost\Client\TrustedHosts-Value
    
    注意:计算机名中可以使用通配符

    我还想给你一些对我帮助很大的信息:

    获取WindowsUpdate:这是模块的主要cmdlet。它列出、下载、安装或隐藏满足预定义条件的更新列表,并在安装更新时设置重启规则

    删除WindowsUpdate:卸载更新

    添加WUServiceManage:注册新的Windows Update API服务管理器

    获取历史记录:显示已安装更新的列表

    获取WUSettings:获取Windows Update客户端设置

    获取WUInstallerStatus:获取Windows Update安装程序状态,无论它是否忙

    启用WURemoting:为PSWindowsUpdate远程处理启用防火墙规则

    调用WUJob:远程调用PSWindowsUpdate操作

    与所有PowerShell cmdlet一样,可以为键入Get-Help“command”的每个命令显示不同的用法示例-示例。 PSWindowsUpdate主要参数

    如前一节所示,PSWindowsUpdate模块包括不同的预定义别名,以简化修补过程。但是,Get-WindowsUpdate cmdlet的主要参数将在下面列出并解释:

    筛选更新

    AcceptAll:下载或安装所有可用更新

    KBArticleID:查找包含KBArticleID(或KBArticleID集)的更新

    UpdateID:指定具有特定UUID(或UUID集)的更新

    类别:指定包含指定类别名称的更新,例如“更新”、“安全更新”或“关键更新”

    标题:查找与部分标题匹配的更新

    严重性:查找与部分严重性匹配的更新,例如“重要”、“严重”或“中等”

    UpdateType:查找具有特定类型的更新,例如“驱动程序”和“软件”。默认值包含所有更新

    行动和目标

    下载:下载已批准的更新,但不安装

    安装:安装已批准的更新

    隐藏:隐藏指定的更新以防止安装它们

    ScheduleJob:指定作业开始的日期

    SendReport:从安装过程发送报告

    ComputerName:指定目标服务器或计算机

    客户端重新启动行为

    自动重启:根据需要自动重启系统

    忽略重新启动:禁止自动重新启动

    ScheduleReboot:指定系统重新启动的日期

    #如何避免意外安装#

    Windows更新和修补程序改进了系统的功能和稳定性。但是,某些更新可能会弄乱系统并导致不稳定,尤其是传统软件(如图形卡驱动程序)的自动更新。为避免自动更新和意外安装此类应用程序,您可以暂停Windows更新

    或者,您可以隐藏不想更新的功能的特定更新。隐藏更新后,Windows将无法再下载和安装此类更新。在隐藏更新之前,您需要了解其详细信息,包括其知识库(KB)编号和标题。在下面键入cmdlet以列出所有
    Set-Item WSMan:\localhost\Client\TrustedHosts -Value <ComputerName>