Some PDIs are currently unavailable, and PDI actions are paused. View the latest updates here. Read More

Background script to update the serial number for 225 Linux servers

RuthirakottA
Tera Contributor

Hi Everyone 

 

can someone help me to get the background script to update the serial number filed for 225 Linux servers

 

Thanks

Ruthirakotti A

3 REPLIES 3

Tanushree Maiti
Tera Patron

Hi @RuthirakottA 

 

try this :

 

var count = 0;
var grLinux = new GlideRecord('cmdb_ci_linux_server');
grLinux.addEncodedQuery('install_status=1^manufacturer=HardwareManufacturer^serial_number='); //Replace the encoded query below with the exact filter for your 225 servers
grLinux.setLimit(5); // Change to gr.setLimit(225) after validating your test run
grLinux.query();
while(grLinux.next()) {
grLinux.serial_number = 'YOUR_NEW_SERIAL_NUMBER'; //Set your serial no
grLinux.setWorkflow(false);
grLinux.update();
count++;
}
gs.info('Successfully updated ' + count + ' Linux server records.');

Please Accept the solution if it assisted you with your question & Mark this response as Helpful.
Regards
Tanushree Maiti
ServiceNow Technical Architect
LinkedIn: https://www.linkedin.com/in/tanushreemaiti

Ankur Bawiskar
Tera Patron

@RuthirakottA 

so what script did you start and where are you stuck?

Regards,
Ankur
Certified Technical Architect  ||  10x ServiceNow MVP  ||  ServiceNow Community Leader

RaghavendraGund
Tera Contributor

Hi Ruthirakotti,

Quick question first, since it changes the script: do you want all 225 servers to get the same serial number, or 225 distinct serials?

Before either approach, confirm you're actually targeting 225 records (a plain query hits the whole table, which may be more or fewer than 225). Run this on its own first:

var gr = new GlideRecord('cmdb_ci_linux_server');
gr.query();
gs.info('Records that will be updated: ' + gr.getRowCount());

If you need a subset, add an encoded query (e.g. gr.addEncodedQuery('...')) to scope it down.

Same serial for all:
var gr = new GlideRecord('cmdb_ci_linux_server');
gr.query();
var count = 0;
while (gr.next()) {
gr.serial_number = 'NEW_SERIAL_HERE';
gr.update();
count++;
}
gs.info('Updated ' + count + ' records.');

Distinct serials: this depends on where the values come from. If they follow a pattern, a formula works:
var gr = new GlideRecord('cmdb_ci_linux_server');
gr.query();
var count = 0;
while (gr.next()) {
gr.serial_number = 'SN-' + gr.getValue('name'); // pattern only
gr.update();
count++;
}
gs.info('Updated ' + count + ' records.');

But if these are 225 real serials from a spreadsheet, a background script isn't the best tool — an Import Set + Transform Map (coalescing on name or serial) is cleaner, repeatable, and gives you an audit trail. If you'd rather stay in script, you could load a name→serial mapping object and look each one up in the loop.

please Mark helpful, if my reply adds any value