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.

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.
const msg = "Hello World!!"; console.log(msg);

output: Hello World!!
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!");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With