Server sys_id lookups for ServiceNow

I've created the below function to use in some of Cookdown's server-side ServiceNow scripts. If you're looking for a quick way to resolve a server name or FQDN to its ServiceNow sys_id this should do the trick.

You can find these sample scripts ServiceNow-Cookbook GitHub repo. In the short sample below, you can see where we've included the function in the script and then made a call to lookup our server Infra-Web01. This call will return the sys_id of the server to be used in any other records. The function supports either the netBIOS name or FQDN.

// The below line will return the sys_id, ready to be added to another record.lookupServer('infra-web01')function lookupServer(serverName, strictMode) {// We'll search against the server table, getting windows and linux serversvar serverGr = new GlideRecord('cmdb_ci_server');// Our query will look at the name and fqdn fields for an exact matchserverGr.addQuery('name', serverName);serverGr.addOrCondition('fqdn', serverName);serverGr.query();// When using strict mode, error for no serversif (serverGr.getRowCount() == 0 && strictMode) {throw "No server has been found.";}// When using strict mode, error for more than a single matchif (serverGr.getRowCount() > 1 && strictMode) {throw "More than one server was returned by the query.";}// .next() only moves to the record if one exists/matched the queryif (serverGr.next()) {// We can quickly pull the sys_id of the object to use in another field or confirm in the UIreturn serverGr.sys_id;}else {return null;}}

Previous
Previous

Coffee Break Webinar "Upgrading to SCOM 2019"

Next
Next

SCOM - Alert Basics