Skip to content

Devices

lordmilko edited this page Dec 16, 2020 · 33 revisions

Contents

C#

Basic Requests

Devices can be retrieved from a PRTG Server via the GetDevices method

var devices = client.GetDevices();

If you wish to filter for devices that meet certain search criteria you can specify a Property to filter on using one of several overloads

//Get devices whose name match "dc-1"
var devices = client.GetDevices(Property.Name, "dc-1");
//Get devices under probes whose name contains "office"
var devices = client.GetDevices(Property.Probe, FilterOperator.Contains, "office");

Queries can also be specified via LINQ style query expressions, allowing high performance queries to be executed via dynamically generated request parameters.

var names = client.QueryDevices(d => d.Host.Contains("192.168"))
                  .Select(d => d.Name)
                  .Take(20);

For more information on using LINQ style queries, see Queries.

When retrieving a particular device you insist should exist, it is possible to use the singular GetDevice method, returning a single Device rather than a List<Device> as with GetDevices.

var device = client.GetDevice(1234);

If a device with the specified object ID cannot be found (or somehow multiple objects were found) GetDevice will throw an InvalidOperationException. If you are not sure whether a device exists, you should use GetDevices instead and check for the presence of any results.

var device = client.GetDevices(Property.Id, 1234).FirstOrDefault();

if (device != null)
    Console.WriteLine($"Found device {device}!");

Advanced Filtering

Filters are implemented internally via the SearchFilter class. One or more SearchFilter objects can be specified to filter on multiple properties.

//Get all devices with more than 10 sensors in New York whose host address
//contains "192.168"
var filters = new[]
{
    new SearchFilter(Property.TotalSensors, FilterOperator.GreaterThan, 10),
    new SearchFilter(Property.Location, FilterOperator.Contains, "New York"),
    new SearchFilter(Property.Host, FilterOperator.Contains, "192.168")
};

var devices = client.GetDevices(filters);

GetDevices can be used in conjunction with other methods in order to chain results together.

//Get all sensors for all devices that have the "chicago" tag
var sensors = client.GetDevices(Property.Tags, FilterOperator.Contains, "chicago")
    .SelectMany(device => client.GetSensors(Property.ParentId, device.Id));

Creation

For information on creating new devices please see Object Creation.

System Information

PrtgAPI supports the retrieval of device system information (including hardware, software, processes, services, etc) via the GetSystemInfo method

//Retrieve all info from the object with ID 1001
var info = client.GetSystemInfo(1001);

PrtgAPI provides several GetSystemInfo overloads for retrieving system information

  • GetSystemInfo(Either<Device, int>): retrieve all system information for an object
  • GetSystemInfo<T>(Either<Device, int>): retrieve a specific type of system info based on its object type (such as DeviceHardwareInfo)
  • GetSystemInfo(Either<Device, int>, SystemInfoType): retrieve a specific type of system info based on its enum type (such as SystemInfoType.Hardware)

When you only require a specific type of system information, it is recommended to use GetSystemInfo<T> for maximum performance.

var processes = client.GetSystemInfo<DeviceProcessInfo>(2001).Select(p => p.Name);

PRTG automatically updates device system information once every 24 hours. Please note that not all devices may support providing system information, and may not support providing all information types.

If you wish to force a refresh of system information for a specified device, this can be done via the RefreshSystemInfo method

client.RefreshSystemInfo(1001, SystemInfoType.Hardware, SystemInfoType.Software);

If no system information types are specified, by default all types will be refreshed. PRTG can execute up to 24 system information scans at once. If more than 24 scans are requested, these requests will be queued until a slot becomes available.

PowerShell

Basic Requests

Devices can be retrieved with PowerShell via the Get-Device cmdlet. (Note: for complete instructions on using Get-Device, please see Get-Help Get-Device)

C:\> Get-Device

Name                Id      Status      Group           Probe
----                --      ------      -----           -----
exch1               2061    Up          Windows         London Office
sql1                2070    Up          Servers         Local Probe
wsus1               2077    Down        Servers         Local Probe

PrtgAPI automatically formats Device objects in a table, displaying the most relevant properties. To view all properties of devices, pipe your devices to Format-List

Get all devices named "dc1" (case insensitive)

C:\> Get-Device dc1

