Hey Mark,
Here's an example of a Groovy script that queries WMI for a running service and adds a category to the device that can be used for AppliesTo. This one looks for the NTDS service (an Active Directory domain controller) and assigns category 'ADDS' to the device. The example AppliesTo for the below code would be 'hasCategory("ADDS")'. Credit due to Michael Rodrigues on our datasource team for the script (which I've re-used with different service and category names.) This is the exact code from an "Active Directory Domain Controller" PropertySource:
import com.santaba.agent.groovyapi.win32.WMI
import com.santaba.agent.groovyapi.win32.WMISession
// Set hostname
def hostname = hostProps.get('system.hostname');
// Form the full query.
def wmiQuery = "Select name,state from Win32_Service Where Name ='NTDS'";
try
{
// using default namespace
def session = WMI.open(hostname);
def result = session.queryFirst("CIMv2", wmiQuery, 10);
// Did we get anything?
if (result.STATE == "Running")
{
// Yes, apply the properties
println 'system.categories=ADDS'
}
}
catch(Exception e)
{
println e
return 1;
}
// Exit by returning 0.
return 0;
Cheers,
Kerry