lordmilko / PrtgAPI

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

issues with Get-Sensor -Id 17835 | Get-ObjectProperty -RawProperty query_ #33

Closed olsonnn closed 6 years ago

olsonnn commented 6 years ago

when i do: Get-Sensor -Id 17835 | Get-ObjectProperty -RawProperty query_

i get Get-ObjectProperty : '=' is an unexpected token. The expected token is ';'. Line 4, position 86. At line:1 char:24

as my "query" field contains multiple = character...

PS C:\WINDOWS\system32> Get-Sensor -Id 17830 | Get-ObjectProperty -Raw

query : /api/Queues?subscriptionId=blabla=blabla=blabla

olsonnn commented 6 years ago

sensor type = xml rest custom beta

Get-Sensor -Id 17835 | Set-ObjectProperty -RawProperty query_ -RawValue "/whatever = = " seems to work fine

lordmilko commented 6 years ago

Hi @olsonnn,

Can you please confirm what the exact value of your REST Query property is in the PRTG UI (replace any sensitive information with dummy values) and also display the output of $error[0].Exception.Stacktrace after encountering the exception? Then, can you also check for any inner exceptions and any inner inner exceptions ($error[0].Exception.InnerException / $error[0].Exception.InnerException.Stacktrace) and so on; this will allow me to identify where exactly an issue is occurring

When I assign the string "/api/Queues?subscriptionId=blabla=blabla=blabla" as the value of a REST Query I am successfully able to retrieve it with both Get-ObjectProperty -RawProperty query_ as well as see it defined on Get-ObjectProperty -Raw

olsonnn commented 6 years ago

here it is: query : /api/Queues?subscriptionId=<id>&resourceGroup=<name>&resourceName=<name>&TenantId=<id>&queueName=<queue>

olsonnn commented 6 years ago

i have no idea what to do with:

can you also check for any inner exceptions and any inner inner exceptions ($error[0].Exception.InnerException / $error[0].Exception.InnerException.Stacktrace) and so on; this will allow me to identify where exactly an issue is occurring

so please guide me here... what to do exactly

lordmilko commented 6 years ago

They are PowerShell commands. Execute them after encountering the exception


C:\> $error[0].Exception.Stacktrace

C:\> $error[0].Exception.InnerException

C:\> $error[0].Exception.InnerException.Stacktrace
lordmilko commented 6 years ago

I was successfully able to replicate the issue using the query you provided; I will look into this issue and get back to you

olsonnn commented 6 years ago

Ah nice! so this does report something as you found out already:

PS C:\WINDOWS\system32> $error[0].Exception.Stacktrace at System.Xml.XmlTextReaderImpl.Throw(Exception e) at System.Xml.XmlTextReaderImpl.HandleEntityReference(Boolean isInAttributeValue, EntityExpandType expandType, Int32& charRefEndPos) at System.Xml.XmlTextReaderImpl.ParseText(Int32& startPos, Int32& endPos, Int32& outOrChars) at System.Xml.XmlTextReaderImpl.FinishPartialValue() at System.Xml.XmlTextReaderImpl.get_Value() at System.Xml.Linq.XContainer.ReadContentFrom(XmlReader r) at System.Xml.Linq.XContainer.ReadContentFrom(XmlReader r, LoadOptions o) at System.Xml.Linq.XDocument.Load(XmlReader reader, LoadOptions options) at System.Xml.Linq.XDocument.Parse(String text, LoadOptions options) at PrtgAPI.Helpers.XDocumentHelpers.SanitizeXml(String str) at PrtgAPI.PrtgClient.GetObjectPropertyRaw(Int32 objectId, String property) at PrtgAPI.PowerShell.Base.PrtgProgressCmdlet.WriteObjectWithProgress(Func1 obj) at PrtgAPI.PowerShell.Cmdlets.GetObjectProperty.ProcessRecordEx() at PrtgAPI.PowerShell.Base.PrtgCmdlet.ExecuteWithCoreState(Action action) at System.Management.Automation.CommandProcessor.ProcessRecord() `

lordmilko commented 6 years ago

Hi @olsonnn,

I have successfully identified the cause of the issue. By default, the PRTG endpoint api/getobjectproperty.htm does not escape invalid XML characters (such as ampersands) unless you specify the parameter show=text. PrtgAPI does not currently do this. As such, when you attempt to query the property via the -RawProperty parameter you will encounter an exception if the property value contains any illegal character. The -Raw parameter executes a different request to retrieve its values which does escape invalid XML characters correctly, hence why it does not have any issue.

This issue will be corrected in PrtgAPI 0.9.0, due out in a month or two. In the meantime, you can access the query property by simply doing

(Get-Sensor -Id 1234 | Get-ObjectProperty -Raw).query

Regards, lordmilko

olsonnn commented 6 years ago

ok last question: how to do this: i want the sensor name in the part of the query: but didn't succeed

Get-Sensor -Id 17836 | Get-ObjectProperty -RawProperty name -OutVariable queue | Set-ObjectProperty -RawProperty query -RawValue "/api/Queues?subscriptionId=$queue"

tells me: Set-ObjectProperty : The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its properties do not match any of the parameters that take pipeline input. any recommendations?

lordmilko commented 6 years ago

Hi @olsonnn,

There are a few issues with your above command

The correct way to do this would either be to split this over two lines, or loop on the Sensor object and access the Name property inside the -RawValue expression

# Two lines
$sensor = Get-Sensor -Id 17836
$sensor | Set-ObjectProperty -RawProperty query_ -RawValue "/api/Queues?subscriptionId=$($sensor.Name)"

# One line
Get-Sensor -Id 17836 | foreach { $_ | Set-ObjectProperty -RawProperty query_ -RawValue "/api/Queues?subscriptionId=$($_.Name)" }

Please let me know if you have any issues

olsonnn commented 6 years ago

That was exactly what i needed! works! Is there also a way to prevent the "are you sure what you're doing?" :)

lordmilko commented 6 years ago

Specify -Force

lordmilko commented 6 years ago

Hi @olsonnn,

PrtgAPI 0.9.0 has now been released! Performing Get-ObjectProperty -Property / -RawProperty on any property should now work, regardless of whether it contains any invalid characters

To update to the latest version of PrtgAPI, simply run

Update-Module PrtgAPI

and reopen your PowerShell. Please be aware of any breaking changes that may affect you before considering upgrading.

Please let me know if you have any issues

Regards, lordmilko

olsonnn commented 6 years ago

Thanks for fixing! will test asap!

olsonnn commented 6 years ago

Issue solved! tested successfully