I have a Powershell Custom sensor which performs a scan of ..., every 5 minutes or so, i want to be able to display when it has run ie today's time, but i am having problems with the output - here is a snippet of my current code, but i would either like to display the value as 'Time' or as a string rather than as a real number

  1. Now Display how long since last scan - Compare current time with time of last Record
	$lastscan = Get-Date -Format "yyyy/MM/dd HH:mm:ss"						# Time Now
	$midNight = Get-Date -Hour 0 -Minute 00 -Format "yyyy/MM/dd HH:mm:ss"	# Midnight
	$timeDiff = new-timespan -start $midNight -end $lastscan				# Calc diff as Structure of Times
  1. Days : 0
  2. Hours : 16
  3. Minutes : 3
  4. Seconds : 0
  5. Milliseconds : 0
  6. Ticks : 577800000000
  7. TotalDays : 0.66875
  8. TotalHours : 16.05
  9. TotalMinutes : 963
  10. TotalSeconds : 57780
  11. TotalMilliseconds : 57780000
  12. Convert Returned Time Into hh:mm for readability
	[string]$timeDiffS = ([string]$timeDiff.hours).PadLeft(2,'0') + '.' + ([string]$timeDiff.minutes).PadLeft(2,'0')

	Write-Host '            {'
	Write-Host '                "channel": "'zTimeOf Last Scan'",'
	Write-Host '                "unit": "Hours",'
	Write-Host '                "mode": "Absolute",'
	Write-Host '                "showChart": "1",'
	Write-Host '                "showTable": "1",'
	Write-Host '                "warning": "0",'
	Write-Host '                "float": "1",'
	Write-Host '                "decimalmode": "2.2",'
	Write-Host '                "value": "'$timeDiffS'",'
	Write-Host '                "LimitMaxError": "99999",'
	Write-Host '                "LimitMaxWarning": "9999",'	
	Write-Host '                "limitmode": "0"'
	Write-Host '            },'

i would like it to be HH:MM, but i have to go with HH.MM and if there is a trailing 0 i get HH.M How can i recode this to get the format i require PS i know its messy and inefficient, but it works Optimisation can come when its all working correctly


Article Comments

Hello Gerald,

Thank you for your message.

Regarding what you would like to achieve, I'm afraid that it's not possible to configure the channel to display all decimal during its creation. Nevertheless, you can override that parameter in the channel settings, by enabling the option Custom under Decimal Places and provide the value "2" there.

Here is also an updated version of the script:

$lastscan = Get-Date -Format "yyyy/MM/dd HH:mm:ss"						# Time Now
$midNight = Get-Date -Hour 0 -Minute 00 -Format "yyyy/MM/dd HH:mm:ss"	# Midnight
$timeDiff = new-timespan -start $midNight -end $lastscan				# Calc diff as Structure of Times
[string]$timeDiffS = ([string]$timeDiff.hours).PadLeft(2,'0') + '.' + ([string]$timeDiff.minutes).PadLeft(2,'0')

Write-output @"
{
    "prtg": {
        "result": [
            {
                "channel": "Time of Last Scan",
                "value": "$timeDiffS",
                "float": "1"
            }
        ],
        "text": "$timeDiff"
    }
}
"@

The latter provide the possibility to return information in the sensor message field via the key "text".

If you have questions, let us know.

Regards.


Sep, 2021 - Permalink

re Decimal

"Regarding what you would like to achieve, I'm afraid that it's not possible to configure the channel to display all decimal during its creation. Nevertheless, you can override that parameter in the channel settings, by enabling the option Custom under Decimal Places and provide the value "2" there."

Isnt that what "decimalmode": "2.2",' does, in my script?

re Text

There are 32 other channels in this sensor and the text attribute doesnt work as it doesnt show up on my Map which looks like this:

ChannelLast Value
AK1 #
AL1 #
BL1 #
BN1 #
...
SW-99,999,999 #
WM-99,999,999 #
zTimeOf Last Scan8.17 #
Scan Count30 #

Downtime

And i would like to suppress the Downtime channel in my map, but this doesnt work (if i do it manually it does, so i am guessing i have the wrong ID for the channel?)

Write-Host ' {' Write-Host ' "channel": "Downtime",' Write-Host ' "showChart": "0",' Write-Host ' "showTable": "0",' Write-Host ' }'


Sep, 2021 - Permalink

Hello Gerald,

The setting DecimalMode allows to choose between the option "auto" and "all" only. The latter is the default option when float is set to 1 however it automatically remove the trailing 0. Therefore, the only option is to manually configure the channel setting Decimal Places to "Custom" with the value set to 2.

If you have to change the setting for all channels, I invite you to have a look at the PowerShell module PRTGAPI to modify them all at once. Here is the wiki of the module: https://github.com/lordmilko/PrtgAPI/wiki

Here is the command you can use for it:

Get-Sensor -id SensorID | get-channel | Set-ChannelProperty -DecimalMode Custom -DecimalPlaces 2

Regarding the downtime channel, it is not possible to "remove" it at the creation of the sensor. However, you can hide it from the table and graph when the sensor is created, as you noticed.

If you have further questions, let me know.

Regards.


Sep, 2021 - Permalink