lordmilko / PrtgAPI

C#/PowerShell interface for PRTG Network Monitor
MIT License
301 stars 37 forks source link

startIndex cannot be larger than length of string. #283

Closed cyberoner1 closed 2 years ago

cyberoner1 commented 2 years ago

Describe the bug

When running the script I am getting the error somewhere in the middle,

get-sensorhistory : startIndex cannot be larger than length of string.
Parameter name: startIndex
At C:\Powershell_Scripts\Postgres\Eurogate\history_new.ps1:27 char:114
+ ... lyContinue |get-sensorhistory -Enddate $Enddate -Startdate $Startdate ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Get-SensorHistory], ArgumentOutOfRangeException
    + FullyQualifiedErrorId : System.ArgumentOutOfRangeException,PrtgAPI.PowerShell.Cmdlets.GetSensorHistory

any Idea why=

Steps to reproduce

`$Enddate='2022-03-18 12:00:00'
 $Startdate='2022-03-18 23:59:59'

 #------------Get Data SNMP Traffic-----------------------------------

    get-sensor -tag tableau  -type snmptraffic,snmpdiskfree,snmpcpu,ping -status up -ErrorAction SilentlyContinue |get-sensorhistory -Enddate $Enddate -Startdate $Startdate |  Select-Object -Property DateTime,SensorId,'TrafficIn(volume)','TrafficIn(speed)','TrafficOut(volume)','TrafficOut(speed)',FreeSpace,Total,PacketLoss | Export-csv -Path C:\Powershell_Scripts\Postgres\Eurogate\data\export.csv -Delimiter ";" -NoTypeInformation

What is the output of 'Get-PrtgClient -Diagnostic'?

PSVersion      : 5.1.17763.2268
PSEdition      : Desktop
OS             : Microsoft Windows Server 2019 Standard
PrtgAPIVersion : 0.9.17
Culture        : de-DE
CLRVersion     : .NET Framework 4.8 (528049)
PrtgVersion    : 21.3.70.1629
PrtgLanguage   : english.lng

Additional context

No response

lordmilko commented 2 years ago

Hi @cyberoner1,

After reproducing the error can you please provide the output of $error[0].Exception.StackTrace

cyberoner1 commented 2 years ago

sorry for the late reply.. here is the output:

$error[0].Exception.StackTrace at System.String.Substring(Int32 startIndex, Int32 length) at PrtgAPI.Utilities.ConvertUtilities.TryRoundDecimal(String str, Double raw, Func2 toDecimal, String numDecStr, String strDecStr, Double& result) at PrtgAPI.Utilities.ConvertUtilities.ToDynamicDouble(String str, Nullable1 raw) at PrtgAPI.PowerShell.SensorHistoryFormatter.GetChannelValue(ChannelHistoryRecord channel) at PrtgAPI.PowerShell.SensorHistoryFormatter.CreateObject(SensorHistoryRecord date) at PrtgAPI.PowerShell.SensorHistoryFormatter.d__10.MoveNext() at PrtgAPI.PowerShell.Base.PrtgProgressCmdlet.WriteList[T](IEnumerable`1 sendToPipeline) at PrtgAPI.PowerShell.Cmdlets.GetSensorHistory.ProcessSensorHistory() at PrtgAPI.PowerShell.Base.PrtgCmdlet.ExecuteWithCoreState(Action action) at System.Management.Automation.CommandProcessor.ProcessRecord()

lordmilko commented 2 years ago

Thanks @cyberoner1,

Can you also do the following:

First do

Set-PrtgClient -LogLevel Response

then invite Get-SensorHistory again, specifying the -Verbose parameter

get-sensor -tag tableau  -type snmptraffic,snmpdiskfree,snmpcpu,ping -status up -ErrorAction SilentlyContinue |get-sensorhistory -Enddate $Enddate -Startdate $Startdate -Verbose

assuming it still crashes can you please provide a copy of the response that is emitted

lordmilko commented 2 years ago

Hi @cyberoner1,

Just following up on this, are you able to advise?

squintfox commented 2 years ago

Hi @lordmilko, I've actually had this same issue for a long time and just happened to see this open issue now. It happens on maybe 3 or 4 sensors out of around 300, with which ones fail changing week to week (I guess as underlying data changes). I've attached all the same output requested with a failing sensor. Hopefully this helps.

Commands: $current_date = Get-Date $sensor = Get-Sensor -Id 60010 $sensor | Get-SensorHistory -StartDate $current_date -EndDate $current_date.AddDays(-2) -Average 3600 -Verbose

output.txt

lordmilko commented 2 years ago

Hi @squintfox,

Thanks for providing these logs. I have pushed a commit which I believe should contain a fix for this issue. Are you able to test PrtgAPI 0.9.18-preview.5 by following the manual installation instructions and advise whether the issue is now resolved?

squintfox commented 2 years ago

@lordmilko it looks like it's working great. Thank you.