PowerShell, Solicitudes web y Proxies


Al hacer una simple solicitud web ¿hay alguna forma de decirle al entorno de PowerShell que simplemente use la configuración del proxy de Internet Explorer?

Mi configuración de proxy está controlada por una política de red(o script) y no quiero tener que modificar los scripts de ps más adelante si no tengo que hacerlo.

ACTUALIZACIÓN: Gran información de los participantes. La plantilla de script final que usaré para esto se verá algo como lo siguiente:

$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
Author: Community, 2009-02-21

5 answers

Untested :

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

...

 12
Author: Mitch Wheat,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2009-02-20 22:37:23

Algo mejor es lo siguiente, que también maneja proxies auto-detectados:

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

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

(editar) Además de lo anterior, esta definición parece funcionar bien para mí también:

function Get-Webclient {
    $wc = New-Object Net.WebClient
    $wc.UseDefaultCredentials = $true
    $wc.Proxy.Credentials = $wc.Credentials
    $wc
}
 19
Author: Paul Moore,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-02-15 09:59:46
$proxy = New-Object System.Net.WebProxy("http://yourProxy:8080")
$proxy.useDefaultCredentials = $true
$wc = new-object system.net.webclient
$wc.proxy = $proxy
$wc.downloadString($url)
 7
Author: Shay Levy,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-12-07 18:09:18

Esto es mucho más tarde que la pregunta original, pero sigue siendo una respuesta relevante para versiones posteriores de PowerShell. A partir de la v3, tenemos dos elementos que pueden abordar esto:

Invoke-WebRequest - que reemplaza el uso de system.net. webclient para casi todos los escenarios

$PSDefaultParameterValues - que puede almacenar detalles para los parámetros

Cómo usarlos juntos para resolver el problema original de la configuración de proxy controlada por una política de red(o script) ¿y no tener que modificar los scripts de ps más adelante?

Invoke-WebRequest viene con los parámetros-Proxy y-ProxyUseDefaultCredentials.

Almacenamos nuestras respuestas a estos parámetros en PS PSDefaultParameterValues, así: $PSDefaultParameterValues.Add('Invoke-WebRequest:Proxy','http://###.###.###.###:80') $PSDefaultParameterValues.Add('Invoke-WebRequest:ProxyUseDefaultCredentials',$true)

Puede reemplazar 'http://###.###.###.###: 80 ' con pro proxyAddr como quieras. En qué ámbito eliges almacenar esto, es tu elección. Los puse en mi profile perfil, así que nunca más tendré que configurar estos elementos en mis scripts.

Esperanza esto ayuda a alguien!

 6
Author: Bewc,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-05-08 15:54:19

Simplemente actualice la URL con su propia dirección proxy:port. Habilita PowerShellGet para ir más allá del proxy usando sus credenciales locales. Si no tiene requisitos de credenciales, simplemente haga clic en Aceptar cuando se le solicite su contraseña. Cambié el nombre de esa caja a "Cerrar esta ventana". También puede utilizar otros gestores de paquetes como Chocolatey / Nuget a través del proxy debido a este script.

 -1
Author: Rene Abrego,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-07-18 07:45:59