407 PowerShell Invoke-RestMethod所需的代理身份验证

发布于 2025-02-04 01:12:15 字数 2651 浏览 7 评论 0原文

我要设置代理,然后使用PowerShell Invoke-restMethod命令对Azure资源进行API呼叫。有时我会遇到以下错误:

远程服务器返回一个错误:(407)所需的代理身份验证。

我尝试了不同的方法:

方法1: 最初,我尝试设置代理设置“自动检测设置:true”,“使用自动配置脚本:true',使用以下代码

$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"

ables2: 设置自动配置脚本',设置自动配置pac Server URL 设置代理设置,然后使用以下代码

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

方法3: 我尝试使用以下代码通过不同的注册表路径设置代理设置:

$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"

但是有时我会遇到代理错误。我无法理解为什么要遇到代理错误,如何解决此问题。有人可以帮忙吗?

注意:我在下面引用了下面的链接,并尝试了所有这些方法: powerShell脚本“和“自动检测设置” <

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文