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

Powershell Quotes

Votes:

0

Hey,

APRIL 2018 – VERSION 18.2.39 Note: The fix required changes to the handling of command line parameters. In certain cases, it will be necessary to adapt quoting with backticks (``) in the parameters field because PRTG will process quotes more accurately. Please use single or double quotes to escape special characters and whitespaces in the parameters fields.

Any Idea how to reformat the below

-Commands  "text1","text2","cat /proc/analog/value4" -RemoteHost "172.20.1.188"

Problem seem to be that I cant escape the SPACE between the multiple quote sets

Thanks,

escape powershell telnet

Created on Apr 16, 2018 2:41:56 AM

Last change on Apr 16, 2018 6:51:14 PM by  Dariusz Gorka [Paessler Support]



16 Replies

Votes:

0

Hi there,

Should the following be an array or is this entire string one command?

"text1","text2","cat /proc/analog/value4"

Please note that this is not the correct way to save values into an array within PowerShell.

Best regards.

Created on Apr 16, 2018 6:53:20 PM by  Dariusz Gorka [Paessler Support]



Votes:

0

Not sure what the best answer to that is

they are separate values, used one after another as they are piped to a telnet device, however when i wrote the script it was easier to write it as above

do you have an example of passing a array to powershell? and parsing ?

Thanks,

Created on Apr 16, 2018 7:02:03 PM



Votes:

0

Hi there,

It would be interesting to see what happens within the script. Is the actual variable ""text1","text2","cat /proc/analog/value4""?

In the meantime please try the following:

-Commands '"text1","text2","cat /proc/analog/value4"' -RemoteHost "172.20.1.188"


Does that work?

Best regards.

Created on Apr 16, 2018 7:30:17 PM by  Dariusz Gorka [Paessler Support]



Votes:

0

I will test now however script is below

purpose is to retrieve a value from a linux device via telnet

I know password and username in a script are bad practice but leaving as is

  1. Example
  2. -RemoteHost "192.168.0.55" -Commands "username","password","cat /proc/analog/value1"
Param (
        [Parameter(ValueFromPipeline=$true)]
        [String[]]$Commands = @("username","password","disable clipaging","sh config"),
        [string]$RemoteHost = "HostnameOrIPAddress",
        [string]$Port = "23",
        [int]$WaitTime = 1200
    )
    #Attach to the remote device, setup streaming requirements
    $Socket = New-Object System.Net.Sockets.TcpClient($RemoteHost, $Port)
    If ($Socket)
    {   $Stream = $Socket.GetStream()
        $Writer = New-Object System.IO.StreamWriter($Stream)
        $Buffer = New-Object System.Byte[] 1024 
        $Encoding = New-Object System.Text.AsciiEncoding

        #wait for session to stablize
        Start-Sleep -Milliseconds $WaitTime

        #Now start issuing the commands
        ForEach ($Command in $Commands)
        {   $Writer.WriteLine($Command) 
            $Writer.Flush()
            Start-Sleep -Milliseconds $WaitTime
        }
        #All commands issued, but since the last command is usually going to be
        #the longest let's wait a little longer for it to finish
        Start-Sleep -Milliseconds ($WaitTime * 4)
        $Result = ""
        #Save all the results
        While($Stream.DataAvailable) 
        {   $Read = $Stream.Read($Buffer, 0, 1024) 
            $Result += ($Encoding.GetString($Buffer, 0, $Read))
        }
    }
    Else     
    {   $Result = "Unable to connect to host: $($RemoteHost):$Port"
    }

    $next = 0 
    foreach ($line in $Result.Split("`n") ){
        if ($next -eq 1){
            $final= [double]$line
            $far = ($final * 1.8)+32

            
            $x=[string]$far+":The Current Value is "+$far+" thanks."
            write-host $x
            
            exit 0
        }

        if ($line.Contains("# cat /proc/")){
            $next = 1
        }

    }


exit 0

Created on Apr 16, 2018 7:34:07 PM

Last change on Apr 16, 2018 7:34:07 PM



Votes:

0

Nope

External EXE/Script did not return a response (code: PE087)

what i suspect is happening is that the unix box is running

cat without the value that is meant to be returned

FYI

I have tried ' " \

and various combinations unsuccessfully before reaching out

Thanks,

Created on Apr 16, 2018 7:37:55 PM



Votes:

0

Hi there,

I am able to output the data properly that is transmitted as a parameter. Parameters within PRTG:

-Commands '"text1","text2","cat /proc/analog/value4"'

Test-Script:

