What is this?

This knowledgebase contains questions and answers about PRTG Network Monitor and network monitoring in general. You are invited to get involved by asking and answering questions!

Learn more

PRTG Network Monitor

Intuitive to Use. Easy to manage.
300.000 administrators have chosen PRTG to monitor their network. Find out how you can reduce cost, increase QoS and ease planning, as well.

Free Download

Top Tags


View all Tags

Powershell Custom Sensors and Time

Votes:

0

Your Vote:

Up

Down

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

channel powershell prtg

Created on Sep 8, 2021 6:55:34 AM by  GeraldC (0) 1

Last change on Sep 8, 2021 11:06:24 AM by  Felix Wiesneth [Paessler Support]



3 Replies

Votes:

0

Your Vote:

Up

Down

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.

Created on Sep 9, 2021 2:15:26 PM by  Florian Lesage [Paessler Support]



Votes:

0

Your Vote:

Up

Down

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

Created on Sep 10, 2021 12:42:50 AM by  GeraldC (0) 1



Votes:

0

Your Vote:

Up

Down

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.

Created on Sep 10, 2021 6:22:49 AM by  Florian Lesage [Paessler Support]



Please log in or register to enter your reply.


Disclaimer: The information in the Paessler Knowledge Base comes without warranty of any kind. Use at your own risk. Before applying any instructions please exercise proper system administrator housekeeping. You must make sure that a proper backup of all your data is available.