Forum Discussion
jonnt111325
Jul 25, 2021Copper Contributor
Proxy Authentication
Hi, I am trying to create a PowerShell script to get my proxy ip from ipify.org and save it to a file
I have used their sample code but get the error, 407 Proxy Authentication Required
$ip = Invoke-RestMethod -Uri 'https://api.ipify.org?format=json'
"My public IP address is: $($ip.ip)"
I have looked for ways to do this and found this;
$Wcl=New-Object System.Net.WebClient
$Creds=Get-Credential
$Wcl.Proxy.Credentials=$Creds
However this pops up a credential window and lets me manually enter the username and password. I don't really want this, I would like the username and password typed into the script.
For example;
$ip = Invoke-RestMethod -Uri 'https://USERNAME:PASSWORD@api.ipify.org?format=json'
I know this is not the correct syntax as it gives me an error.
I have my proxy server setup in the Windows 10 settings and it all works
Can someone tell me if this is possible and what the correct syntax is?
Thanks,
jonny
- gastoneBrass Contributor
$headers = @{"X-My-ApiKey"=$apiKey} $contentType = "application/json" $proxyUri = [Uri]$null $proxy = [System.Net.WebRequest]::GetSystemWebProxy() if ($proxy) { $proxy.Credentials = [System.Net.CredentialCache]::DefaultCredentials $proxyUri = $proxy.GetProxy("$server$url") } if ("$proxyUri" -ne "$server$url") { Write-Host "Using proxy: $proxyUri" return Invoke-RestMethod -Uri $server$url -ContentType $contentType -Headers $headers -Method $method -UseDefaultCredentials -Proxy $proxyUri -ProxyUseDefaultCredentials } else { return Invoke-RestMethod -Uri $server$url -ContentType $contentType -Headers $headers -Method $method -UseDefaultCredentials }