Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run NinjaRMM custom library script from console

Tags:

javascript

I need to run javascript code on an ninjaRMM managed device. I know how to do it using a powershell script,I have problems doing it with a javascript.

enter image description here

When this script is added to the ninjaRMM library and then run on a device, I get errors: syntax error:const; console.log:console is undefined

The above errors are signed with "Microsoft Script Host v5.81 which does support "const" keyword and I still can't figure out what is in the global namespace on that engine.

Googling has been hopeless: all I can find is info on accessing the Ninja API from a node server.

  1. Add simple hello world as new script in library in ninja console Administration=>Scripts:

const msg = "Hello World!!"; console.log(msg);

  1. Open device in the console=>Run=>from library=>my script
  2. Script returns with "success"
  3. clicking more in the status box yields: enter image description here

output: Hello World!!

like image 635
Steve H Avatar asked Dec 10 '25 08:12

Steve H


1 Answers

You might be thinking that Ninja's JavaScript functionality should work like Node.js, which is not the case. As you noticed, it is powered by Microsoft Script Host v5.81, which does not support console.log()

This code will execute without error, for example.

function someFunc() { return 0; } 
someFunc();

This code will achieve what you were looking for in this post.

WScript.Echo(message);

Now, I would really recommend switching to PowerShell for NinjaOne scripts that are going to execute against Windows devices, but if you are committed to JS, you could do something like this to preserve your muscle memory for common functionality.

function Console() {
}

Console.prototype.log = function(message) {
  WScript.Echo(message);
};

var console = new Console();
console.log("Hello, World!");
like image 197
Michael Greene Avatar answered Dec 11 '25 22:12

Michael Greene



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!