Skip to content

Miscellaneous

lordmilko edited this page Jul 27, 2022 · 12 revisions

Contents

C#

PRTG System Status

The current system of a PRTG Server, including license, configuration and version details can be retrieved using the GetStatus method.

var status = client.GetStatus();

PowerShell

Open PRTG Objects

PrtgAPI can open any Sensor, Device, Group or Probe in a web browser by piping the corresponding object to Open-PrtgObject

Get-Sensor -Tags wmicpu* | Open-PrtgObject

Open-PrtgObject will open a new tab for each object in your systems default web browser. Care should be taken when opening too many tabs at once.

PRTG System Status

The current system of a PRTG Server, including license, configuration and version details can be retrieved using the Get-PrtgStatus cmdlet.

C:\> Get-PrtgStatus

DateTime        : 11/11/2017 10:36:11 PM
Version         : 17.4.33.3283
NewLogs         : 0
NewAlarms       : 2
LicenseType     : Commercial
IsCluster       : True
ClusterNodeType : Master
ClusterNodeName : PRTG Network Monitor (Master)
...

With-Property

From PrtgAPI 0.10

When chaining multiple cmdlets together in PowerShell, sometimes you find you'd data that was contained in the output from an earlier cmdlet to also be contained in the output of a later cmdlet.

For example, consider the simple PrtgAPI invocation of Get-Sensor | Get-Channel

C:\> Get-Sensor | Get-Channel

Name                SensorId    Id    LastValue LimitsEnabled UpperErrorLimit LowerErrorLimit ErrorLimitMessage
----                --------    --    --------- ------------- --------------- --------------- -----------------
Total               3001         0       0.32 %          True              95                 PANIC!! PANIC!!!
Processor 1         3001         1         <1 %         False

While Channel objects may contain the SensorId of the Sensor they belong to, they don't contain any other properties commonly found in sensors, such as the sensor Name, Device, Group, Probe or any other custom property you may like to include. Ultimately, there is never going to be a way of including every single type of information a user may want to have on an object by default. Fortunately, using our programming skills, we can often find clever ways of working around this

C:\> Get-Sensor | foreach { $_ | Get-Channel | Add-Member SensorName $_.Name -Force }

However these workarounds are often messy, verbose, and just annoying to type out.

PrtgAPI provides a solution to this, via the magic With-Property cmdlet, aliased as with for short. with allows you to inspect the entire pipeline up to the point where with is called, allowing you to easily extract values from objects that were piped through multiple cmdlets ago.

Using with, our Channel Sensor Name example above can be written as

# Add a SensorName property to each Channel
C:\> Get-Sensor | Get-Channel | with { $__.Name }

A common programming idiom in PowerShell is the automatic variable Dollar Under ($_), which is used by many cmdlets allow you to use within a ScriptBlock for referring to the current object in the pipeline that is being processed. With-Property takes this concept one step further, allowing you to specify Double, Triple, Quadruple and so on Dollar Underline variables, for accessing the last value that was emitted an arbitrary number of cmdlets away from your with invocation. With-Property is capable of retrieving the current pipeline element of both variables and cmdlets; as such, the following would work just as easily as well

C:\> $sensors = Get-Sensor
C:\> $sensors | Get-Channel | with { $__.Name }

When a property name is not specified to the -Name parameter, with will attempt to intelligently calculate a name to use for the property, based on the type of the input object and the names of all members that were invoked to access the resulting value

# Add a new property DeviceIntervalTotalSecondsToString to each channel containing
# the total number of seconds of each Channel's Sensor's Device's Scanning Interval,
# as a string
C:\> Get-Device | Get-Sensor | Get-Channel | with { $___.Interval.TotalSeconds.ToString() }

When you start dealing with Multi Level Dollar Under Variables, it can sometimes get confusing trying to figure out which cmdlet a variable refers to. Does Dollar Under refer to the cmdlet that is emitting or receiving? The answer is, Dollar Under refers to the value that was emitted from the nth cmdlet behind me (me being your with invocation). As such, given the following pipeline

Get-Sensor | Get-Channel | with { $__.Name }

The Dollar Under breakdown would be

Val Receiving Emitting Description
$_ with Get-Channel The input to with from the first cmdlet behind it
$__ Get-Channel Get-Sensor The input to Get-Channel from the second cmdlet behind with
$___ Get-Sensor N/A The input to Get-Sensor from the third cmdlet behind with

In the event you do type an illegal Dollar Under variable, With-Property will emit a helpful table similar to the above that will help you understand what Dollar Under variables are valid and what they will refer to within your invocation

C:\> Get-Sensor | Get-Channel | with { $___.Name }

with : Dollar Under variable with 3 underscores was specified, however there is no pipeline element at position 3 that
would emit a value to pipeline element 2 ('Get-Sensor'). Pipeline contains the following prior elements:
0: with        - $_ will get the output of 'Get-Channel'
1: Get-Channel - $__ will get the output of 'Get-Sensor'
2: Get-Sensor

With-Property expects a relatively simple expression to be specified, which it then parses in an attempt to figure out what the dynamic name should be. If an expression with non-trivial Ast nodes is specified (i.e. some ridiculous if expression or something) With-Property will throw an exception stating it does not know how to parse the unknown Ast node for calculating a name.

If a -Name is specified however, you can specify whatever you like in the ScriptBlock. Each invocation of With-Property should return only a single property value. If a -Name is not specified and you attempt to access multiple unrelated properties in your expression, an exception will be thrown. If you wish to append multiple properties with with, this should be done by specifying an array of script blocks

# Wrong
C:\> Get-Sensor | Get-Channel | with { $__.Name; $__.Device }
# Will add a SensorName and SensorDevice to each Channel
C:\> Get-Sensor | Get-Channel | with { $__.Name },{ $__.Device }

If a "literal value" is piped into With-Property (such as string or primative types such as int, double, bool, etc), With-Property will create a new PSObject that encapsulates the properties you are trying to add, followed by a Value property containing the literal value that was piped in

C:\> Get-Sensor -Type wmiremoteping | Get-ObjectProperty Target | with { $__.Name }

SensorName  Value
----------  -----
router-1    192.168.0.1
...
Clone this wiki locally