Is the name of your pc "server2"? If not, that code isn't going to work. If you are running the command on the target system, you don't need to specify the computername. And if the status of the service "is equal to running", then setting it to running isn't going to do anything. You need to test for "not equal".
$servicename = "sshd" # or use the display name "OpenSSH SSH Server"
$service = Get-Service -Name $servicename
if ($service.Status -ne 'Running') {
"Starting $servicename"
$service | Set-Service -Status Running
}
if ($service.StartType -ne 'Automatic') {
"Setting $servicename to start=auto"
$service | Set-Service -StartupType Automatic
}
Windows update should not change the startup type of that service. There must be something else going on with your pc. After you install an update, and BEFORE you reboot, check the status of the service. Did it revert to manual?
Also check the system and application eventlogs and see if there are any events that refernece the service.
I have installed sshd on my win11 laptop I will see if my pc has the same issue the next time that I install any updates.