NetXMS Support Forum

English Support => General Support => Topic started by: maliodpalube on September 11, 2025, 02:19:12 PM

Title: Automatic Apply rules based on OS type
Post by: maliodpalube on September 11, 2025, 02:19:12 PM
Hello,

we are trying to apply templates based on OS type (linux/windows)

We have created and applied following filtering script on Automatic Apply Rules tab on Template Object properties, but no targets have been added. Is there anything we are doing wrong?

if (!$object || !$object.isNode())
   return false;

pn = LowerCase($object.platformName);
if ((pn != null) && StrStartsWith(pn, "windows"))
   return true;

sd = LowerCase($object.snmpSysDescription);
return (sd != null) && (StrFind(sd, "windows") != -1);

Title: Re: Automatic Apply rules based on OS type
Post by: Filipp Sudanov on September 11, 2025, 03:07:15 PM
Here's a fixed version:

if (!$node)
   return false;

pn = $object.platformName.toLowerCase();
if (pn != null && pn.startsWith("windows"))
   return true;

sd = $object.sysDescription.toLowerCase();
return (sd != null && sd.contains("windows"));

$object always exists, so there's no need to check for that. NXSL never had LowerCase() or StrStartsWith() functions, did you use an AI to generate this?
Title: Re: Automatic Apply rules based on OS type
Post by: maliodpalube on September 11, 2025, 03:11:19 PM
Yes,Ai was used to create snippet.

Also How can we test it/force it to show results? Or is the only option to wait?
Title: Re: Automatic Apply rules based on OS type
Post by: Filipp Sudanov on September 11, 2025, 03:15:18 PM
You can easily test it - just take any node, select Execute script from the context menu and paste your script there. Your script will be executed in the same way as when a template executes it on nodes.

In addition, when you execute it that way you can use println() commands to print, so you can use it for debugging purposes.
Title: Re: Automatic Apply rules based on OS type
Post by: maliodpalube on September 11, 2025, 04:12:52 PM
We were able tu use shorter code snippet

return $node->isAgent && $node->platformName like "windows-*";
It returns the same output.

Thx for the help
Title: Re: Automatic Apply rules based on OS type
Post by: Filipp Sudanov on September 11, 2025, 05:17:19 PM
It's better to add check for $node:

return $node && $node->isAgent && $node->platformName like "windows-*";

the reason is that there are some other types of object - clusters, sensors, collectors on which templates can also be applied, but there will be no $node on these objects.
Title: Re: Automatic Apply rules based on OS type
Post by: maliodpalube on September 12, 2025, 10:36:18 AM
Ok, changed the code, thx again for the info ;)