繁体   English   中英

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

[英]Update Windows security remotely using powershell scheduled task

我正在尝试使用 powershell 远程处理在远程计算机上安装 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
}

当我在本地主机上运行此功能时,该功能成功安装了 Windows 安全补丁。 我有以下脚本可以远程执行相同的操作:

param(

    [Parameter(Mandatory = $true)]
    [string] $IPaddress
)
try
{
$secpasswd = ConvertTo-SecureString "Pass@12345678" -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential ("Admin02", $secpasswd)


#Create a Session.
$Session = New-PSSession -ComputerName $IPaddress -Credential $cred

cd C:\Users\Admin01\Documents
. .\Install-WindowsUpdates.ps1


Invoke-Command -Session $Session -ScriptBlock ${function:Install-WindowsUpdates}
return $true
}
catch
{
return $false
}

当我运行此脚本时,出现以下错误:

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 模块中。 内置解决方案确实使用了 Windows 任务,但它会立即启动,它实际上有助于跟踪其完成进度,并确保集成安全。 我发现的问题是 PSWindowsUpdate 的文档很差。 以下代码对我有用:

Invoke-WUJob -ComputerName $svr -Script {ipmo PSWindowsUpdate; Get-WUInstall -AcceptAll -AutoReboot -Install | Out-File C:\PSWindowsUpdate.log } -Confirm:$false -Verbose -RunNow

关于这个主题有很多零散的信息,所以请仔细阅读。 PSWindowsUpdate 是迄今为止最适合这项工作的库,虽然对我来说这是一个漫长的过程,但我相信上述解决方案对每个人都有效。

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

Set-Item WSMan:\localhost\Client\TrustedHosts -Value <ComputerName>

注意:计算机名称中可以使用通配符

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

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

Remove-WindowsUpdate :卸载更新

Add-WUServiceManage :注册新的 Windows Update API 服务管理器

Get-WUHistory :显示已安装更新的列表

Get-WUSettings :获取 Windows 更新客户端设置

Get-WUInstallerStatus :获取 Windows 更新安装程序状态,无论是否忙

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

Invoke-WUJob :远程调用 PSWindowsUpdate 操作

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

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

过滤更新

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

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

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

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

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

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

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

行动和目标

下载:下载批准的更新但不安装它们

安装:安装批准的更新

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

ScheduleJob : 指定作业开始的日期

SendReport : 发送安装过程的报告

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

客户端重启行为

AutoReboot : 如果需要,自动重启系统

IgnoreReboot :禁止自动重启

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

#如何避免意外安装#

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

或者,您可以隐藏那些不想更新的功能的特定更新。 隐藏更新后,Windows 将无法再下载和安装此类更新。 在隐藏更新之前,您需要了解其详细信息,包括其知识库 (KB) 编号和标题。 键入下面的 cmdlet 以列出系统上的所有可用更新:

获取-WULList

要使用 KB 编号隐藏特定更新,请使用鼠标复制该 KB 编号。 接下来,键入以下命令:

Hide-WUUpdate -KBArticleID KB_Number

突出显示“KB_Number”并单击粘贴以使用实际的 KB 编号替换该部分。

当提示确认操作时,键入 A,然后按 Enter 键。 如果命令成功,“Get-WUList”会列出所有可用的更新,隐藏的更新会在其状态下出现带有符号“H”。

某些更新可能无法获得更新的 KB 编号。 在这种情况下,您可以使用标题来隐藏更新。 为此,请通过下面的 cmdlet 列出所有可用更新:

获取-WULList

接下来,使用鼠标复制更新标题。 确保它与其他更新标题不同。 现在,在下面键入以下命令以隐藏更新:

Hide-WUUpdate -Title “Update_Title”

不要忘记在“更新标题”部分粘贴实际的更新标题。

当提示确认操作时,键入 A,然后按 Enter 键。 如果命令成功,“Get-WUList”将列出所有可用的更新。 但是,隐藏更新的状态显示在其下方并带有符号“H”。 如何确定错误

为了能够修复错误的部署,获得尽可能多的关于 Windows 更新安装过程的信息至关重要。 Get-WindowsUpdate cmdlet 和模块中可用的其余 cmdlet 在管理更新时提供非常详细的日志级别,包括状态、KB ID、大小或标题。

集中所有计算机日志并分析它们以查找错误,管理员将始终能够了解其 Windows 计算机和服务器的补丁级别。

以上段落来自 本站

说到 Windows 更新,您有很多选择,例如:

  1. 使用 psexec 工具连接,然后运行 ​​wuauclt /detectnow /updatenow

  2. 如果您使用的是 windows 10 /server 2016 ,这些工具已替换为更有效的 USOClient.exe。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM