首页 > 解决方案 > 如何在powershell中只显示前三行输出

问题描述

我正在尝试创建一个脚本,该脚本将检查对 Windows 机器进行最新更新的时间。然后,如果最近的更新超过 90 天,请向 itadmin 电子邮件发送电子邮件通知并运行更新(我还没有添加电子邮件部分,但这只是一个复制意大利面)。我遇到的问题是我只想在 get-WUHistory 中显示三个最近的更新,并让可变的更新时间赶上这些。关于如何做到这一点的任何想法?提前感谢您的帮助。

另外我应该补充一点,我对 Powershell 的经验很少,大约两周前我才开始使用它,所以这可能是一个愚蠢的问题。


Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force
Install-Module PSWindowsUpdate -Force
Import-Module PSWindowsUpdate -Force 

$Version = (Get-Item "HKLM:SOFTWARE\Microsoft\Windows NT\CurrentVersion").GetValue('ReleaseID')
$Date = (Get-Date).AddDays(-90)
$UpdateTime = Get-WUHistory | Format-List -Property Date

Write-Output $Version

If($UpdateTime -lt $Date){
    ########################
}

If($version -lt 1909){
    Write-Host("This computer is not up to date") -ForegroundColor Red
    Get-WindowsUpdate -MicrosoftUpdate -Download  
}
else {
    Write-Host("This computer is up to date" ) -foregroundcolor green
    Write-Host("Checking for patches") -ForegroundColor Green
    Get-WindowsUpdate -MicrosoftUpdate -category "Security Updates" -Severity Critical,Important,Moderate 
}

#Restart-Computer -force

标签: powershellscripting

解决方案


推荐阅读