Access web using Powershell and Proxy

2019-01-31 08:50发布

I can't seem to get access to a webpage using Powershell. I keep getting a "(407) Proxy Authentication Required". I've tried many things. I don't know what the proxy is or what kind of authentication it requires. The only thing I have access to is in IE it uses a script for configuring. I tried using some IPs from that, but no luck. Any ideas?

Here is one example of what I tried:

$wc = New-Object System.Net.WebClient
$wc.Headers.Add("User-Agent","Mozilla/4.0+")        
$wc.Proxy = [System.Net.WebRequest]::DefaultWebProxy
$wc.Proxy.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials
$wc.DownloadString("http://stackoverflow.com")

6条回答
Rolldiameter
2楼-- · 2019-01-31 09:17

If you use the following you'll receive a prompt to enter your credentials:

$client.Credentials = Get-Credential
查看更多
虎瘦雄心在
3楼-- · 2019-01-31 09:21

I had a similar issue and resolved it with just two lines of powershell:

$browser = New-Object System.Net.WebClient
$browser.Proxy.Credentials =[System.Net.CredentialCache]::DefaultNetworkCredentials 

Hope this helps.

查看更多
干净又极端
4楼-- · 2019-01-31 09:22

try adding cache credentials....

$domain = 'ChDom'
$Client = new-object System.Net.WebClient
$cc = New-object System.Net.CredentialCache
$urlObj = New-object System.Uri($url)

#these variables must be plaintext strings
$creds = New-object System.Net.NetworkCredential($Username,$Password)

#your auth might be different than mine
$cc.add($urlobj,"NTLM",$creds)
$client.Credentials = $cc
$Client.Downloadfile($url, C:\Temp\TestPage.html)
查看更多
小情绪 Triste *
5楼-- · 2019-01-31 09:23

If you know the script - just download it, open with Notepad and find IP and port of your proxy server. As for authentication - most probably your windows credentials are used, so in theory you should be able to keep it empty, unless there's something suspicious in the script.

查看更多
叼着烟拽天下
6楼-- · 2019-01-31 09:30

I haven't seen anyone doing something like this but there is a way to do this as a "global setting" in your Powershell script (I remember doing this in C# before for local dev builds).

[System.Net.WebRequest]::DefaultWebProxy = [System.Net.WebRequest]::GetSystemWebProxy()
[System.Net.WebRequest]::DefaultWebProxy.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials

This way if you don't want to update all your WebClients with proxy details, you can just override the global setting (have to be done every time you run the script). But this assumes that the current logged in Windows user is valid for the system-defined proxy server.

NOTE: I would say that this is only useful as a quick and dirty way to get a PS script working that wasn't proxy aware before (like Cake build).

查看更多
Ridiculous、
7楼-- · 2019-01-31 09:35

If the proxy answers "407", "proxy authentication required", then the authentication is required:

$Username="Hugo"
$Password="abcdefgh"
$WebProxy = New-Object System.Net.WebProxy("http://webproxy:8080",$true)
$url="http://aaa.bbb.ccc.ffffd/rss.xml"

$WebClient = New-Object net.webclient

$WebClient.Proxy=$webproxy
$WebClient.proxy.Credentials = New-Object System.Net.NetworkCredential($Username, $Password)
$path="C:\Users\hugo\xml\test.xml"
$WebClient.DownloadFile($url, $path)

Content now resides in "test.xml"

查看更多
登录 后发表回答