Problems with PowerShell script on Invoke-WebRequest for HEnet IPv6 DDNS

sebholz 1 Reputation point
2022-07-21T01:49:52.427+00:00

Hi, I'm trying to develope a PowerShell script for a dynamic update service on Hurricane Electric service (aka HEnet) on IPv6 AAAA records. I have problems with the Invoke-WebRequest when applying the update, don't know what I'm doing wrong here.
This is my code:

   # Set static content  
   $myPass = "thepassword"  
   $myHost = "thedyn.domain.com"  
   $myWan = "Ethernet"  
     
   #Write-Eventlog  -Logname 'Application' -Source 'HEnet Updater' -EventID 666 -EntryType Information -Message "Starting…"  
     
   # Fetch external IP  
   Write-Host "Fetching external IP..."  
   $response = Get-NetIPAddress -AddressFamily IPv6 -InterfaceAlias "$myWan" -SuffixOrigin "Link" -PrefixOrigin "RouterAdvertisement"  
   $myIP = $response.IPAddress.Trim()  
     
   Write-Host "Value $myIP found, validating..."  
     
   # Validating IP  
   $IPCheck = [bool]("$myIP" -as [ipaddress])  
   Write-Host "Validation result: $IPCheck $myIP"  
     
   If ($IPCheck -eq $false)  
   {  
   Write-Host "Failed to get external IP. Trying with external method."  
     
   $myIP = (Invoke-WebRequest -Uri "http://checkipv6.dyndns.com").Content -replace "(<html><head><title>Current IP Check</title></head><body>Current IP Address: )|(</body></html>)|",""  
   Write-Host "Value $myIP found, assuming as external IP."  
     
   Exit  
   }  
     
   Write-Host "External IP: $myIP"  
     
   # Build URL for update  
   $URL = "http://${myHost}:${myPass}@dyn.dns.he.net/nic/update?hostname=$myHost&myip=$myIP"  
     
   # Print output  
   Write-Host "Updating host $myHost with IP $myIP"  
     
   # Updated  
   $update = Invoke-WebRequest $URL  
     
   # Write to EventLog  
   $strToLog = "Error returned: $update'r'nFull HTTPS string used: $URL"  
   Write-Host "Writing to log: $strToLog"  
   #Write-Eventlog  -Logname 'Application' -Source 'No-IP Updater' -EventID 666 -EntryType Information -Message $strToLog  
Windows for business Windows Server User experience PowerShell
0 comments No comments
{count} votes

5 answers

Sort by: Most helpful
  1. MotoX80 36,291 Reputation points
    2022-07-21T11:49:50.777+00:00

    What error are you getting? What response do you get from the web site?

    Is $myIP correct? Display the contents of the $url variable before you call invoke-webrequest. Display the contents of the $update variable after the call.

    0 comments No comments

  2. sebholz 1 Reputation point
    2022-07-21T15:46:36.973+00:00

    Hi @MotoX80 , thanks for answering!

    Yes $myIP is correct, its a IPv6 address and the outpu in this case is 2xxx:xxxx:xxxx:xxxx:b4e7:4193:4d0c:914b also the script does run a validation on:
    $IPCheck = [bool]("$myIP" -as [ipaddress])
    Write-Host "Validation result: $IPCheck $myIP"

    The $url content is: http://thedyn.domain.com:thepassword[@](/users/na/?userId=9caf6c8e-0000-0003-0000-000000000000).dns.he.net/nic/update?hostname=thedyn.domain.com&myip=2xxx:xxxx:xxxx:xxxx:b4e7:4193:4d0c:914b

    As described by dns.he.net website, also if you use this address on a browser it does work.

    The result after invoking that address is:
    Invoke-WebRequest : El servidor ha cometido una infracción de protocolo. Section=ResponseStatusLine
    En C:\Users\Usuario\noip.ps1: 45 Carácter: 11

    • $update = Invoke-WebRequest "$URL"
    • ~~~~~~~~~~~~~~~~~~~~~~~~
    • CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException
    • FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

  3. MotoX80 36,291 Reputation points
    2022-07-21T22:41:42.887+00:00

    Try changing the URL to HTTPS.

    # Build URL for update  
    $URL = "https://${myHost}:${myPass}@dyn.dns.he.net/nic/update?hostname=$myHost&myip=$myIP"  
      
    
    0 comments No comments

  4. sebholz 1 Reputation point
    2022-07-22T00:46:26.963+00:00

    Hi, I have already tryed that.
    Also by replacing the line with:

    • (Invoke-WebRequest -Uri "$URL").Content
    • Invoke-WebRequest -Method GET -Uri "$URL"
    • Invoke-RestMethod -Uri "$URL"

    Even in each case, with http or https.

    0 comments No comments

  5. sebholz 1 Reputation point
    2022-07-22T22:40:19.383+00:00

    So, I was able to figure it out in some way, with Wireshark as @Rich Matheisen said I was able to capture the packets, the problem is that the reponse from the server was: 401 Authorization Required
    Even if I place the credentials on the $URL on the part http://${myHost}:${myPass}@ there is still some required step and that is to acept the connection with such credentials!
    So what I did, Ive change the $URL for:
    $URL = "http://dyn.dns.he.net/nic/update?hostname=${myHost}&password=${myPass}&myip=${myIP}"

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.