In a WMI sensor, how do I store the value returned from a registry key to a field in the CI record?

HugoFirst
Kilo Sage

Context: Discovering a Windows Server using WMI/Powershell.

I need to populate a field named u_patch_host from a value stored in the registry key

HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate\WUServer

In the WMI Fields related list to the WMI Probe: Windows - OS Information,   I add a field with the WMI patch set to the registry key named above.

The package is set to "Global" and I'm unable to set it to "Core Automation".   Don't know why.

I ran a discovery of a Windows Server and the probe returns a server name as expected.

But I can't access the value in the sensor to save it to the proper field in the current CI record.

Can you give me any advice?   I can't even find an error in the log to debug this.

So I'm open to ideas and advice.

Thanks in advance for your help.

1 ACCEPTED SOLUTION

HugoFirst
Kilo Sage

I ended up filing an question/incident on HI and have a solution.


I was very close in my trial and error attempts.   Here is the code that actually works.   Instead of pasting in the entire script of the sensor, most of which is OOB,   I am providing the script sequence added which performs the function.   I follow up with a couple of observations from my experiments.



The code fragment for retrieving the value of a registry key in the sensor named "Windows - OS Information"



// This code is to set the Patch Cycle followed by the Patch Server


// Reference: https://community.servicenow.com/message/787029#787029


var regKey = "HKEY_LOCAL_MACHINE.SOFTWARE.Policies.Microsoft.Windows.WindowsUpdate"; // need dots between the levels.


var node = g_disco_functions.findRegistryNode(result.Registry, regKey);


      var patch_cycle = g_disco_functions.findNodeValueWithAttribute(node, "TargetGroup");


current.u_patch_cycle = patch_cycle;



Notes:



1. The registry key retrieved by the probe "Windows - OS Information"   is:


HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate\TargetGroup



2. When invoking findRegistgryNode, format the key with the following ideas in mind.


    - Replace the backslashes with dots ".".


    - Exclude the final node name.     This will be used in the invocation of findNodeValueWithAttribute.



3. store the resulting value in a field within the current object and it will be included in the CI record when it is updated.



As always, I'm happy to receive feedback from those more knowledgeable than myself.   Especially about any corrections or omissions.


View solution in original post

6 REPLIES 6

HugoFirst
Kilo Sage

I want to provide some more information in the hope that it triggers some thoughts which may lead to resolution.


Here is an image of the WMI probe "Windows - OS Information" to show the key I'm fetching.   Note that the package is "Global" and not "Core Automation".   I tried to change it, but I can't.


find_real_file.png



The probe is returning a value ( which is blurred in the image below )


find_real_file.png



In the sensor I added this line of code to the parseResult function:


current.u_patch_host = result.Registry.HKEY_LOCAL_MACHINE.SOFTWARE.Policies.Microsoft.Windows.WindowsUpdate.WUServer;



This did not work.



I researched other sensors and found code using a utility named g_disco_functions.   I tried the following code too.   but not dice 😞



              var patchServerKey = 'HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate';


              var node = g_disco_functions.findRegistryNode(result.Registry, patchServerKey);


              var patchServer = g_disco_functions.findNodeValueWithAttribute(node, "WUServer");


              current.u_patch_host = patchServer;



Can anyone give me any clues on how to debug a sensor?


Ryan Zulli
ServiceNow Employee
ServiceNow Employee

Hi Steve,



It may help to provide the whole sensor - as another example I got working take a look at this New Discovery sensor not able to read a registry node



Let me know if this helps, if not let me know and we can debug.



Thanks,-Ryan


Hello Ryan,   Thank you for the pointer!   I continue to pursue this with a "trial and error" approach.   You're link gave me an idea and the following code is the sensor script in its entirety.   My part is denoted by some comments.   Again, the name of the sensor is Windows - OS Information, and most of the script is OOB.   Note that the   gs.log comments do appear in the log and that code is running.   The value for patch_cycle, logged in line 36,   is null in the system log though.



new DiscoverySensor({


      process: function(result) {


              this.parseResult(result);


      },




      parseResult: function(result) {


              current.os_version = result.Win32_OperatingSystem.Version;


              current.os_service_pack = result.Win32_OperatingSystem.CSDVersion;




              var man = result.Win32_ComputerSystem.Manufacturer;


              var mod = result.Win32_ComputerSystem.Model;




              var mm = MakeAndModelJS.fromNames(man, mod, "hardware");


              current.manufacturer = mm.getManufacturerSysID();


              current.model_id = mm.getModelNameSysID();


              current.short_description = result.Win32_OperatingSystem.Description;




              // Contrary to what the name implies, this returns *OS* address width, not CPU


              var processors = g_array_util.ensureArray(result.Win32_Processor);


              current.os_address_width = processors[0].AddressWidth;




              this.setAssignedTo(result);


//


// This code is to set the Patch Cycle


// Reference: https://community.servicenow.com/message/787029#787029


gs.log("Windows - OS   Information: Patch Cycle.   Got to 26");


var regKey = "HKEY_LOCAL_MACHINE.SOFTWARE.Policies.Microsoft.Windows.WindowsUpdate"; // try with dots between the levels.


var node = g_disco_functions.findRegistryNode(output.Registry, regKey);


      var patch_cycle = g_disco_functions.findNodeValueWithAttribute(node, "TargetGroup");


current.u_patch_cycle = patch_cycle;


gs.log("Windows - OS   Information: Patch Cycle.   Got to 31, patch_cycle = " + patch_cycle);


// end of code to set the Patch Cycle


      },




      setAssignedTo: function(result) {


              var userName = result.Win32_ComputerSystem.UserName;




              if (!gs.nil(userName)) {


                      var x = userName.indexOf("\\");




                      if (x > -1)


                              userName = userName.substring(x + 1);




                      var nameField = gs.getProperty('glide.discovery.assigned_user_match_field', "user_name");


                      var userID = GlideUser.getSysId(nameField, userName);


                     


                      var atOverwrite = JSUtil.toBoolean(gs.getProperty('glide.wmi.assigned_to_always_overwrite', "true"));


                      if (!atOverwrite)


                              if (this._isAssignedToFilled())


                                      return;


                             


                      current.assigned_to = userID;


              }


      },


     


      /*


        * Is the assigned_to field already populated? Return true if yes; false if no.


        */


      _isAssignedToFilled: function() {


              var compGr = this.getCmdbRecord();


              if (JSUtil.notNil(compGr.assigned_to + ''))


                      return true;


             


              return false;


      },




      type: "DiscoverySensor"


});


Ryan Zulli
ServiceNow Employee
ServiceNow Employee

What value(s) should we expect back from the windowsUpdate/TargetGroup registry entry?   I also do not get anything back when testing against my own Windows 2008 Server machine.   I was looking here for reference Configure Automatic Updates using Registry Editor