Hi there, I would appreciate any assistance as I've spent two days trying to find solutions to my problem online.
The purpose of my powershell sensor (for PRTG) is to collect data from a corporate API and display the relevant stats in PRTG. Here's the problem(s): - I need the device IP (not the server IP) as it forms part of the API URL. - The device on which the sensor will be placed is very likely to be cloned in the future (several times), so I cannot hard-code the device ID. New clones will have their own API and a different IP address (API and device IPs are the same). - Similarly, I can't hard-code in the sensor ID - Since there are alot of other sensors, my manage does not want me to enter the IP/device-id as a param because the person doing the cloning could easily forget to update it. So no params.
From what I've researched online, I can get to the device IP if only I could just fetch the Sensor ID or device ID, but I don't know how to do that in powershell (and I didn't find any examples online that did either).
Thanks
Article Comments
Thanks Arne,
Unfortunately, as mentioned in the original post, I cannot hard code in a sensor ID as I'm sure the ID will be different for new clones. If there were some way to get the ID using PowerShell, that would pretty much solve everything (indirectly).
Apr, 2017 - Permalink
Dear MichaelMcQuirk
See the first API call I mentioned. You could generate a list of all sensor IDs in the script and identify the sensor by the name, or comments. (Somehow you have to have a unique property.) That you can use the ID that search yielded. Do you have anything to identify the sensor(s) in question?
Apr, 2017 - Permalink
@Arne, the problem I have is that almost nothing will be unique as the sensor is expected to be cloned multiple times. From what I've heard, the name and ID will change (incrementally for each new clone) but I'll still have no way of telling which one of the sensors the current PowerShell script is running for/under, which is necessary for the sensor to do its thing.
The closest solution I have found to my problem is a Python sensor example which manages to get its device id. Unfortunately I'm not experienced in python and recoding my PowerShell sensor (as simple as it is) could take up too much time. Here's the code for that Python Sensor:
# -*- coding: utf-8 -*- import sys import json # get CustomSensorResult from paepy package from paepy.ChannelDefinition import CustomSensorResult if __name__ == "__main__": # interpret first command line parameter as json object data = json.loads(sys.argv[1]) # create sensor result result = CustomSensorResult("This sensor is added to " + data['host']) # add primary channel result.add_channel(channel_name="Percentage", unit="Percent", value=87, is_float=False, primary_channel=True, is_limit_mode=True, limit_min_error=10, limit_max_error=90, limit_error_msg="Percentage too high") # add additional channel result.add_channel(channel_name="Response Time", unit="TimeResponse", value="4711") # print sensor result to std print(result.get_json_result())
Apr, 2017 - Permalink
Dear MichaelMcQuirk
This looks like a rather complex issue. Managing device tree objects with scripts in in principle possible, but PRTG is optimized to be used manually via the interface. Especially if you clone objects, it can get complicated.
Apr, 2017 - Permalink
Can you explain why you cannot use a placeholder as a parameter? for example use %host, so when you clone to sensor (you are forced to enter a new hostname / IP) the custom exe script will continue to point to the correct host.
https://https://helpdesk.paessler.com/en/support/solutions/articles/76000063844
Apr, 2017 - Permalink
@AndrewG, thanks for the suggestion, I did not consider placeholders before-hand.
I've done some looking into using placeholders and unfortunately, I don't think it'll help. We clone from working/existing sensors and not templates, so (if my understanding that placeholders are only really used in templates, is correct) it won't be applicable.
Busy learning python at the moment to convert our current powershell code to python.
Apr, 2017 - Permalink
@MichaelMcQuirk, that is not the case with PRTG, placeholders do not "hardcode themselves" once you clone. They will stay as a placeholder variable for life.
Placeholders is certainly the way to go.
I use this method to pass the DeviceID through to a powershell script, this script does a few things, then updates the PRTG devices NOTES tab (based on the DeviceID I pass to the script) with the detailed results of the script. The script also returns the results normally to the PRTG sensor so I can see if its "up" or "down".
Apr, 2017 - Permalink
I highly recommend using the PRTG API for Powershell written by lordmilko. i've been using it for a few months & it makes it possible to use PRTG in an automated fashion that Paessler have no included.
PS Install Command: Install-Module -Name PrtgAPI
https://www.powershellgallery.com/packages/PrtgAPI/0.9.5
Mar, 2019 - Permalink
Dear MichaelMcQuirk
There is no easy way to do that. You could use the API to generate a list which contains object ids and sensor names and then look the ip up by its name, or by its comments.
You can use the API to get the parent object (the device id of a sensor)
(Of course, the id value has to be replaced with the actual sensor ID.)
You can use the API to extract the host name or IP (whatever is entered) for a device
(Again, the id value has of course to be the correct ID.)
Apr, 2017 - Permalink