dump/Windows/PSPatching.ps1

46 lines
1.1 KiB
PowerShell
Raw Permalink Normal View History

#for old windows, 8/2012r2, check to come
#[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
2024-07-30 23:40:39 -05:00
#module logic
if (Get-Module -ListAvailable -Name PSWindowsUpdate) {
Import-Module PSWindowsUpdate
Write-Host "On it"
}
else {
Write-Host "Haven't been here before, give me a min"
Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force
Install-Module PSWindowsUpdate -Force
}
2024-07-30 23:40:39 -05:00
#do we even need updates?
$updates = Get-WUList -verbose
if ($updates.Contains("Found [0] Updates")){
Write-Host "No Updates"
2024-07-30 23:40:39 -05:00
}
else {
Write-Host "Updates are `n" $updates.Title
}
2024-07-30 23:40:39 -05:00
#time logic
$now = Get-Date
$WorkHours = $now.Hour -in 8..17
2024-07-30 23:40:39 -05:00
if (!$WorkHours) {
Install-WindowsUpdate -AcceptAll -ignorereboot | Out-File "C:\Windows\Temp\$(get-date -f yyyy-MM-dd)-WindowsUpdate.log" -force
}
else {
#scheduled task creation goes here
Write-Host "Its still during operating hours, do me again later"
}
2024-07-30 23:40:39 -05:00
if (Get-WURebootStatus -silent) {
$tonight = (Get-Date -Hour 23)
Get-WURebootStatus -ScheduleReboot $tonight | select RebootScheduled
}
else {
Write-Host "Updates installed without reboot required"
}