Script to traverse proxy and download/install updates.

I’ve been trying to write a script which does the following.

-Disables two scheduled tasks.
-Enables proxy server settings.
-Downloads and install updates using the PSWindowsUpdate module found here.
https://gallery.technet.microsoft.com/scriptcenter/2d191bcd-3308-4edd-9de2-88dff796b0bc
-List all updates installed over the last 7 days.
-Re-enables to the two scheduled tasks.
-Disables the proxy server settings.

Although I can’t get the script to work. Basically the portion of the script which enables the proxy works, although by the time the portion of the script runs which needs to go out to the MS update servers, it as though it doesn’t realise it can go through the proxy.

Whats even more strange, is that if I split this in to two scripts and run the first script (where the proxy is enabled), open Internet Explorer and then run the seconds script (where the updates are installed, the scheduled tasks are re-enabled and the proxy deisabled), it works.

Likewise, if I just run the script which enables the proxy server and then launch IE, I can see that I have outbound internet access via the proxy.

I have also tried running the scripts mentioned in two seperate scheuled tasks, but still doesn’t download updates

The script is as follows.

Import-Module PSWindowsUpdate -ErrorAction Stop

schtasks.exe /CHANGE /TN "ParcelForce_Send" /DISABLE
schtasks.exe /CHANGE /TN "ParcelForce_Receive" /DISABLE

$LastSevenDays = (Get-Date).AddDays(-7).ToString('MM-dd-yyyy')
$RegKey = "HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings"
Set-ItemProperty -Path $RegKey ProxyEnable -Value 1 -Verbose -ErrorAction Stop
Set-ItemProperty -Path $RegKey ProxyServer -Value "xx:xx:xx:xx:3128" -verbose -ErrorAction Stop
Get-ItemProperty -Path $RegKey | Format-Table ProxyEnable, ProxyServer -AutoSize
Start-Sleep -Seconds 10

Get-WUInstall -WindowsUpdate -MicrosoftUpdate -AcceptAll -IgnoreReboot -Verbose

Get-WUHistory | Where-Object {$_.Date.ToString('MM-dd-yyyy') -ge $LastSevenDays} | Format-Table Date, KB, Title -AutoSize

Set-ItemProperty -Path $RegKey ProxyServer -Value ":" -verbose -ErrorAction Stop
Set-ItemProperty -Path $RegKey ProxyEnable -Value 0 -Verbose -ErrorAction Stop

schtasks.exe /CHANGE /TN "ParcelForce_Send" /ENABLE 
schtasks.exe /CHANGE /TN "ParcelForce_Receive" /ENABLE 

I’m sure that I’m missing something glaringly obvious, but can’t put my finger on what it is, can anyone advise?

Right, reading back through my question, I think I;ve answered it myself…

This seems to work.

Import-Module PSWindowsUpdate -ErrorAction Stop

schtasks.exe /CHANGE /TN "ParcelForce_Send" /DISABLE
schtasks.exe /CHANGE /TN "ParcelForce_Receive" /DISABLE

$IE = "IExplore.exe"
$LastSevenDays = (Get-Date).AddDays(-7).ToString('MM-dd-yyyy')
$RegKey = "HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings"
Set-ItemProperty -Path $RegKey ProxyEnable -Value 1 -Verbose -ErrorAction Stop
Set-ItemProperty -Path $RegKey ProxyServer -Value "1xx:xx:xx:xx:3128" -verbose -ErrorAction Stop
Get-ItemProperty -Path $RegKey | Format-Table ProxyEnable, ProxyServer -AutoSize
Start-Sleep -Seconds 10

Start-Process $IE

Start-Sleep -Seconds 15

Get-WUInstall -WindowsUpdate -MicrosoftUpdate -AcceptAll -IgnoreReboot -Verbose

Get-WUHistory | Where-Object {$_.Date.ToString('MM-dd-yyyy') -ge $LastSevenDays} | Format-Table Date, KB, Title -AutoSize

Set-ItemProperty -Path $RegKey ProxyServer -Value ":" -verbose -ErrorAction Stop
Set-ItemProperty -Path $RegKey ProxyEnable -Value 0 -Verbose -ErrorAction Stop

schtasks.exe /CHANGE /TN "ParcelForce_Send" /ENABLE 
schtasks.exe /CHANGE /TN "ParcelForce_Receive" /ENABLE 

Stop-Process -Name 'IExplore'

Launching IE prior to checking seems to forces PS to aknowledge further that the proxy is in fact enabled. Not sure why it needs to do this through as the sanity check of ‘Get-ItemProperty -Path $RegKey | Format-Table ProxyEnable, ProxyServer -AutoSize’ should do this in the first instance.