If you access the Internet in the organization via the proxy server, by default you won’t be able to access an external webpage (Invoke-WebRequest cmdlet), update help using Update-Help cmdlet or download an application package from an external package repository (using PackageManagement or NanoServerPackage) from your PowerShell session. In this article we’ll show how to get the Internet access via proxy server with the authentication from a PowerShell session.
Let’s try to update PowerShell Help:
Or access an external web page:
If you haven’t got a direct Internet connection, the command will return a similar error:
The matter is that PowerShell (to be more precise, .NET class System.Net.WebClient that is using these cmdlets to address the external resources over HTTP) does not use system proxy settings specified in IE. However, WebClient class has some properties that allow to specify both proxy settings (WebClient.Proxy) and proxy authentication data (WebClient.Credentials or WebClient.UseDefaultCredentials). Let’s consider how to use these properties of WebClient class.
Let’s check the current settings of the system proxy in the PowerShell session:
netsh winhttp show proxy
As you can see, proxy settings are not specified
Current WinHTTP proxy settings:
Direct access (no proxy server).
Import the proxy settings from Internet Explorer parameters:
netsh winhttp import proxy source=ie
or set them manually:
netsh winhttp set proxy "192.168.0.14:3128"
If proxy authentication is necessary, the error like “(407) Proxy Authentication Required” will appear when trying to run PoSh commands. Let’s consider two ways of proxy authentication.
If you are signed in using your domain account and your proxy supports NTLM/AD authentication, you can use the credentials of the current user to authenticate on the proxy server (you won’t have to enter your username/password):
$Wcl = new-object System.Net.WebClient
$Wcl.Headers.Add(“user-agent”, “PowerShell Script”)
$Wcl.Proxy.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials
If you have to authenticate on the proxy server manually, run the following commands and specify user name and password in the corresponding window.
Now you can try to access an external website or update the help using Update-Help command.
Everything turned out!