Getting Web Proxy Settings


We have a lab of hosts that have proxy exclusion lists set at various times in the lifecycle of the lab, so they have slightly varying values.

We’ll build on the REG_BINARY to [string] trick with this:

function Get-WinHttpProxy {
    param ( [string[]]$computer = ‘.’ );   

    $reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey(‘LocalMachine’, $computer);
    $subKey= “SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings\Connections” -replace ‘\\’, ‘\\’;
    $regKey= $reg.OpenSubKey($subkey);
   
    $proxyServer = $exclusionList = $null;
 
    if ($regKey) {
        $data = $regKey.GetValue(‘WinHttpSettings’);
       
        if (!$data -or $data.Count -le 16) {
            Write-Warning “$computer insufficent proxy data returned”;
        } else {      
            $data1 = $data[16 .. ($data.Count-1)];
            $firstNull = [array]::IndexOf($data1, [byte]0);
 
            if ($firstNull -le 1) {
                Write-Warning “$computer incorrect prxy data returned”;
            } else {
                $proxyServer = [string]::Join(“”,($data1[0 .. ($firstNull-2)]|%{[char][int]$_}));
 
                if ($data1.Count -lt ($firstNull+3)) {
                } else {
                    $exclusionList = [string]::Join(“”,($data1[($firstNull+3) .. ($data1.Count-1)]| %{[char][int]$_}));
                }
            }
        }
    }

   $computer | Select-Object -Property @{
        name = ‘Computer’;
        expression = { $computer; }
    }, @{
        name = ‘ProxyServer’;
        expression = { $proxyServer; }
    }, @{
        name = ‘ExclusionList’;
        expression = { $exclusionList; }
    };
}


Comments (3)

  1. bashUser says:

    Hey!

    NIce script, I used it for my daily uses!

    I also use it to configure it with proxy sites. I get the proxy sites from a proxy list like http://www.AnonTux.com

  2. Curious says:

    So where/how did you figure out the data structure for the REG_BINARY blob stored in WinHttpSettings?

    How did you determine the first byte was useless?

    Or that the 1st byte should never be a null?

    Or that the total array size should be greater than 16?

  3. timdunn says:

    In the intervening years, I've been better about attributing 'deep magic' stuff like that to the proper authors / URLs.  Sadly, this was back when I was using this blog primarily as a personal knowledge base, so the fact that I found it useful was more important than how / where I found it.  My apologies for my poor cross-referencing.

Skip to main content