I am back writing a blog post about how I used the HTTP Data Collector API to fulfill a specific customer request.
My customer requested to create a computer group in OMS, whose membership was based on an attribute different from the computer name.
There was hence the need for a new custom attribute to be populated in OMS and to be later used in the Log Analytics to create a Computer Group.
Since the official documentation can be found on the HTTP Data Collector API TechNet page, I'll focus on the method I used to retrieve the data I need. Analyzing the PowerShell example on the TechNet page, I recognized 3 specific areas:
On the top of the script there's an area containing your workspace access information. Here, you have to customize this info before running the script.
In the center, there's the body or data part, used to retrieve your data. I am going to cover how to change this part in this post.
In the bottom part, you see the area containing the functions used to sign in and submit the data. I am going to refer to this part as the "fixed part"
Because I need to query my data to create records with different dynamic values (coming from the query results) and it is not possible to use variables with the syntax used in the sample script (which is using fixed values), how do I convert into Json something that is using variables?
- Create the data structure
- Populate the data structure as needed
- Convert the data structure to Json
So, in the sample script below (attachments link in the bottom), you can see my script doing the following:
- Setting the Workspace ID and Primary Key
- Setting the LogType with my own type (see "My body part" green arrow)
- Retrieving the data to be sent to OMS (see "My body part" green arrow)
- Creating and setting the PowerShell object containing the data (see "My body part" green arrow)
- Converting the PowerShell Object to Json (see "My body part" green arrow)
- Sending the data (using the Build-Signature and Post-OMSData functions and the Post-OMSData call from the "fixed part")
Running the above script manually, will upload a record with the following format:
This is just a prototype of the script I left to the customer. Hence, it must be considered as an example to explain how to retrieve a registry key to be used as part of a Computer Group Log Search Query like this one:
RegistryKey_CL | where (Value_s == "BR1-Role") | distinct Computer
- LogType: It represents the information type used in the query. Just remind that OMS will add the "_CL" suffix to it.
- TimeGenerated: This one is not mandatory the default value is the data ingestion timestamp (the timestamp when the data get uploaded).
- Body part: by appropriately changing the body part you can create a script that uploads different information such as ActiveDirectory attributes (Description, EmployeeID, etc), Active Directory OU location, number of files in a given path and so on.
- Key fields in the record: Use it if you like to have a more detailed result from your query. Here's why, in this example, I included the computer name.
Just for the purpose of demonstrating that you can really change just the body part, I am also attaching another sample script which gets the OU Attribute for the local computer (if joined to AD).
That's all folks, for now. I will cover how to make the data submission automatic in another post.