Update Windows/PSPatching.ps1

Got Good -er
was just the bones before, now its got *logic*
This commit is contained in:
Upgrayedd 2024-08-21 19:20:11 -05:00
parent b65e728162
commit 82815d4116

View file

@ -1,17 +1,46 @@
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force
Install-Module PSWindowsUpdate -Force
Set-ExecutionPolicy bypass
Import-Module PSWindowsUpdate
#for old windows, 8/2012r2, check to come
#[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
#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
}
#do we even need updates?
$updates = Get-WUList -verbose
if ($updates.Contains("Found [0] Updates")){
Write-Host "No Updates"
}
else {
Write-Host "Updates are `n" $updates.Title
}
#time logic
$now = Get-Date
$WorkHours = $now.Hour -in 8..17
Get-WUList -verbose
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"
}
Install-WindowsUpdate -AcceptAll -ignorereboot | Out-File "C:\Windows\Temp\$(get-date -f yyyy-MM-dd)-WindowsUpdate.log" -force
Get-WURebootStatus -silent
$tonight = (Get-Date -Hour 23)
Get-WURebootStatus -ScheduleReboot $tonight | select RebootScheduled
if (Get-WURebootStatus -silent) {
$tonight = (Get-Date -Hour 23)
Get-WURebootStatus -ScheduleReboot $tonight | select RebootScheduled
}
else {
Write-Host "Updates installed without reboot required"
}