How to query the Internet behind a NTLM Proxy on a corporate network
Sometimes at client sites, we need to use PowerShell scripts to invoke REST APIs through a Proxy. If the Proxy required NTLM authentication it can be an utter pain to get this to work correctly.
Invoke-RestMethod has the -Proxy and ProxyCredentials parameters, but you should use this instead:
1
$proxyUri = [System.Uri]"http://10.xx.xx.xxx:8080"
2
$proxy = New-Object System.Net.WebProxy($proxyUri)
3
$proxy.Credentials = New-Object System.Net.NetworkCredential("accountname", "password")
4
5
[System.Net.WebRequest]::DefaultWebProxy = $proxy
6
[System.Net.WebRequest]::DefaultWebProxy.BypassProxyOnLocal = $true
7
8
$result = Invoke-RestMethod -Uri $urlToCall -ContentType "application/json" -Headers $header
Copied!
This should give you the results you need!
Copy link