execute command on management server - string not passed correctly

Started by AP_ops, September 18, 2014, 12:39:06 PM

Previous topic - Next topic

AP_ops

Hi

I'm struggeling to get the Action Configuration "execute command on management server" working as it should. I'd like to have the %m Message forwarded as a String to my script. What happens is that only the first word gets forwarded.

My command:
powershell C:\\NetXMS\\send_sms.ps1 \-recipients "00417xxxxxxxx" \-msg %m

if i use "%m" nothing works also if i use single quotes. But if I use ' [%[get_first_parent]][%n] [%S]: [%[get_event_name]] ' it gets forwarded..

The same behavior was discussed in the topic in 2011 but not solved:
https://www.netxms.org/forum/configuration/parameters-with-spaces-on-execute-command-on-management-server/

Where is my mistake?

Thanks for your help!


Victor Kirhenshtein

Hi,

"%m" should work, although I didn't test it specifically with powershell scripts. When I test it with the following cmd script:


@echo off
echo %2 >> c:\temp\cmd.log


and action configured as

c:\\temp\\test.cmd parameter_1 "%m"

file c:\temp\cmd.log contains records like

"Node status changed to UNMANAGED"

Note that cmd script interprets " as part of parameter. Can you create a simple powershell script which will just log all command line parameters it got?

Best regards,
Victor

AP_ops

Hello Victor

Thx for your reply. I just tested if your test with the batch-file works in my configuration. It is exactly the same behaviour as with the powershell script... if I configure the action as:
c:\\temp\\test.cmd parameter_1 "%m" it won't get executed. If I remove the "" from the %m (c:\\temp\\test.cmd parameter_1 %m) it gets executed but I just receive the first word. In my case "Zone:"

Message form the event is configured as: Zone:\t%[get_first_parent]\nSystem:\t%n\nTime:\t\t%t\nSeverity:\t%S\n\n%2 is stopped!

We have the version 1.2.13 running.

Thx for help & best regards
Ap_ops

Victor Kirhenshtein

Hi,

I suppose that new line characters is the problem. I doubt that command line parser can handle them even inside quotes. Try to form message without new lines. If it helps, you can pass some other characters to indicate new lines and replace them in your script before sending SMS.

Best regards,
Victor

AP_ops

Hi,

Alright I made the recommended changes: I removed all \n and \t characters. The Batchfile works now. After this success I just checked with a simple powerShell script:

param(
       [String]$msg = $(throw "-msg is required.")
);

$error_log_path = "C:\log.txt"
"$(get-date) - "+ $msg | Out-File -Append $error_log_path


I Just receive: "Zone:Comp" instead of
"Zone:Comp XX NMS - 172.20.1.0/24System:[VM]Time:[22-Sep-2014 12:19:46]Severity:[Critical][TEST]isstopped!"

The Line above is created through "Zone:%[get_first_parent]System:[%n]Time:[%t]Severity:[%S][%2]isstopped!" event config.


Do you have any idea what could be the reason for that? Is is possible that the output is not a single string? Or the string is splitted in each space?

Best regards,
AP_ops

AP_ops

Hi,

I just found the reason...now its not NetXMS. One should know how to execute powershell from cmd environment. If you call a ps1 script fom cmd environment or in this case NetXMS you have to use "powershell -file -para "bla" -para2 "bla" " if no -file is used..just the first word is taken. It has to do with a security "feature"..

thanks for your help!


AP_ops

Hi Victor,

I know now how I could reformat all message texts to handover by commandline. But the option to avoid all /t /n characters is not the way with the least work as I use the formatted message texts mostly for messages by mail. Do you have any idea if it's possible to parse this characters in the script or if there is a way to solve this problem without building every message from scretch?

thanks for help
AP_ops

Victor Kirhenshtein

Hi,

I probably can add an escape mode for all external commands executed by server, which will replace new line characters with \n and so on.

Best regards,
Victor

AP_ops

Hi Victor

That would be a nice option. Thanks in advance! Please let us now when you have implemented this option.

Best regards,
AP_ops

Victor Kirhenshtein

Hi,

I've added configuration option EscapeLocalCommands. If it is set to 1, tab, carriage return, and new line characters will be replaced by \t, \r, and \n in commands. It will be available in release 1.2.17.

Best regards,
Victor

AP_ops

Hi Victor,

Thanks! Sounds really nice. We will upgrade as soon as we find time :)

Kind regards,
AP_ops