Comments on: SOLVED: Easily Script Windows 10 to Download, Install and Restart For Windows Updates https://www.urtech.ca/2018/11/solved-easily-script-windows-10-to-download-install-and-restart-for-windows-updates/ Technology Doesn't Stop & Neither Does URTech.ca Mon, 24 Aug 2020 13:39:21 +0000 hourly 1 By: Albert https://www.urtech.ca/2018/11/solved-easily-script-windows-10-to-download-install-and-restart-for-windows-updates/comment-page-1/#comment-237230 Mon, 24 Aug 2020 13:39:21 +0000 https://www.urtech.ca/?p=10781#comment-237230 natecull , thanks for your script. It returns a few errors when I tried to run it from powershell
Has anyone been able to run it?

[ref] cannot be applied to a variable that does not exist.
At C:\Users\Administrator\Desktop\2.ps1:29 char:1

$ntdll::RtlAdjustPrivilege(19,$true,$false,[ref]$x)

+ CategoryInfo : InvalidOperation: (x:VariablePath) [], RuntimeException
+ FullyQualifiedErrorId : NonExistingVariableReference
Cannot find an overload for “InitiateShutdown” and the argument count: “2”.
At C:\Users\Administrator\Desktop\2.ps1:37 char:1

$advapi32::InitiateShutdown($null,”Installing updates and restarting” …

+ CategoryInfo : NotSpecified: (:) [], MethodException
+ FullyQualifiedErrorId : MethodCountCouldNotFindBest

]]>
By: Boris https://www.urtech.ca/2018/11/solved-easily-script-windows-10-to-download-install-and-restart-for-windows-updates/comment-page-1/#comment-236125 Fri, 15 Feb 2019 12:58:29 +0000 https://www.urtech.ca/?p=10781#comment-236125 Thank you for your explanations.
Tried to reproduce some commands from your script.
It seems that the best option is to provide a full path to “usoclient” like:

%windir%\System32\UsoClient StartDownload

]]>
By: natecull https://www.urtech.ca/2018/11/solved-easily-script-windows-10-to-download-install-and-restart-for-windows-updates/comment-page-1/#comment-235982 Mon, 17 Dec 2018 02:39:32 +0000 https://www.urtech.ca/?p=10781#comment-235982 Thank you very much for this! One comment: if, like me, you would prefer to run pure Powershell commands rather than a third-party EXE, you can replace ShutdownWithUpdates.exe with the following pure PowerShell:

This runs the two Win32 API functions: ntdll.dll::RtlAdjustPrivilege for priv 19 (SE_SHUTDOWN_NAME) to enable the Shutdown privilege for this process, and advapi32.dll::InitiateShutdown to restart with code 0x44 (install updates and restart) and reason 0x80020011 (Planned, application, hotfix).

$signature = @”
[DllImport(“ntdll.dll”, SetLastError = true)]
public static extern IntPtr RtlAdjustPrivilege(int Privilege, bool bEnablePrivilege, bool IsThreadPrivilege, out bool PreviousValue);
“@

$ntdll = Add-Type -MemberDefinition $signature -name “NtDll” -Namespace “Win32″ -PassThru

$signature = @”
[DllImport(“advapi32.dll”, SetLastError = true)]
public static extern UInt32 InitiateShutdown(string lpMachineName, string lpMessage, UInt32 dwGraceperiod, UInt32 dwShutdownFlags, UInt32 dwReason);
“@

$advapi32 = Add-Type -MemberDefinition $signature -name “AdvApi32” -Namespace “Win32″ -PassThru

$ntdll::RtlAdjustPrivilege(19,$true,$false,[ref]$x)

$x = $null
$advapi32::InitiateShutdown($null,”Installing updates and restarting”,10,[Uint32]’0x44′,[Uint32]’0x80020011′)

]]>