lordmilko / PrtgAPI

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

Creating Groups and Devices in a PRTG Cluster via .csv #127

Closed LKus95 closed 4 years ago

LKus95 commented 4 years ago

Describe the bug Clearly and concisely describe what you were trying to do, what happened and what you were expecting to happen I'm trying to add Groups and Devices to a PRTG Cluster (1 Master, 1 Slave, 3 RP's) which was newly created. From the old PRTG Server i've exported my relevant Data like Group Name, Device Name, Device Host and Probe. Now when i'm running my Script with the Probe Name as input for creating Groups and Devices it gets correctly created under that Probe, but when i want to use the Probe ID (1) as input, it creates the Groups and Devices under every Probe of the Cluster.

It basically works like i want it to, as it creates the Groups and Devices from my .csv-Files, but i'm just wondering why it behaves differently with the Probe ID and the Probe Name. The Powershell output with ID and Name seem to be exactly the same.

Steps to reproduce Put the relevant code from your application that caused the issue to happen in the code block below

# 

#Read-Host um den Probenamen zu erfragen und in die Schleife miteinzutragen 
$ProbeInput = Read-Host -Prompt "Bitte geben Sie den Namen der Probe an"

#Kontrolle ob der Read-Host Probename vom Server abrufbar ist
$ProbeCheck = Get-Probe -Id 1  #-Name $ProbeInput

# Checkt ob die Probe existiert auf dem Server und beginnt dann die Erstellung von den Gruppen
if($ProbeCheck -ne $null)
{        
    ForEach ($DInformation in $ImportListGroups) 
    {
        #CSV Inhalt den einzelnen Objekten zuordnen
        $DeviceProbe = $ProbeInput # User Eingabe als Probe festlegen.
        $DeviceGroup = $DInformation.Group 
        #Erstellen der Gruppen
        Get-Probe -Name $ProbeInput | Add-Group -Name $DeviceGroup
    }
}

#Checkt ob die Probe existiert und ob die entsprechenden Gruppen existieren und beginnt daraufhin mit der Device erstellung
if($ProbeCheck -ne $null)
{        
    ForEach ($DInformation in $ImportListDevices) 
    {
        #CSV Inhalt den einzelnen Objekten zuordnen
        $DeviceGroup = $DInformation.Group 
        $DeviceName = $DInformation.Name
        $DeviceHost = $DInformation.Host
        $GroupCheck = Get-Group -Name $DeviceGroup
          if($GroupCheck -ne $null)
          {
                #Device wieder der entsprechenden  Gruppe zuteilen und erstellen
                Get-Group -Name $DeviceGroup | Add-Device -Name $DeviceName -Host $DeviceHost
          }        
    }
}

What is the output of Get-PrtgClient -Diagnostic?

# 
PS C:\WINDOWS\system32> Get-PrtgClient -Diagnostic
PSVersion      : 5.1.18362.145
PSEdition      : Desktop
OS             : Microsoft Windows 10 Enterprise
PrtgAPIVersion : 0.9.11
Culture        : de-DE
CLRVersion     : .NET Framework 4.8 (528040)
PrtgVersion    : 19.4.54.1506
PrtgLanguage   : german.lng

Additional context Anything else I should know to help solve the issue? I attached the .csv files i'm using with some information redacted, but i hope you get the general layout.

PRTG_Export_Devices_example.zip PRTG_Export_Groups_example.zip

lordmilko commented 4 years ago

Hi @LKus95,

In order to figure out what is going on here I think we should reduce this code to a minimal example that reproduces the issue.

For example, does the following create the device on every probe

Get-Probe -Id 1 | Add-Device dc-1

while the following creates the device on only a single probe?

Get-Probe "Local Probe" | Add-Device dc-2

If you can potentially figure out what code exactly is required, without Read-Host user inputs, loops, if-statements and CSVs, this will allow us to cut straight to the heart of the issue

Regards, lordmilko

LKus95 commented 4 years ago

Good morning lordmilko,

thanks for the quick reply. I tested it with your examples and both create a single device under my first probe (master).

PS C:\WINDOWS\system32> Get-Probe -Id 1 | Add-Device dc-1

Name                        Id     Status      Host            Sensors    Group                     Probe                   
----                        --     ------      ----            -------    -----                     -----                   
dc-1                        6167   Up          dc-1            0          Netmon24_TestLK           Netmon24_TestLK         

PS C:\WINDOWS\system32> Get-Probe "Netmon24_TestLK" | Add-Device dc-2

Name                        Id     Status      Host            Sensors    Group                     Probe                   
----                        --     ------      ----            -------    -----                     -----                   
dc-2                        6169   Up          dc-2            0          Netmon24_TestLK           Netmon24_TestLK         

Regards, Lukas

EDIT: In the Meantime i've split my Script in one creating Groups and one creating Devices, which both produce the same Results from above; Tested with ID and Name.

lordmilko commented 4 years ago

Hi @LKus95,

If you specify the -Verbose parameter to any PrtgAPI cmdlet you can see the exact API requests that are executed for any given command. For more information on this please see the wiki

Regardless of whether you retrieve probes by ID or name, the resultant probe's ID is what is actually used to specify what the parent of the newly created device should be.

Based on the results above I would say the behavior you were initially seeing with devices being created across all of your probes is due to a bug in your script. If you can successfully reduce your script to a minimal example that still exhibits issue we can revisit this; otherwise for now I would say this issue is resolved

Please let me know if you have any further issues

Regards, lordmilko