Retrieve Asset ID using hostname

Is there a better way other than Asset Search to retrieve the ID of the asset? The search returns all details of the asset and it’s difficult to extract just the asset ID since the output has so many “id”: fields so regex is a pain.

1 Like

Hey @beth_whyle! Are you looking for a quick CLI way to do this or are you looking to accomplish it with a particular tool?

If CLI and you have curl/jq on your path, a quick way to do it would be like so:

curl -s -k -X POST 'https://localhost:3780/api/3/assets/search' \
  -H 'Accept: application/json;charset=UTF-8' \
  -H 'Authorization: Basic basicauth' \
  -H 'Content-Type: application/json'  \
  -d '{ "match": "all", "filters": [{"field": "host-name","operator": "is","value": "hostname"}]}' \
  | jq '.resources[] | {asset_id: .id, asset_mac: .mac}'

When using jq, it will iterate all responses in the resources list and then I have it printing out asset ID and MAC. You can also add any number of filters for the search criteria. Output will look similar to:

  "asset_id": 25,
  "asset_mac": "00:01:02:03:AB:CD"

Hey Zac, good to see your in this forum! I’m using the advanced regex plugin for InsightConnect. I created a workflow that grabs specific computers from AD based on their OU and then tags them in InsightVM. I have it working by using the non-greedy parameters in regex assuming the first id found is always the asset ID and not a scan ID. So bascially I take the output of “Get Asset Details” and search through via Advanced Regex to get the asset ID integer to pass to the “Tag Asset”. InsightConnect plugin for InsightVM has a get TagID if you pass it a string with the name of the tag but does not have the same for Assets.

@beth_whyle Could you give us a bit more details about the plugin action being used for Get Asset Details step? If you are using the Asset Search action, then it will return a list of assets and the result does provide the asset ID. If you were to use a loop step on the Asset Search response, then you could access the ID of each asset with something like: {{["Assets Loop"].[$item].[id]}}. Then you might be able to avoid advanced regex all together.

So the first step is to use the action “Asset Search” with a filter that searches host-name:

Step name is “Get Asset Details”
{“match”:“all”,“filters”:[{“field”:“host-name”,“value”:"{{[“Loop through computer names”].[$item].[1]}}",“operator”:“is”}]}

That step returns asset details which contains the ID and many other fields. If I try to use the output as so:

{{[“Get Asset Details”].[assets].[id]}}, it returns an empty value.

The output schema does have “id” in it but it does not return the value.

Screen Shot 2020-05-01 at 8.54.59 AM

I think I found the issue. It has to be reference like this: {{[“Get Asset Details”].[assets].[0].[id]}}

Thanks for the tip, saves me 2 steps!

1 Like