当前位置:  开发笔记 > 开发工具 > 正文

PowerShell,Web请求和代理

如何解决《PowerShell,Web请求和代理》经验,为你挑选了4个好方法。

在制作简单的Web请求时,有没有办法告诉PowerShell环境只使用Internet Explorer的代理设置?

我的代理设置由网络策略(或脚本)控制,如果不必要,我不希望以后修改ps脚本.

更新: 来自参与者的精彩信息.我将用于此的最终脚本模板将如下所示:

$proxyAddr = (get-itemproperty 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings').ProxyServer
$proxy = new-object System.Net.WebProxy
$proxy.Address = $proxyAddr
$proxy.useDefaultCredentials = $true

$url = "http://stackoverflow.com"
$wc = new-object system.net.WebClient
$wc.proxy = $proxy
$webpage = $wc.DownloadData($url)
$str = [System.Text.Encoding]::ASCII.GetString($webpage)
Write-Host $str

Paul Moore.. 22

以下更好一点,它也处理自动检测到的代理:

$proxy = [System.Net.WebRequest]::GetSystemWebProxy()
$proxy.Credentials = [System.Net.CredentialCache]::DefaultCredentials

$wc = new-object system.net.WebClient
$wc.proxy = $proxy
$webpage = $wc.DownloadData($url)

(编辑)除此之外,这个定义似乎对我也很好:

function Get-Webclient {
    $wc = New-Object Net.WebClient
    $wc.UseDefaultCredentials = $true
    $wc.Proxy.Credentials = $wc.Credentials
    $wc
}


Mitch Wheat.. 13

未经测试:

$user = $env:username
$webproxy = (get-itemproperty 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet
Settings').ProxyServer
$pwd = Read-Host "Password?" -assecurestring

$proxy = new-object System.Net.WebProxy
$proxy.Address = $webproxy
$account = new-object System.Net.NetworkCredential($user,[Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($pwd)), "")
$proxy.credentials = $account

$url = "http://stackoverflow.com"
$wc = new-object system.net.WebClient
$wc.proxy = $proxy
$webpage = $wc.DownloadData($url)
$string = [System.Text.Encoding]::ASCII.GetString($webpage)

...



1> Paul Moore..:

以下更好一点,它也处理自动检测到的代理:

$proxy = [System.Net.WebRequest]::GetSystemWebProxy()
$proxy.Credentials = [System.Net.CredentialCache]::DefaultCredentials

$wc = new-object system.net.WebClient
$wc.proxy = $proxy
$webpage = $wc.DownloadData($url)

(编辑)除此之外,这个定义似乎对我也很好:

function Get-Webclient {
    $wc = New-Object Net.WebClient
    $wc.UseDefaultCredentials = $true
    $wc.Proxy.Credentials = $wc.Credentials
    $wc
}



2> Mitch Wheat..:

未经测试:

$user = $env:username
$webproxy = (get-itemproperty 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet
Settings').ProxyServer
$pwd = Read-Host "Password?" -assecurestring

$proxy = new-object System.Net.WebProxy
$proxy.Address = $webproxy
$account = new-object System.Net.NetworkCredential($user,[Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($pwd)), "")
$proxy.credentials = $account

$url = "http://stackoverflow.com"
$wc = new-object system.net.WebClient
$wc.proxy = $proxy
$webpage = $wc.DownloadData($url)
$string = [System.Text.Encoding]::ASCII.GetString($webpage)

...



3> Shay Levy..:
$proxy = New-Object System.Net.WebProxy("http://yourProxy:8080")
$proxy.useDefaultCredentials = $true
$wc = new-object system.net.webclient
$wc.proxy = $proxy
$wc.downloadString($url)



4> Bewc..:

这比原始问题要晚得多,但对于PowerShell的更高版本仍然是一个相关的答案.从v3开始,我们有两个可以解决此问题的项目:

Invoke-WebRequest - 几乎在每个场景中都使用system.net.webclient替换

$ PSDefaultParameterValues - 可以存储参数的详细信息

如何一起使用它们来解决由网络策略(或脚本)控制的代理设置的原始问题,而不必在以后修改ps脚本?

Invoke-WebRequest附带-Proxy和-ProxyUseDefaultCredentials参数.

我们将这些参数的答案存储在$ PSDefaultParameterValues中,如下所示: $PSDefaultParameterValues.Add('Invoke-WebRequest:Proxy','http://###.###.###.###:80') $PSDefaultParameterValues.Add('Invoke-WebRequest:ProxyUseDefaultCredentials',$true)

您可以使用$ proxyAddr 替换' http://#######.###.###:80 '.您选择存储的范围是什么.我将它们放入我的$ profile中,所以我再也不必在脚本中设置这些项目了.

希望这有助于某人!

推荐阅读
虎仔球妈_459
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有