param(
[String[]]$Commands = @("username","password","disable clipaging","sh config")
)

Write-Host @"
<PRTG>
  <result>
    <channel>Test</channel>
    <value>1234</value>
  </result>
  <text>
  $($Commands)
  </text>
</PRTG>
"@

The sensor will then output the following in the message:

"text1","text2","cat /proc/analog/value4"

Therefore I suspect that something is wrong with the script itself or that some kind of issue happens within the script. Please activate the "Write EXE result to disk" option in the sensor's settings and post the log files (Result of Sensor XXX.Data.txt and Result of Sensor XXX.txt) located on the corresponding probe under "C:\ProgramData\Paessler\PRTG Network Monitor\Logs (Sensors)".

Best regards.

Created on Apr 16, 2018 7:48:13 PM by  Dariusz Gorka [Paessler Support]



Votes:

0

FYI

Test Script

   Write-Host $Commands[0]
    #exit 0

Test Input

.\TelnetMFIcatvalue.ps1 -Commands  '\"ubnt\",\"ubnt\",\"cat /proc/analog/value4\"'  -RemoteHost 172.20.1.188
Result 
\"ubnt\",\"ubnt\",\"cat /proc/analog/value4\"

Expected Result single first variable - ubnt

Created on Apr 16, 2018 7:58:36 PM

Last change on Apr 16, 2018 8:02:27 PM by  Dariusz Gorka [Paessler Support]



Votes:

0

Hi there,

I think I got it. Can you try the following instead:

'text1','text2','cat /proc/analog/value4'

So basically I switched all double-quotes with single-quotes and the test script only outputs "text1".

Best regards.

Created on Apr 16, 2018 8:08:50 PM by  Dariusz Gorka [Paessler Support]

Last change on Apr 16, 2018 8:08:57 PM by  Dariusz Gorka [Paessler Support]



Votes:

0

it seems to confuse which parameter is which here

Data['environment'].asString := '';
Data['exefile'].asString := 'TelnetMFIcatvalue.ps1';
Data['exeparams'].asString := '-Commands  'ubnt','ubnt','cat /proc/analog/value4'  -RemoteHost '172.20.1.188'';
Data['exeparamscache'].asString := '-Commands  'ubnt'`,'ubnt'`,'cat /proc/analog/value4'  -RemoteHost '172.20.1.188'';
Data['exeparamshash'].asString := '0be9715e67aeaae989eb16e4205b2d88b92792c9';
Data['fastcount'].asString := '0';
Data['host'].asString := '172.20.0.23';
Data['hostv6'].asString := '';
Data['inerror'].asString := '1';
Data['interfacenumber'].asString := '';
Data['inum'].asString := '';
Data['ipversion'].asString := '0';
Data['isexesensor'].asString := '1';
Data['lastmsg'].asString := '#O132 (New-Object : Cannot convert argument "1", with value: ",ubnt,cat  /proc/analog/value4", for "TcpClient" to type "System.Int32": "Cannot convert  value ",ubnt,cat /proc/analog/value4" to type "System.Int32". Error: "Input  string was not in a correct format."" At C:\Program Files (x86)\PRTG Network Monitor\custom  sensors\EXE\TelnetMFIcatvalue.ps1:22 char:15 + ...   $Socket = New-Object System.Net.Sockets.TcpClient($RemoteHost, $Por ... +                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~     + CategoryInfo          : InvalidOperation: (:) [New-Object], MethodExcept     ion     + FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.Power     Shell.Commands.NewObjectCommand   )';
Data['lastuptime'].asString := '0';

Created on Apr 16, 2018 8:16:10 PM

Last change on Apr 16, 2018 8:17:31 PM by  Dariusz Gorka [Paessler Support]



Votes:

0

Hi there,

I can't reproduce this, the parameters are correctly transmitted to the script. Parameters within PRTG:

-Commands 'ubnt','ubnt','cat /proc/analog/value4' -RemoteHost '172.20.1.188'

PowerShell Test Script:

param(
[String[]]$Commands = @("username","password","disable clipaging","sh config"),
$RemoteHost = ""
)

Write-Host @"
<PRTG>
  <result>
    <channel>Test</channel>
    <value>1234</value>
  </result>
  <text>
  $($Commands[0]) - $($RemoteHost)
  </text>
</PRTG>
"@

Can you output the variable "Commands" and "RemoteHost" in the script that is executed by PRTG to see what values they hold?

Best regards.

Created on Apr 16, 2018 8:25:25 PM by  Dariusz Gorka [Paessler Support]



Votes:

0

sure see below

