This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
microsoft:powershell_proxy [2019/02/26 09:54] admin created |
microsoft:powershell_proxy [2019/02/26 10:20] (current) admin |
||
---|---|---|---|
Line 1: | Line 1: | ||
====== Using PowerShell Behind a Proxy ====== | ====== Using PowerShell Behind a Proxy ====== | ||
+ | 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: | ||
+ | <code winbatch> | ||
+ | Update-Help | ||
+ | </code> | ||
+ | |||
+ | Or access an external web page: | ||
+ | |||
+ | <code winbatch> | ||
+ | Invoke-WebRequest http://woshub.com | ||
+ | </code> | ||
+ | If you haven’t got a direct Internet connection, the command will return a similar error: | ||
+ | |||
+ | <note warning>update-help : Failed to update Help for the module(s) ‘DhcpServer, DirectAccessClientComponents….’ with UI culture(s) {en-US} : Unable to connect to Help content. The server on which Help content is stored might not be available. Verify that the server is available, or wait until the server is back online, and then try the command again.</note> | ||
+ | |||
+ | <note warning>Invoke-WebRequest: Unable to connect to the remote server</note> | ||
+ | |||
+ | {{ :microsoft:powershel_proxy_01.jpg?direct&800 |}} | ||
+ | |||
+ | 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: | ||
+ | |||
+ | <code winbatch> | ||
+ | netsh winhttp show proxy | ||
+ | </code> | ||
+ | |||
+ | As you can see, proxy settings are not specified | ||
+ | |||
+ | <code winbatch> | ||
+ | Current WinHTTP proxy settings: | ||
+ | Direct access (no proxy server). | ||
+ | </code> | ||
+ | |||
+ | {{ :microsoft:powershel_proxy_02.jpg?direct&800 |}} | ||
+ | |||
+ | Import the proxy settings from Internet Explorer parameters: | ||
+ | |||
+ | <code winbatch> | ||
+ | netsh winhttp import proxy source=ie | ||
+ | </code> | ||
+ | |||
+ | or set them manually: | ||
+ | |||
+ | <code winbatch> | ||
+ | netsh winhttp set proxy "192.168.0.14:3128" | ||
+ | </code> | ||
+ | |||
+ | {{ :microsoft:powershel_proxy_03.jpg?direct&800 |}} | ||
+ | |||
+ | 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): | ||
+ | |||
+ | <code winbatch> | ||
+ | $Wcl = new-object System.Net.WebClient | ||
+ | $Wcl.Headers.Add(“user-agent”, “PowerShell Script”) | ||
+ | $Wcl.Proxy.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials | ||
+ | </code> | ||
+ | |||
+ | If you have to authenticate on the proxy server manually, run the following commands and specify user name and password in the corresponding window. | ||
+ | |||
+ | <code winbatch> | ||
+ | $Wcl=New-Object System.Net.WebClient | ||
+ | $Creds=Get-Credential | ||
+ | $Wcl.Proxy.Credentials=$Creds | ||
+ | </code> | ||
+ | |||
+ | {{ :microsoft:powershel_proxy_04.jpg?direct&800 |}} | ||
+ | |||
+ | Now you can try to access an external website or update the help using Update-Help command. | ||
+ | |||
+ | {{ :microsoft:powershel_proxy_05.jpg?direct&800 |}} | ||
+ | |||
+ | Everything turned out! |