lordmilko / PrtgAPI

C#/PowerShell interface for PRTG Network Monitor
MIT License
301 stars 37 forks source link

Get device and get sensor via pipeline in table format #399

Closed hirGorrth4d closed 1 month ago

hirGorrth4d commented 1 month ago

What's going on?

Hello, I have a question regarding retrieving data from some devices in my PRTG through powershell I'm trying using the cmdlet Get-Device with Get-Sensor via pipeline but I can't get the information

image image

I want to combine the information from the sensor like status o Message with the device, like Host, id or group but all in one table. Im trying with the format-list too but it's not readable

is there a way to get all that in one table? also, get more characters to read all devices name

thanks

Due Dilligance

lordmilko commented 1 month ago

You can create a data structure containing whatever properties you like by creating a PSCustomObject.

Rather than do everything in one big pipe, you can write a script in the PowerShell ISE to construct a custom object containing the properties you like

$groups = Get-Group

foreach($group in $groups)
{
    $devices = $group | Get-Device

    foreach($device in $devices)
    {
        $sensors = $device | Get-Sensor

        foreach($sensor in $sensors)
        {
            [pscustomobject]@{
                GroupName = $group.Name
                DeviceName = $device.Name
                SensorName = $sensor.Name
            }
        }
    }
}

By default, PowerShell will format any object as a list that has more than 4 properties, unless a custom format is defined (as is the case with many of PrtgAPI's data types). While you can force formatting as a table using Format-Table, this has its limits. To get a wider view of a set of a set of objects you can potentially pipe to Out-GridView, or otherwise export to a CSV and analyze in Excel. You can store each PSCustomObject] you create in a variable to do this

$results = @()

$groups = Get-Group

foreach($group in $groups)
{
    $devices = $group | Get-Device

    foreach($device in $devices)
    {
        $sensors = $device | Get-Sensor

        foreach($sensor in $sensors)
        {
            $results += [pscustomobject]@{
                GroupName = $group.Name
                DeviceName = $device.Name
                SensorName = $sensor.Name
            }
        }
    }
}

$results | Export-Csv C:\results.csv -NoTypeInformation