seperately are you running the same PRTG verison as this issues only started when i did the latest update

Thanks,

script =======================

#Example
#-RemoteHost "192.168.0.55" -Commands  "username","password","cat /proc/analog/value1"

Param (
        [Parameter(ValueFromPipeline=$true)]
        [String[]]$Commands = @("username","password","disable clipaging","sh config"),
        [string]$RemoteHost = "HostnameOrIPAddress",
        [string]$Port = "23",
        [int]$WaitTime = 1200
    )

    #Write-Host $Commands[1]
    Write-Host $Commands
    Write-Host $RemoteHost
    #exit 0

log =================================

Data['exefile'].asString := 'TelnetMFIcatvalue.ps1';
Data['exeparams'].asString := '-Commands  'ubnt','ubnt','cat /proc/analog/value4'  -RemoteHost '172.20.1.188'';
Data['exeparamscache'].asString := '-Commands  'ubnt'`,'ubnt'`,'cat /proc/analog/value4'  -RemoteHost '172.20.1.188'';
Data['exeparamshash'].asString := '0be9715e67aeaae989eb16e4205b2d88b92792c9';
Data['fastcount'].asString := '0';
Data['host'].asString := '172.20.0.23';
Data['hostv6'].asString := '';
Data['inerror'].asString := '1';
Data['interfacenumber'].asString := '';
Data['inum'].asString := '';
Data['ipversion'].asString := '0';
Data['isexesensor'].asString := '1';
Data['lastmsg'].asString := '#O132 (ubnt 172.20.1.188 New-Object : Cannot convert argument "1", with value: ",ubnt,cat  /proc/analog/value4", for "TcpClient" to type "System.Int32": "Cannot convert  value ",ubnt,cat /proc/analog/value4" to type "System.Int32". Error: "Input  string was not in a correct format."" At C:\Program Files (x86)\PRTG Network Monitor\custom  sensors\EXE\TelnetMFIcatvalue.ps1:23 char:15 + ...   $Socket = New-Object System.Net.Sockets.TcpClient($RemoteHost, $Por ... +                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~     + CategoryInfo          : InvalidOperation: (:) [New-Object], MethodExcept     ion     + FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.Power     Shell.Commands.NewObjectCommand   )';
Data['lastuptime'].asString := '0';

Created on Apr 16, 2018 8:29:04 PM

Last change on Apr 16, 2018 8:40:50 PM by  Dariusz Gorka [Paessler Support]



Votes:

0

Hi there,

The log does not seem to have changed. However you should see the result after one scan in the other log file as the Data File only shows the used options.

Yes I am also using the same version I am aware of that change. :)

Best regards.

Created on Apr 16, 2018 8:41:47 PM by  Dariusz Gorka [Paessler Support]



Votes:

0

Thanks had to check :) ubnt 172.20.1.188 New-Object : Cannot convert argument "1", with value: ",ubnt,cat /proc/analog/value4", for "TcpClient" to type "System.Int32": "Cannot convert value ",ubnt,cat /proc/analog/value4" to type "System.Int32". Error: "Input string was not in a correct format."" At C:\Program Files (x86)\PRTG Network Monitor\custom sensors\EXE\TelnetMFIcatvalue.ps1:23 char:15 + ... $Socket = New-Object System.Net.Sockets.TcpClient($RemoteHost, $Por ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [New-Object], MethodExcept ion + FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.Power Shell.Commands.NewObjectCommand

Created on Apr 16, 2018 8:50:24 PM

Last change on Apr 16, 2018 8:55:22 PM by  Dariusz Gorka [Paessler Support]



Votes:

0

Hi there,

I am afraid that we run in circles. :(

Please contact us, with referring to this thread, under "[email protected]". Please forward us the used Script file, both log files and screenshots of the sensor settings so we can check this further.

Best regards.

Created on Apr 16, 2018 8:58:03 PM by  Dariusz Gorka [Paessler Support]



Votes:

0

any updates for this issue?

Created on Apr 27, 2018 8:17:37 AM



Votes:

0

Hi Timo,

Due to the recent update and security additions to the parameter exchange, it is no longer possible to fill arrays via the parameters. This means that each value needs its own parameter. Before:

-parameter "Value1","Value2"

Now:

-value1 "Value1" -value2 "Value2"


Another possibility is to use "Split" within the script to split the parameter:

-parameter "Value1,Value2,Value3"

In the script it then will look like this:

$parameter.split(",")



Best regards.

Created on Apr 27, 2018 10:13:04 AM by  Dariusz Gorka [Paessler Support]




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.