I am trying to connect to historic data via this URL, which contains my custom date range (last 30 days) and creds.

 $prtgurl = "$PRTGHost/api/historicdata.csv?id=$SensorID&avg=3600&sdate=$StartDate2&edate=$EndDate2&$cred"
 $request = Invoke-WebRequest -Uri $prtgurl -MaximumRedirection 0 -ErrorAction Ignore

I always get this error, which I cant get past. Any suggestions would be great.

Invoke-WebRequest : Unauthorized
+ ...  $request = Invoke-WebRequest -Uri $prtgurl -MaximumRedirection 0 -Er ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

Article Comments

Hi there,

According to the error message "Unauthorized", the credentials are not correct. What does the "$cred"-variable look like?

Best regards.


Jun, 2019 - Permalink

like this $cred = New-Credential -UserName xxxx -Password xxxxxx


Jun, 2019 - Permalink

Hi there,

I am afraid that this won't work. Changing the "$prtgurl"-variable this way should work:

"$PRTGHost/api/historicdata.csv?id=$SensorID&avg=3600&sdate=$StartDate2&edate=$EndDate2&username=XXXXXXX&password=XXXXXX"

Best regards.


Jun, 2019 - Permalink