407 proxy authentication required for powershell invoke-restmethod

2.4k Views Asked by At

I'm setting proxy, then making a POST API call to Azure resource using powershell invoke-restmethod command. Sometimes I'm getting the following error:

The remote server returned an error: (407) Proxy Authentication Required.

I tried different approaches as follows:

Approach 1: Initially I tried to set the proxy settings 'Automatically detect settings: true', 'Use automatic configuration script: true',set auto config PAC server URL using the following code

$key = "HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings\Connections"
$data = (Get-ItemProperty -Path $key -Name DefaultConnectionSettings).DefaultConnectionSettings
$data[8] = 0x0d
Set-ItemProperty -Path $key -Name DefaultConnectionSettings -Value $data
#$key = "HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings"
set-itemproperty -path $key -Name AutoConfigURL -value "http://PacServerURL.pac"

Approach2: Set the proxy settings, then I refreshed the registry key settings using following code

function Update-System
{
  $signature = @'
[DllImport("wininet.dll", SetLastError = true, CharSet=CharSet.Auto)]
public static extern bool InternetSetOption(IntPtr hInternet, int dwOption, IntPtr lpBuffer, int dwBufferLength);
'@

$INTERNET_OPTION_SETTINGS_CHANGED   = 39
$INTERNET_OPTION_REFRESH            = 37
$type = Add-Type -MemberDefinition $signature -Name wininet -Namespace pinvoke -PassThru
$a = $type::InternetSetOption(0, $INTERNET_OPTION_SETTINGS_CHANGED, 0, 0)
$b = $type::InternetSetOption(0, $INTERNET_OPTION_REFRESH, 0, 0)
return $a -and $b
}
$key = "HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings\Connections"
$data = (Get-ItemProperty -Path $key -Name DefaultConnectionSettings).DefaultConnectionSettings
$data[8] = 0x0d
Set-ItemProperty -Path $key -Name DefaultConnectionSettings -Value $data
#$key = "HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings"
set-itemproperty -path $key -Name AutoConfigURL -value "http://PacServerURL.pac"
Update-System

Approach 3: I tried to set proxy settings via different registry paths using below code:

$key = "Registry::HKEY_USERS\<UserSID>\Software\Microsoft\Windows\CurrentVersion\Internet Settings\Connections"
$data = (Get-ItemProperty -Path $key -Name DefaultConnectionSettings).DefaultConnectionSettings
$data[8] = 0x0d
Set-ItemProperty -Path $key -Name DefaultConnectionSettings -Value $data
#$key = "Registry::HKEY_USERS\<UserSID>\Software\Microsoft\Windows\CurrentVersion\Internet Settings"
set-itemproperty -path $key -Name AutoConfigURL -value "http://PacServerURL.pac"

But still sometimes I get proxy error. I'm not able to understand why I'm getting the proxy error, how to resolve this. Can anyone please help?

Note: I referred below links and tried all these approaches: PowerShell script to tick proxy settings : "Use automatic configuration script" AND "Automatically detect settings" IE Enable/Disable Proxy Settings via Registry

0

There are 0 best solutions below