'Restart Services and Set to Automatic from Manual after the Last Successful Patch is Installed

writing a PowerShell script to detect a given number of planned patches have been installed - then restart an application's services. For example four patches are going to be installed - and you don't know if the four patches all require reboot. The objective is to restart application services and reset them from Manual to Automatic after the forth patch is installed - whether the server has been rebooted after the last 4th patch or not.

You then have the PowerShell script run via Task Scheduler every time "when the computer starts up" to it will look for the counter to be at 4. This would be easy if every time a patch was installed a reboot would launch the script. The if the counter shows the forth patch installed successfully - do the Start-Service for the application and Set-Service -StartupType back to Automatic.

How would you deal with the logic condition that says if the last patch does not require a reboot -- get the counter = 4 (the last successful patch install) allowing you to restart the services?

$loopCount = 0
do{
##HERE YOU SET THE DATE TO THE BEGINING OF THE DAY THE PATCHES WILL BE INSTALLED
$count =  Get-HotFix | ? installedon -gt "4/13/2022 00:00:00" | Group installedon –NoElement |
    ForEach-Object Count
    

    $loopCount++
##HERE YOU SET THE COUNT BASED ON HOW MANY KB##### PATCHES WILL BE INSTALLED USING QA COUNT (example below is 4)
    if($count -eq 4){

$s = Get-Service WatchDog
Start-Service -InputObject $s | Set-Service -Name WatchDog -StartupType Automatic
        $counted = $true
    }

} until ($counted -eq $true)




Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source