Proxy Authentication

Copper Contributor

Hi, I am trying to create a PowerShell script to get my proxy ip from and save it to a file


I have used their sample code but get the error, 407 Proxy Authentication Required





$ip = Invoke-RestMethod -Uri ''
"My public IP address is: $($ip.ip)"






I have looked for ways to do this and found this;





$Wcl=New-Object System.Net.WebClient






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 ''






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?




1 Reply




$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
    return Invoke-RestMethod -Uri $server$url -ContentType $contentType -Headers $headers -Method $method -UseDefaultCredentials