Name                Id      Status    Host      Group           Probe
----                --      ------    ----      -----           -----
dc1                 2001    Up        10.0.0.1  Servers         Local Probe

Wildcards can also be used

Get-Device *dc*

Get-Device can filter on a number of properties, including -Host, Id and Tags

C:\> Get-Device -Host 192*,172*
C:\> Get-Device -Id 2197

Name                Id      Status    Host            Group           Probe
----                --      ------    ----            -----           -----
app1                2197    Up        app1.lab.local  Servers         Local Probe
# Get all devices containing the C_OS_WIN tag
Get-Device -Tags C_OS_WIN

When filtering by tags, PrtgAPI provides two parameters that can be used

  • -Tags: Filter for objects that contain all specified tags
  • -Tag: Filter for objects that contain any specified tags
# Get all devices in Manhattan, New York
Get-Device -Tags ny,manhattan

# Get all Manhattan and Queens devices
Get-Device -Tag manhattan,queens

Get-Device accepts both Group and Probe objects via the pipeline. When an object is piped to Get-Device, the object is used as a filter in conjunction with any other filters that may be specified

Get-Probe micro* | Get-Device
Get-Group | Select -First 1 | Get-Device *dc*

For device properties that are not explicitly defined as parameters on Get-Device, these properties can still can be specified as dynamic parameters

# Get all devices that are the first or second object under their parent
Get-Device -Position 1,2

Cmdlet parameters that are typically used for accepting pipeline input (e.g. -Group) can also be used for filtering by a single object name

# Get all devices under all groups whose name includes "vmware"
Get-Device -Group *vmware*

Advanced Filtering

To filter by custom fields the New-SearchFilter cmdlet can be used (alias: flt).

flt location contains "new york" | Get-Device

When chaining results, consider whether your results could be acquired quicker by constructing an array of filters containing the criteria you are after; i.e. you do not need to ask for probe and group objects before devices if you know the name of the probes and groups you'd like to find devices under.

For more information on New-SearchFilter see Filters

Device objects can also be used as the input to cmdlets (including cmdlets retrieving sensors and manipulating state). For more information see the See Also section below.

Note that special traversal logic may be required when requesting child devices belonging to group objects. For more information, see Group Recursion.

Creation

For information on creating new sensors please see Object Creation.

System Information

PrtgAPI supports the retrieval of device system information (including hardware, software, processes, services, etc) via the Get-SystemInfo cmdlet

C:\> Get-SystemInfo -Id 1001 Hardware

Name                       DeviceId   Class            State   Capacity   SerialNumber
----                       --------   -----            -----   --------   ------------
\\.\PHYSICALDRIVE0         1001       DiskDrive        OK
Intel(R) Xeon(R) CPU X...  1001       Processor        OK                 Intel64 Fami...
Microsoft ISATAP Adapter   1001       NetworkAdapter
Microsoft Kernel Debug...  1001       NetworkAdapter
Microsoft Print to PDF     1001       Printer          Unknown
...

If more than one -Type is specified, Get-SystemInfo will wrap info results up in a PSObject. If no -Type is specified, all types will be retrieved.

C:\> Get-Device -Count 1 | Get-SystemInfo

System    : {Bios Serial Number, vmxnet3 Ethernet Adapter, vmxnet3 Ether...
Software  : {Microsoft Visual C++ 2008 Redistributable - x64 9...
            Redistributable - x86 9.0.30729.4148, PRTG Network Monitor...
Hardware  : {\\.\PHYSICALDRIVE0, Intel(R) Xeon(R) CPU X5650 @ 2.10GHz,...
Users     : {PRTG-1\ANONYMOUS LOGON, PRTG-1\DWM-1, PRTG-1\DWM-2...
Processes : {System Idle Process (PID: 0), System (PID: 4), winlogon.exe...
Services  : {AJRouter, ALG, AppIDSvc, Appinfo...}

PRTG automatically updates device system information once every 24 hours. Please note that not all devices may support providing system information, and may not support providing all information types.

If you wish to force a refresh of system information for a specified device, this can be done via the Refresh-SystemInfo cmdlet

Get-Device exch* | Refresh-SystemInfo -Type Hardware,Software

If no system information types are specified, by default all types will be refreshed. PRTG can execute up to 24 system information scans at once. If more than 24 scans are requested, these requests will be queued until a slot becomes available.

See Also

Clone this wiki locally