Thera are a lot of material about patching Windows using PowerShell. And some about patching remotely.
When optimizing/stabilizing patches for SharePoint they should be applied in separate batches.
- OS Patches
- SQL Patches
- SharePoint Patches
To greatly ease the selection of patches to apply, I'm using the module PS Gallery - PSWindowsUpdate.
However, most of the patches for SharePoint has to be run interactively, which is possible using Scheduled Tasks
. But that normally requires a user with the appropriate permissions to already be logged in locally, which defies the purpose of remote scripting :/
But I remembered setting S4U
, Service For User, that should bypass that requirement. However, I can't seem to find the correct params to use.
I do think S4U
is blocked from accessing the network (although other sources states otherwise), so I have tried to make sure the patches are downloaded beforehand. Either by using the built-in WindowsUpdateProvider
or PSWindowsUpdate
.
The account used for running the scheduled task of course also need the system privilege Logon as batch job
.
One thing that might trip things up in my case is that legacy RPC has to be kept disabled throughout the network.
Invoke-Command -ComputerName RemoteSrv -ScriptBlock {
$SoftwareUpdates = Start-WUScan -SearchCriteria "Type='Software' AND IsInstalled =0"
Install-WUUpdates -Updates $SoftwareUpdates -DownloadOnly $true | Out-Null
$Trigger = New-ScheduledTaskTrigger -Once -At (Get-Date).AddYears(10)
$Argument = "Install-WindowsUpdate -NotCategory 'Microsoft SQL Server','Office Online Server' -AcceptAll -AutoReboot"
$Action = New-ScheduledTaskAction -Execute "PowerShell.exe" -Argument "$Argument"
$Principal = New-ScheduledTaskPrincipal `
-UserId 'domain\userid' `
-RunLevel Highest `
-LogonType S4U
Register-ScheduledTask `
-TaskPath '\SharePoint\' -TaskName 'PatchSharePoint' `
-Trigger $Trigger `
-Principal $Principal `
-Action $Action `
-Force | Out-Null
Start-ScheduledTask -TaskPath '\SharePoint\' -TaskName 'PatchSharePoint'
}
The patches requiring interactive logon still isn't applied. But the Action Completed
in the Task Scheduler history states.
Task Scheduler successfully completed task "\SharePoint\PatchSharePoint", instance "{...},
action "PowerShell.exe" with return code 0.
Edit: Fixed typo in Argument.