@echo off
setlocal enabledelayedexpansion& pushd "%~dp0"
title [update]
call:local
call:remote
call:update
goto:end
:local
if not exist "psexec.exe" (echo/missing 'psexec.exe' & goto:end)
if not exist "settings.ini" (echo/missing 'psexec.exe' & goto:end)
if not exist "scripts\modules\PSWindowsUpdate\Get-WUInstall.ps1" (echo/missing 'scripts\modules\PSWindowsUpdate\Get-WUInstall.ps1' & goto:end)
call:vars settings.ini RemoteIpv4 RemoteIpv4 & if [!RemoteIpv4!]==[] (echo/Check:'RemoteIpv4' not set & goto:end) else (echo/RemoteIpv4:!RemoteIpv4!)
call:vars settings.ini RemoteUser RemoteUser & if [!RemoteUser!]==[] (echo/Check:'RemoteUser' not set & goto:end) else (echo/RemoteUser:!RemoteUser!)
call:vars settings.ini RemotePass RemotePass & if [!RemotePass!]==[] (echo/Check:'RemotePass' not set & goto:end) else (echo/RemotePass:!RemotePass!)
call:vars settings.ini RemotePath RemotePath & if [!RemotePath!]==[] (echo/Check:'RemotePath' not set & goto:end) else (echo/RemotePath:!RemotePath!)
call:vars settings.ini RemoteTime RemoteTime & if [!RemoteTime!]==[] (echo/Check:'RemoteTime' not set & goto:end) else (echo/RemoteTime:!RemoteTime!)
call:vars settings.ini RemoteDate RemoteDate & if [!RemoteDate!]==[] (echo/Check:'RemoteDate' not set & goto:end) else (echo/RemoteDate:!RemoteDate!)
goto:eof
:remote
net use "!RemotePath!" /user:"!RemoteUser!" "!RemotePass!">nul 2>&1
if not exist "!RemotePath!"         (echo/Check:'!RemotePath!' is not accessible & goto:end) else (echo/Check:'!RemotePath!' is accessible)
if not exist "!RemotePath!\scripts" (echo/Check:'!RemotePath!\scripts does not exist') else (echo/Check:Clean up '!RemotePath!\scripts' && rmdir "!RemotePath!\scripts" /s /q>nul 2>&1)
echo/Check:Compressing '%~dp0scripts' & powershell.exe -NoProfile -ExecutionPolicy Bypass -Command "Compress-Archive -Path "%~dp0scripts" -DestinationPath "%~dp0scripts.zip""
echo/Check:Moving '%~dp0scripts.zip' to remote server & move "%~dp0scripts.zip" "!RemotePath!">nul 2>&1
echo/Check:Extracting '!RemotePath!\scripts.zip' & powershell.exe -NoProfile -ExecutionPolicy Bypass -Command "Expand-Archive -LiteralPath "!RemotePath!\scripts.zip" -DestinationPath "!RemotePath!""
echo/Check:Removing '!RemotePath!\scripts.zip' & del "!RemotePath!\scripts.zip" /f /q>nul 2>&1
if not exist "!RemotePath!\scripts\modules\PSWindowsUpdate\Get-WUInstall.ps1" (echo/Check:'!RemotePath!\scripts\modules\PSWindowsUpdate\Get-WUInstall.ps1' is not accessible & goto:end)
echo/Check:Remote System is ready for updates
net use "!RemotePath!" /d>nul 2>&1
goto:eof
:update
echo/Update:Scheduling Windows Updates at '!RemoteTime!' on '!RemoteDate!'
psexec "\\!RemoteIpv4!" -s -u "!RemoteUser!" -p "!RemotePass!" cmd /c "schtasks /delete /f /tn update_and_reboot>nul 2>&1 & schtasks /create /tn update_and_reboot /tr "powershell.exe -NoProfile -ExecutionPolicy Bypass -Command \"Import-Module C:\Windows\Setup\scripts\modules\PSWindowsUpdate;Get-WUInstall -AcceptAll -IgnoreReboot;shutdown -r -t 60;schtasks.exe /delete /f /tn update_and_reboot\"" /sc once /st "!RemoteTime!" /sd "!RemoteDate!" /ru "system""
goto:eof
:vars
set ini_file_to_search_for_variables=%~1& set variable_to_search=%~2& set variable_to_set=%~3
for /f "eol=; eol=[ tokens=1,2* delims==" %%i in ('findstr /b /l /i %variable_to_search%=%ini_file_to_search_for_variables%') do set %variable_to_set%=%%~j
goto:eof
:end
pause
endlocal
exit /b
