lordmilko / PrtgAPI

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

Clone-Object parameter handling (in powershell) differs from documentation #264

Open terminatorbs opened 2 years ago

terminatorbs commented 2 years ago

Hi there

I am trying to add devices in bulk, and since Clone-Device lets me add them with pre-configured sensors that's what I'm going with. I am using Powershell with the provided .bat for this.

However, whenever I attempt to clone one, the new device does not have the expected specified properties, namely Device Name and Hostname. I'm attaching a screenshot of the issue, you can see in the returned device the properties do not match what's in the command. It appears it "skips" the Name part?

image

From reading the documentation, it appears to me that this is supposed to work, but in my case I had to add another parameter to make it work. Here a screenshot of the command producing the expected result:

image

Documentation I'm referring to: https://github.com/lordmilko/PrtgAPI/wiki/Object-Creation#cloning-1 Namely the following part:

# Clone the device with ID 1000 into the group or probe with ID 2001 naming the
# device "dc-2" and setting its hostname to "dc-2.contoso.local"
$newDevice = Get-Device -Id 1000 | Clone-Object -DestinationId 2001 "dc-2" "dc-2.contoso.local"

This is with the latest PrtgAPI downloaded today, against PRTG 21.3.70.1629, command run on a Windows Server 2019 build 17763

lordmilko commented 2 years ago

Hi @terminatorbs,

I've managed to successfully reproduce this issue

In the DeviceToDestination parameter set

When parameter binding is happening however, the PowerShell engine appears to be assigning both parameters 0 and 1 to -Name (-Name is parameter 0 in the Manual parameter set).

Once it becomes apparent that the parameter set is not Manual and what would normally be parameter 0 (-DestinationId) was specified as a named parameter, you would expect the PowerShell engine would figure out that the the remaining parameters 0 and 1 now map to 1 and 2 (-Name and -Host), however that is clearly not what is happening.

I'll have to figure out how I can potentially modify this cmdlet so that this works as expected without breaking of the any other parameter sets on this cmdlet

lordmilko commented 2 years ago

Note that you can work around this issue for now by explicitly specifying the names of the parameters you're trying to use

Get-Device -Id 45503 | Clone-Object -DestinationId 45502 -Name "ACU02" -Host "10.100.47.20"
terminatorbs commented 2 years ago

Thank you for the info! Yes, that's definitely a more solid solution than my workaround of adding a useless parameter, i'll adjust my script so it doesn't break when this is fixed.

Also, this might not be the place for it, but thank you so much for the work you put into this. I really appreciate the thorough documentation, and this project will save me tons of work.

Silex commented 2 years ago

@lordmilko: seems there is still another bug:

PS C:\> Get-Module

Binary     0.9.17     PrtgApi                             {Add-Device, Add-Group, Add-NotificationTrigger, Add-Sensor...}                                            

PS C:\> Clone-Object -SourceId $template.Id -DestinationId $destination.Id -Name $ServerHostname -Host $ServerIp

Copy-Object : Parameter set cannot be resolved using the specified named parameters.

At line:1 char:1

+ Clone-Object -SourceId $template.Id -DestinationId $destination.Id -N ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Copy-Object], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : AmbiguousParameterSet,PrtgAPI.PowerShell.Cmdlets.CloneObject

EDIT ok it works if I use the exact same form as you (piping the source).