Proxy Authentication

%3CLINGO-SUB%20id%3D%22lingo-sub-2584890%22%20slang%3D%22en-US%22%3EProxy%20Authentication%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2584890%22%20slang%3D%22en-US%22%3E%3CP%3EHi%2C%20I%20am%20trying%20to%20create%20a%20PowerShell%20script%20to%20get%20my%20proxy%20ip%20from%20ipify.org%20and%20save%20it%20to%20a%20file%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI%20have%20used%20their%20sample%20code%20but%20get%20the%20error%2C%20407%20Proxy%20Authentication%20Required%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3E%24ip%20%3D%20Invoke-RestMethod%20-Uri%20'https%3A%2F%2Fapi.ipify.org%3Fformat%3Djson'%0A%22My%20public%20IP%20address%20is%3A%20%24(%24ip.ip)%22%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%26nbsp%3B%3C%2FP%3E%3CP%3EI%20have%20looked%20for%20ways%20to%20do%20this%20and%20found%20this%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-powershell%22%3E%3CCODE%3E%24Wcl%3DNew-Object%20System.Net.WebClient%0A%24Creds%3DGet-Credential%0A%24Wcl.Proxy.Credentials%3D%24Creds%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%3EHowever%20this%20pops%20up%20a%20credential%20window%20and%20lets%26nbsp%3B%20me%20manually%20enter%20the%20username%20and%20password.%20I%20don't%20really%20want%20this%2C%20I%20would%20like%20the%20username%20and%20password%20typed%20into%20the%20script.%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%3EFor%20example%3B%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3E%24ip%20%3D%20Invoke-RestMethod%20-Uri%20'https%3A%2F%2FUSERNAME%3APASSWORD%40api.ipify.org%3Fformat%3Djson'%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%3EI%20know%20this%20is%20not%20the%20correct%20syntax%20as%20it%20gives%20me%20an%20error.%26nbsp%3B%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%3EI%20have%20my%20proxy%20server%20setup%20in%20the%20Windows%2010%20settings%20and%20it%20all%20works%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%3ECan%20someone%20tell%20me%20if%20this%20is%20possible%20and%20what%20the%20correct%20syntax%20is%3F%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%3CSPAN%3EThanks%2C%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%3CSPAN%3Ejonny%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-2584890%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EProxy%20Authentication%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-2587947%22%20slang%3D%22en-US%22%3ERe%3A%20Proxy%20Authentication%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-2587947%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F1112160%22%20target%3D%22_blank%22%3E%40jonnt111325%3C%2FA%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Efrom%20%3CA%20href%3D%22https%3A%2F%2Fstackoverflow.com%2Fquestions%2F20471486%2Fhow-can-i-make-invoke-restmethod-use-the-default-web-proxy%22%20target%3D%22_self%22%20rel%3D%22nofollow%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fstackoverflow.com%2Fquestions%2F20471486%2Fhow-can-i-make-invoke-restmethod-use-the-default-web-proxy%3C%2FA%3E%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3E%24headers%20%3D%20%40%7B%22X-My-ApiKey%22%3D%24apiKey%7D%0A%24contentType%20%3D%20%22application%2Fjson%22%0A%0A%24proxyUri%20%3D%20%5BUri%5D%24null%0A%24proxy%20%3D%20%5BSystem.Net.WebRequest%5D%3A%3AGetSystemWebProxy()%0Aif%20(%24proxy)%0A%7B%0A%20%20%20%20%24proxy.Credentials%20%3D%20%5BSystem.Net.CredentialCache%5D%3A%3ADefaultCredentials%0A%20%20%20%20%24proxyUri%20%3D%20%24proxy.GetProxy(%22%24server%24url%22)%0A%7D%0A%0Aif%20(%22%24proxyUri%22%20-ne%20%22%24server%24url%22)%0A%7B%0A%20%20%20%20Write-Host%20%22Using%20proxy%3A%20%24proxyUri%22%0A%20%20%20%20return%20Invoke-RestMethod%20-Uri%20%24server%24url%20-ContentType%20%24contentType%20-Headers%20%24headers%20-Method%20%24method%20-UseDefaultCredentials%20-Proxy%20%24proxyUri%20-ProxyUseDefaultCredentials%0A%7D%0Aelse%0A%7B%0A%20%20%20%20return%20Invoke-RestMethod%20-Uri%20%24server%24url%20-ContentType%20%24contentType%20-Headers%20%24headers%20-Method%20%24method%20-UseDefaultCredentials%0A%7D%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
Occasional Visitor

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

1 Reply

@jonnt111325 

 

from https://stackoverflow.com/questions/20471486/how-can-i-make-invoke-restmethod-use-the-default-web-pr...

$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
}