When I am logged in via remote desktop on a Windows Server 2012 R2 Core machine I can trigger the windows update by running sconfig.cmd. This however fails when its getting called within a PS remote session:
Enter-PSSession -ComputerName server2
This is what sconfig complains about. It basically says that it can't find a specific registry value. A funny thing to note is that Microsoft choose VBScript over PowerShell to program this CLI interface...
[server2]: PS C:\> sconfig
C:\>echo off
sconfig : FEHLER: Der angegebene Registrierungsschl?ssel bzw. Wert wurde nicht gefunden.
+ CategoryInfo : NotSpecified: (FEHLER: Der ang...nicht gefunden.:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
Microsoft (R) Windows Script Host, Version 5.8
Copyright (C) Microsoft Corporation. Alle Rechte vorbehalten.
System wird ?berpr?ft...
[...]
14) Server herunterfahren
15) Zur Befehlszeile wechseln
C:\Windows\System32\de-DE\sconfig.vbs(326, 1) Laufzeitfehler in Microsoft VBScript: Eingabe hinter Dateiende.
How should/can I initiate a Windows Update when being logged in via PS remote session?
You can't actually trigger that directly over winrm/winrs.
A somewhat popular powershell module (PSWindowsUpdate) for performing Windows Updates from Powershell exists, and to perform updates on a remote system it actually copies the module to the remote system and schedules a new one-time task on the remote system.
In the invoke-WUInstall.ps1 file it has this comment about remotely triggering an update.
FWIW, on Windows Server 2019, you can use
The array
$u
tells you what updates are available, if it is empty, you are done.The boolean
$b
tells you whether a reboot is required. After rebooting, you should scan for updates again.All hail to the poster of the last comment. It works.
Get the
pswidowsupdate
files from the web & unzip them. Then import the module & run this code (theinvoke-wsuinstall.ps1
file has the sample code but I removed a bit from it and it still works):