A Post-Clone script to Deactivate and Cancel Discovery Schedules


Description

Discovery Schedules will be copied as part of a clone. They will then run on the same schedule on the clone target instance, and may cause performance problems for the instance, MID Servers of the target instance, or the devices and servers that are being Discovered due to the unwanted additional probes running against those IPs. Customers generally don't need or want the CMDB updated so regularly or at all on sub-production instances.

This KB provides a way of automatically turning off Discovery jobs on any sub-prod instances immediately after a clone.

Release or Environment

Any.

Instructions

This solution leverages the Post-Clone Cleanup Script platform feature. More details of what that is are in the docs.

  1. Navigate to: System Clone -> Clone Definition -> Cleanup Scripts
  2. Click New
  3. Name: De-activate And Stop Discovery Schedules
    Script: (see below)
  4. Submit
/*
Custom post-clone cleanup script based on Service-Now KB0789119
David Piper, 12/12/2019

This will deactivate any scheduled discoveries on the clone target after the clone, and stop any that are already started.
*/
deactivateAndStopDiscoverySchedules();

function deactivateAndStopDiscoverySchedules() {
	// De-activate Discovery Schedules
	var schedules = new GlideRecord('discovery_schedule');
	schedules.addEncodedQuery("active=true^disco_run_typeINdaily,weekly,monthly,periodically,weekdays,weekends,month_last_day,calendar_quarter_end");
	schedules.query();
	while (schedules.next()) {
		schedules.active = false;
		schedules.update();
	}
	
	// Stop any running schedules
	var statuses = new GlideRecord('discovery_status');
	statuses.addEncodedQuery('stateINStarting,Active');
	statuses.query();
	while (statuses.next()) {
		var dac =  new SncDiscoveryCancel();
		dac.cancelAll(statuses.sys_id);
	}
}

Alternatively, an update set is attached containing this script.