I'm trying to export a list of devices and their parentIDs using the api and PowerShell. I have the lines entered to ignore invalid SSL certs and if I paste the following URL into a web browser, it works fine after clicking SaveAs and providing a path:

https://{host_name}/api/table.xml?content=devices&output=csvtable&columns=device,host,objid,parentid&id=$StartingID&count=20000&username={user_name}&passhash={passhash}

But if I put it in to PowerShell, it does not work:

function getObjGroupList($oHost, $oUser, $oPass)
{
	$url = "https://$oHost/api/table.xml?content=devices&output=csvtable&columns=device,host,objid,parentid&id=$StartingID&count=20000&username=$oUser&passhash=$oPass"
	$objGroupList = Invoke-WebRequest -Uri $url -MaximumRedirection 0 -ErrorAction Ignore
	$objGroupList | Export-CSV -Path  "{Path}\groupID.csv" -NoTypeInformation
}

It creates the CSV file, but it only contains the headers, no data. Has anyone been able to successfully export parentIDs in order to pause and resume probes based on parentID?


Article Comments

Hi jrob,

You can do this fairly easily using PrtgAPI

C:\> Get-Device | Export-Csv C:\devices.csv -notype

This will contain all properties of devices, including their ParentId, Group and Probe names. If you don't actually need a CSV however and simply want to pause based on probes you can this fairly easily using PowerShell

C:\> Get-Device | group Probe | foreach { Get-Probe $_.name | Pause-Object -Forever }

Note that you can't pause probes based on "ParentId" as you suggest, as there's no guarantee that the ParentId of a device is a probe and not a group

If you wish to execute these API requests manually, you can specify the -Verbose parameter to any cmdlet within PrtgAPI to see the exact URL that it requests

Regards,

lordmilko


May, 2019 - Permalink

Hello Milko,

Thanks for the response. Hoping to avoid installing a PRTG module for PS as this may be launched from multiple computers. This is being put together as a PowerShell script to be run from a SCORCH runbook.

Pausing probes by parentID is working fine, I find the parentID of a hostname for one of our clients, all of their servers have the same parentID, I can pause and then resume without issue. The only problem is getting the export to find the parentID. I have to do it manually every time.

Thanks, jrob


May, 2019 - Permalink

Hi jrob,

One issue I'm seeing is you're trying to export the response to a file as a CSV, however you haven't actually converted the response into a series of objects yet. You need to parse the response *from* a CSV using the ConvertFrom-Csv cmdlet before you can re-export it *to* a CSV using the Export-Csv cmdlet

Potentially you can try the following

C:\> $response = iwr "http://prtg.example.com/api/table.csv?content=devices&columns=objid,name,location,host,group,probe,favorite,condition,upsens,downsens,downacksens,partialdownsens,warnsens,pausedsens,unusualsens,undefinedsens,totalsens,schedule,basetype,baselink,notifiesx,intervalx,access,dependency,position,status,comments,priority,message,parentid,tags,type,active&count=*&username=prtgadmin&passhash=12345678"

C:\> $response.content | ConvertFrom-Csv | Export-Csv C:\output.csv -notype

If you wanted to output the existing CSV response to a file as is, potentially you could use the Set-Content cmdlet instead of the Export-Csv cmdlet; I would recommend going through the intermediate steps of using ConvertFrom-Csv / Export-Csv however, as this will rectify any issues in PRTG's response such as certain columns missing headers, etc

Regards,

lordmilko


May, 2019 - Permalink