0
点赞
收藏
分享

微信扫一扫

PowerShell巡检服务器补丁安装状态

先放置查询脚本到服务器固定位置,然后巡检时可以不用登录服务器,直接在内网工作机器上运行脚本批量查询

查询单台服务器补丁安装状态:

Invoke-Command -ComputerName 10.10.100.33 -ScriptBlock {powershell -file "C:\Program Files\PS查询补丁安装状态2.ps1"} -credential $Cred

批量查询多台服务器补丁安装状态:

$cred = Get-Credential
$name = "192.168.0.100","192.168.0.140","192.168.0.60","192.168.0.50"

foreach ($a in $name ){Invoke-Command -ComputerName $a -ScriptBlock {powershell -file "C:\Program Files\PS查询补丁安装状态2.ps1"} -credential $Cred }

PowerShell巡检服务器补丁安装状态_补丁更新

根据补丁安装列表,确定最近补丁是否安装成功,如果服务器已安装补丁,还没有重启,

则脚本会在最下面的计算机名、IP----显示“已安装补丁,需要重启


#此脚本放在服务器固定目录,批量查询时调用此脚本来查询
function Get-WindowsUpdate
{
[CmdletBinding()]
param
(
[String[]]
$ComputerName,
$Title = '*',
$Description = '*',
$Operation = '*'
)

$code = {
param
(
$Title,
$Description
)


$Type = @{
name='Operation'
expression={

switch($_.operation)
{
1 {'成功'}
2 {'成功但包含错误'}
3 {'失败'}
}
}
}


$Session = New-Object -ComObject 'Microsoft.Update.Session'
$Searcher = $Session.CreateUpdateSearcher()
$historyCount = $Searcher.GetTotalHistoryCount()
$Searcher.QueryHistory(0, $historyCount) |
Select-Object Title, Description, Date, $Type |
Where-Object { $_.Title -like $Title } |
Where-Object { $_.Description -like $Description } |
Where-Object { $_.Operation -like $Operation }
}

$null = $PSBoundParameters.Remove('Title')
$null = $PSBoundParameters.Remove('Description')
$null = $PSBoundParameters.Remove('Operation')

Invoke-Command -ScriptBlock $code @PSBoundParameters -ArgumentList $Title, $Description
}


$ip = ((ipconfig | findstr [0-9].\.)[0]).Split()[-1]

#查询补丁安装状态,只显示最后5行
Get-WindowsUpdate |?{$_.title -notlike "*Defender Antivirus*" -and $_.title -notlike "*Endpoint Protection*" }|sort date |select Operation,date,title -Last 8
$path = 'HKLM:SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\RebootRequired'
$aa = hostname
if(Test-Path $path){
echo $aa" "$ip"----已安装补丁,需要重启。"
}else{
echo $aa" "$ip"----"
}

举报

相关推荐

0 条评论