Skip to content

Instantly share code, notes, and snippets.

@Yvand
Created December 21, 2023 13:28
Show Gist options
  • Save Yvand/c403b946349de1bc64dc0f89658eedd8 to your computer and use it in GitHub Desktop.
Save Yvand/c403b946349de1bc64dc0f89658eedd8 to your computer and use it in GitHub Desktop.
Sets the proxy on a Windows machine
param(
[string]$proxyIp = '10.1.5.4',
[string]$proxyHttpPort = '8080',
[string]$proxyHttpsPort = '8443',
[string]$localDomainFqdn = 'contoso.local'
)
$proxy = 'http={0}:{1};https={0}:{2}' -f $proxyIp, $proxyHttpPort, $proxyHttpsPort
$bypasslist = '*.{0};<local>' -f $localDomainFqdn
# Set WinHTTP proxy
netsh winhttp set proxy proxy-server=$proxy bypass-list=$bypasslist
# Set WinINET proxy, based on https://blog.workinghardinit.work/2020/03/06/configure-wininet-proxy-server-with-powershell/
$proxyEnabled = 1
# Enable machine wide proxy settings (0: per-machine proxy / 1 (or not set): per-user)
New-ItemProperty -Path 'HKLM:\\SOFTWARE\\Policies\\Microsoft\\Windows\\CurrentVersion\\Internet Settings' -Name 'ProxySettingsPerUser' -PropertyType DWORD -Value 0 -Force
$proxyBytes = [system.Text.Encoding]::ASCII.GetBytes($proxy)
$bypassBytes = [system.Text.Encoding]::ASCII.GetBytes($bypasslist)
$defaultConnectionSettings = [byte[]]@(@(70, 0, 0, 0, 0, 0, 0, 0, $proxyEnabled, 0, 0, 0, $proxyBytes.Length, 0, 0, 0) + $proxyBytes + @($bypassBytes.Length, 0, 0, 0) + $bypassBytes + @(1..36 | % { 0 }))
$registryPaths = @('HKLM:\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings', 'HKLM:\\Software\\WOW6432Node\\Microsoft\\Windows\\CurrentVersion\\Internet Settings')
foreach ($registryPath in $registryPaths) {
Set-ItemProperty -Path $registryPath -Name ProxyServer -Value $proxy
Set-ItemProperty -Path $registryPath -Name ProxyEnable -Value $proxyEnabled
Set-ItemProperty -Path $registryPath -Name ProxyOverride -Value $bypasslist
Set-ItemProperty -Path '$registryPath\\Connections' -Name DefaultConnectionSettings -Value $defaultConnectionSettings
}
# Run Bitsadmin to set proxy for localsystem, required for the WS-Management service to actually use the proxy to download the DSC config
Bitsadmin /util /setieproxy localsystem MANUAL_PROXY $proxy $bypasslist
@FlorianHeigl
Copy link

thx for documenting the bit with bitsadmin!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment