What is this?

This knowledgebase contains questions and answers about PRTG Network Monitor and network monitoring in general.

Learn more

PRTG Network Monitor

Intuitive to Use. Easy to manage.
More than 500,000 users rely on Paessler PRTG every day. Find out how you can reduce cost, increase QoS and ease planning, as well.

Free Download

Top Tags


View all Tags

Get data from PRTG with Powershell

Votes:

0

Is it possible to get data from PRTG, with powershell? Has anybody done that before?

I would like to receive data like disk-usages and uptime-information.

information powershell prtg

Created on Apr 7, 2016 7:14:16 AM



2 Replies

Votes:

0

Hi,
Sure, this is possible. PRTG offers an HTTP API which can be queried using Powershell. The API Dcoumentation can be found at YOUR_PRTG_SERVER/api.htm. I personally am using the Invoke-WebRequest cmdlet here. Example code below:

param(
    [string]$user = "prtgadmin",
    [string]$pass = "prtgadmin",
    [string]$prtgserver = "https://127.0.0.1",
    [string]$time = "yesterday",
    [string]$path = "D:\Temp\"
)
# function from http://stackoverflow.com/questions/11696944 to override SSL Cert check
function Ignore-SSLCertificates
{
    $Provider = New-Object Microsoft.CSharp.CSharpCodeProvider
    $Compiler = $Provider.CreateCompiler()
    $Params = New-Object System.CodeDom.Compiler.CompilerParameters
    $Params.GenerateExecutable = $false
    $Params.GenerateInMemory = $true
    $Params.IncludeDebugInformation = $false
    $Params.ReferencedAssemblies.Add("System.DLL") > $null
    $TASource=@'
        namespace Local.ToolkitExtensions.Net.CertificatePolicy
        {
            public class TrustAll : System.Net.ICertificatePolicy
            {
                public bool CheckValidationResult(System.Net.ServicePoint sp,System.Security.Cryptography.X509Certificates.X509Certificate cert, System.Net.WebRequest req, int problem)
                {
                    return true;
                }
            }
        }
'@ 
    $TAResults=$Provider.CompileAssemblyFromSource($Params,$TASource)
    $TAAssembly=$TAResults.CompiledAssembly
    ## We create an instance of TrustAll and attach it to the ServicePointManager
    $TrustAll = $TAAssembly.CreateInstance("Local.ToolkitExtensions.Net.CertificatePolicy.TrustAll")
    [System.Net.ServicePointManager]::CertificatePolicy = $TrustAll
}
Ignore-SSLCertificates

$get_devices_uri = ("{0}/api/table.json?content=devices&output=json&columns=objid&username={1}&password={2}" -f $prtgserver, $user, $pass)

try{
    $JSON = Invoke-WebRequest -Uri $get_devices_uri | ConvertFrom-Json
}
catch{
    Write-Host "Error. Cannot get device ids. Exiting!"
    exit 0
}
foreach($object in $JSON.devices){
    $destination = ("{0}\{1}.csv" -f $path, $object.objid)
    $get_messages_uri = ("{0}/api/table.xml?content=messages&output=csvtable&columns=objid,datetime,parent,type,name,status,message&filter_drel={1}&id={2}&username={3}&password={4}" -f $prtgserver, $time, $object.objid, $user, $pass)
    try{
        Invoke-WebRequest -Uri $get_messages_uri -OutFile $destination
    }
    catch{
        Write-Host "Error. Cannot get messages. Exiting!"
        exit 0
    }
}

Above code gets messages from a sensor but can easily be adapted to sensor data. Please note that the function from Stackoverflow has to stay in place when you are using a self signed Certificate as this will (I know this is not optimal) overwrite the certificate check. If not needed please remove!

Created on Apr 7, 2016 1:36:05 PM by  Konstantin Wolff [Paessler Support]



Votes:

1

Certainly done it before..

Have a look here:

Look at the function Get-prtgSensorData

Params: SensorID, [datetime]StartDate, [datetime]EndDate

Returns a table of the raw data.

More details here:

Created on Apr 21, 2016 11:43:48 AM

Last change on Jun 6, 2016 6:13:41 AM by  Luciano Lingnau [Paessler]




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.