Script to add relationship between windows server and Ip addresses

Miran Alam
Tera Contributor

Hi,

 

I need help doing a ONE time job (Background script/fix script). I need to make a script that:

 

1. Copies the Ip address from cmdb_ci_win_server record and creates a new record for each windows server with Ip address in cmdb_ci_ip_address table. (This part is done) See below script:

var oldRecord = new GlideRecord('cmdb_ci_win_server');
oldRecord.query("sys_id", "27e3a47cc0a8000b001d28ab291fa65b");
oldRecord.setLimit(1);
while (oldRecord.next()) {
  var newRecord = new GlideRecord('cmdb_ci_ip_address');
  newRecord.initialize();
  newRecord.ip_address = oldRecord.ip_address.toString();
newRecord.insert();
gs.info("value:" + newRecord.ip_address
);
}
}

 

Now I need help with a script that adds the Ip address as child for the windows server with below type. Dynamically for each ip_address.

 [parent] =  Sys ID [u_windows_server_1.sys_id] [cmdb_ci_win_server]
[type] = "Owns::Owned by" [cmdb_rel_type]
[child] = <the IP Address CI I created in first step > [cmdb_ci_ip_address]

 

Would really appreciate any input here! 

Thanks

1 ACCEPTED SOLUTION

@Miran Alam 

To make the first step dynamic so that it runs through all the Windows servers that have IP addresses, you can modify the script to query the cmdb_ci_win_server table for all records that have an IP address.

Here's how you can modify the script(add condition based on your requirement):

// Step 1: Create IP address records and relationships
var winServer = new GlideRecord('cmdb_ci_win_server');
winServer.addQuery('ip_address', 'ISNOTEMPTY');
winServer.query();
while (winServer.next()) {
  var ipAddress = new GlideRecord('cmdb_ci_ip_address');
  ipAddress.initialize();
  ipAddress.ip_address = winServer.ip_address.toString();
  ipAddress.insert();

  var relRecord = new GlideRecord('cmdb_rel_ci');
  relRecord.initialize();
  relRecord.parent = winServer.sys_id.toString();
  relRecord.child = ipAddress.sys_id.toString();
  relRecord.type = 'Owns::Owned by';
  relRecord.insert();

  gs.info("IP address " + ipAddress.ip_address + " added as child of Windows server " + winServer.name);
}

 

View solution in original post

4 REPLIES 4

DUGGI
Giga Guru

@Miran Alam 

 

try this

 

You can use a GlideRecord to create a relationship between the IP address and the Windows server. Here's how you can modify your script to add the IP address as a child of the Windows server:

csharpCopy code
// Step 1: Create IP address records
var oldRecord = new GlideRecord('cmdb_ci_win_server');
oldRecord.query("sys_id", "27e3a47cc0a8000b001d28ab291fa65b");
oldRecord.setLimit(1);
while (oldRecord.next()) {
  var newRecord = new GlideRecord('cmdb_ci_ip_address');
  newRecord.initialize();
  newRecord.ip_address = oldRecord.ip_address.toString();
  newRecord.insert();

  // Step 2: Create relationship between IP address and Windows server
  var relRecord = new GlideRecord('cmdb_rel_ci');
  relRecord.initialize();
  relRecord.parent = oldRecord.sys_id.toString();
  relRecord.child = newRecord.sys_id.toString();
  relRecord.type = 'Owns::Owned by';
  relRecord.insert();

  gs.info("IP address " + newRecord.ip_address + " added as child of Windows server " + oldRecord.name);
}

Miran Alam
Tera Contributor

Thanks for quick response. How do I make the first step dynamic so it will not take only a specific sys_id. 
I want it ro run through all the windows server that has ip addresses and then add the relationships in second step

@Miran Alam 

To make the first step dynamic so that it runs through all the Windows servers that have IP addresses, you can modify the script to query the cmdb_ci_win_server table for all records that have an IP address.

Here's how you can modify the script(add condition based on your requirement):

// Step 1: Create IP address records and relationships
var winServer = new GlideRecord('cmdb_ci_win_server');
winServer.addQuery('ip_address', 'ISNOTEMPTY');
winServer.query();
while (winServer.next()) {
  var ipAddress = new GlideRecord('cmdb_ci_ip_address');
  ipAddress.initialize();
  ipAddress.ip_address = winServer.ip_address.toString();
  ipAddress.insert();

  var relRecord = new GlideRecord('cmdb_rel_ci');
  relRecord.initialize();
  relRecord.parent = winServer.sys_id.toString();
  relRecord.child = ipAddress.sys_id.toString();
  relRecord.type = 'Owns::Owned by';
  relRecord.insert();

  gs.info("IP address " + ipAddress.ip_address + " added as child of Windows server " + winServer.name);
}

 

Miran Alam
Tera Contributor

Two things with above script.
I changed it to .addEncodedQuery, addQuery did not work. I dont know why

winServer.addEncodedQuery("ip_addressISNOTEMPTY");

Other thing on the last part relRecord.type = '[sys_id of the type need to be inserted here]'

 

Works fine! thank u so much Duggi! 😄