PowerShell cmdlet with proxy? - Well, this is for Windows User…
Sometimes you will need to work in a company that has a Proxy Server configured for security reason… I know it’s bit annoying.
Especially when I’m trying to connect my Azure Account:
1
| Add-AzureAccount -Credential (Get-Credential)
|
Then I got:
1
2
3
4
5
6
7
8
9
| PS C:\Users\ryang> Add-AzureAccount -Credential (Get-Credential)
cmdlet Get-Credential at command pipeline position 1
Supply values for the following parameters:
Add-AzureAccount : user_realm_discovery_failed: User realm discovery failed: The remote server returned an error: (407) Proxy Authentication Required.
At line:1 char:1
+ Add-AzureAccount -Credential (Get-Credential)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : CloseError: (:) [Add-AzureAccount], AadAuthenticationFailedException
+ FullyQualifiedErrorId : Microsoft.WindowsAzure.Commands.Profile.AddAzureAccount
|
Proxy Authentication… ah…
By checking the winHTTP settings:
1
2
3
| PS C:\Users\ryang> netsh winhttp show proxy
Current WinHTTP proxy settings:
Direct access (no proxy server).
|
All we need to do is:
- Tell PowerShell using the same Proxy Setting as IE
- Pass my current Credential to Proxy
Firstly…
1
| netsh winhttp import proxy source=ie
|
Then with the Credential…
1
2
| $webclient=New-Object System.Net.WebClient
$webclient.Proxy.Credentials= Get-Credential
|
Try it again….