А АSaturday, 25 November 2017

Using PowerShell behind a Proxy.

Hi everybody.

Today I would like to point that PowerShell after version 3 has some useful  commandlets concerning Web.

As simple task, you try to load some resource from the web in PowerShell, e.g., with one of the following two commandlets:

PS> Invoke-WebRequest http://example.org
PS> Update-Help

However, you get an “access denied” or “no network connection” error message there may be a web proxy in your company  blocking your way, demanding authentication.

What needs to be done?

You have to configure PowerShell’s proxy settings. Some commandlets like Invoke-WebRequest offer options that let you set the proxy for just one command:

PS> Invoke-WebRequest -Proxy http://proxy.example.net:8080  -ProxyUseDefaultCredentials  http://example.org

For commandlets that don’t have such options (like Update-Help), or if you’d rather set the proxy for the whole PowerShell session, issue the following command:

PS> (New-Object System.Net.WebClient).Proxy.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials

All commands after that will implicitly use the system proxy, so you can issue commands like the above Invoke-WebRequest http://example.org or Update-Help and they will “just work”.

There some examples are the following by Proxy:

#1:

$secPasswd=ConvertTo-SecureString "Password" -AsPlainText -Force
$myCreds=New-Object System.Management.Automation.PSCredential -ArgumentList "Domain\name",$secPasswd
$Site="http://nyuk.narod.ru"
$Test=Invoke-WebRequest -URI $Site -Proxy "http://myproxy.skyfleet.com:8080" -ProxyCredential $mycreds
$Test.Links

#2:

PS C:\Users\Admin> $w = Invoke-WebRequest -Proxy http://myproxy.skyfleet.com:8080 -ProxyUseDefaultCredentials http://nyuk.narod.ru

PS C:\Users\Admin> $w.links | fl innerText, href

innerText :
href      : http://www.peresvetmed.ru
innerText :
href      : http://flasher911.narod.ru/demo5/
innerText :
href      : http://www.medinfo.dp.ua
innerText :
href      : http://nyukers.ucoz.net
innerText : MedixWare 2000 Ultrasound®:
href      : index4.html
innerText : Nyukers® Network Suite - швейцарский нож для админа!
href      : http://nyuk.narod.ru/soft.html
...
and so on.

#3:

# Create the Web Client object
$Client = New-Object -TypeName System.Net.WebClient

# Tell it to use our default creds for the proxy
$Client.Proxy.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials

# Now we try to run our command, in this case, downloading a web page.
$Client.DownloadString("http://nyuk.narod.ru")

#4:

$Client = New-Object -TypeName System.Net.WebClient
 $proxy = New-Object -TypeName System.Net.WebProxy

$proxy.Address = [uri]"http://myproxy.skyfleet.com:8080"
 $proxy.Credentials = (new-object System.Net.NetworkCredential("Username", "Password", "Domain"))

$Client.Proxy = $proxy

$Client.DownloadString(http://nyuk.narod.ru)


#5: (for session login have to type online)
$c = Get-Credential
$web = New-Object System.Net.WebClient
$web.Proxy.Credentials = $c
update-help -force



See you later.

3 comments:

Unknown said...

Спасибо тебе добрый человек, хоть кто-то нормально показал все варианты работы с проксёй!

Unknown said...

Спасибо тебе добрый человек, хоть кто-то нормально показал все варианты работы с проксёй!

Anonymous said...

For user PS profile:
[system.net.webrequest]::defaultwebproxy = new-object system.net.webproxy('http://my-proxy.forza.com:9090')
[system.net.webrequest]::defaultwebproxy.credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials
[System.Net.WebRequest]::DefaultWebProxy.BypassProxyOnLocal = $true

Post a Comment

А что вы думаете по этому поводу?

Версия на печать

Популярное