mardi 21 mai 2019

How to disable or override a web proxy from a Powershell script without restarting the browser?

I'd like to navigate to an IP address on a browser from a PowerShell script, but that action is blocked by a web proxy. I'd like to find a way to avoid the proxy, and also to not change its configuration after I'm done with my script.

Thus, I tried disabling the web proxy before running my script and enabling it again afterwards. From this source http://learnpowershellwithme.blogspot.com/2017/07/enabledisable-proxy-settings-via.html I tried the following codes:

Function Set-InternetProxy
{
    [CmdletBinding()]
    Param(

        [Parameter(Mandatory=$True,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
        [String[]]$Proxy,

        [Parameter(Mandatory=$False,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
        [AllowEmptyString()]
        [String[]]$acs

    )

    Begin
    {

            $regKey="HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings"

    }

    Process
    {

        Set-ItemProperty -path $regKey ProxyEnable -value 1

        Set-ItemProperty -path $regKey ProxyServer -value $proxy

        if($acs) 
        {            

                 Set-ItemProperty -path $regKey AutoConfigURL -Value $acs          
        }

    } 

    End
    {

        Write-Output "Proxy is now enabled"

        Write-Output "Proxy Server : $proxy"

        if ($acs)
        {

            Write-Output "Automatic Configuration Script : $acs"

        }
        else
        {

            Write-Output "Automatic Configuration Script : Not Defined"

        }
    }
}

Disable-InternetProxy 

Function Disable-InternetProxy
{
  Begin
    {

            $regKey="HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings"

    }

    Process
    {

        Set-ItemProperty -path $regKey ProxyEnable -value 0 -ErrorAction Stop

        Set-ItemProperty -path $regKey ProxyServer -value "" -ErrorAction Stop

        Set-ItemProperty -path $regKey AutoConfigURL -Value "" -ErrorAction Stop          

    } 

    End
    {

        Write-Output "Proxy is now Disabled"

    }

}

When I call the disable function, disabling the proxy only works if I manually close and reopen the browser. That is problematic to me as I want it all to be automated. Furthermore, when I enable the proxy again, the automatic configuration script remains unchecked. This is how I try to re-enable the proxy:

Set-InternetProxy -Proxy https://myproxy/pac.pac -acs https://myproxy/pac.pac

I then tried to override the proxy to avoid that problem, by typing the following code:

$regKey="HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings"
Set-ItemProperty -path $regKey ProxyOverride -value 'http://165.100.100.10/'

However it still does not work. How can I properly avoid the web proxy from my script?




Aucun commentaire:

Enregistrer un commentaire