SOLVED

query multiple "contains"

Iron Contributor

Greetings Community,

 

I'm trying to come up with a way to query for multiple computers, but I have different strings to search for. For example:

Heartbeat
| where TimeGenerated >= ago(1h)
| where Computer contains 'ACOMPUTER1'
| summarize max(TimeGenerated) by Computer

 I can run this query but I have to execute it for a different string each time:

 

Heartbeat
| where TimeGenerated >= ago(1h)
| where Computer contains 'ACOMPUTER1'
| summarize max(TimeGenerated) by Computer
Heartbeat
| where TimeGenerated >= ago(1h)
| where Computer contains 'SERVERABC'
| summarize max(TimeGenerated) by Computer
Heartbeat
| where TimeGenerated >= ago(1h)
| where Computer contains 'THISMACHINE_B'
| summarize max(TimeGenerated) by Computer


Is there a way to go through multiple "contains" or "has" statements in a single query? Was thinking that I'd have to build an array in a function or something... any help is appreciated.

11 Replies

@Scott Allison 

 

Like this ?

Heartbeat
| where TimeGenerated >= ago(1h)
| where Computer in ('ACOMPUTER1', 'SERVERABC')
| summarize max(TimeGenerated) by Computer

or

let cList = dynamic(["ContosoASCAlert", "ContosoAzLnx1"]);
Heartbeat
| where TimeGenerated >= ago(1h)
| where Computer in (cList)
| summarize max(TimeGenerated) by Computer

 

or

 

Heartbeat
| where TimeGenerated >= ago(1h)
| where Computer startswith "cont"
| summarize max(TimeGenerated) by Computer

 

 

@CliveWatson No. I want to look in COMPUTER for multiple possible strings in a single query, much like the "contains" operator. For example, my "dream" query would have the following fake operator (contains_in):

Heartbeat
| where TimeGenerated >= ago(1h)
| where Computer contains_in ('ACOMPUTER1', 'SERVERABC')
| summarize max(TimeGenerated) by Computer

I know this doesn't exist, but I was hoping to fake it.

For background, we have 15,000 computers across multiple domains (and growing) and the computers mostly show up as FQDNs, but some as short names. Also, they are added in multiple cases (some all lower, some all upper). So a "Computer in" statement will never work for this scenario if we don't know the FQDN or if it is even listed as FQDN. The best way is to just search for the short name using "contains" or "has", but again, for multiple strings (I have a current use case for about 12 different strings).

@Scott Allison 

 

Heartbeat
| where Computer in ("ContosoASCAlert", "ContosoAzLnx1","ContosoWeb1.ContosoRetail.com") 
| extend Computer = split(Computer,".").[0]
| summarize max(TimeGenerated) by tolower(tostring(Computer))

What if we lowercase all machines and ignore the FQDN?   

@Scott Allison 

 

Heartbeat
| where Computer in~ ("ContosoASCALERT", "ContosoAzLnx1","ContosoWeb1.ContosoRetail.com") 
| extend Computer = split(Computer,".").[0]
| summarize max(TimeGenerated) by  tostring(Computer)

 Sorry, best practice would be to use "in~" 

@CliveWatson 

Returns zero results because the "in~" string operator means 'Equals to one of the elements' (according to documentation).

 

I do not need "equals"... I need "contains". I am not looking for full names--only partials. And again, about 12 different strings I want to search. If I could simply provide a list of strings to search, and then have the query look at each of those strings and find matches in the Computer column, that's all I need.

best response confirmed by Scott Allison (Iron Contributor)
Solution

@Scott Allison 

 

Sorry for being slow on the uptake, string is the search criteria (or pattern match you want) within the computer name column? e.g.

Heartbeat
| extend CompBucket = case(Computer contains "aks", Computer, 
                           Computer contains "Con", Computer
                           ,"")
| where isnotempty(CompBucket)

 

or 

 

 

Heartbeat
| where Computer contains "aks" 
     or Computer contains "Con"
| project Computer
Gotcha... it's all a little onerous, but I guess it's what I've got. It'd be nice to send an array instead of "or" or "case" statements. I'll add that as an enhancement.

Thanks again Clive!

@Scott Allison I come across the same problem. Can you please share the solution based on array? 

@Scott Allison 

Maybe you can use the operator has_any.

let ComputerTerms = pack_array('abcd', 'xyz0');
datatable (Computer:string)['abcd.123.com', 'def.xyz0.org', 'ijk.com']
| where Computer has_any (ComputerTerms)

Links to the Kusto query documentation:

@steffen_zeidler Thanks.

has_any works in the case where you're matching a FULL word within a string.
so "the quick brown fox" - you can match on any of those words but not a partial word like "bro".
So great suggestion steffen!
1 best response

Accepted Solutions
best response confirmed by Scott Allison (Iron Contributor)
Solution

@Scott Allison 

 

Sorry for being slow on the uptake, string is the search criteria (or pattern match you want) within the computer name column? e.g.

Heartbeat
| extend CompBucket = case(Computer contains "aks", Computer, 
                           Computer contains "Con", Computer
                           ,"")
| where isnotempty(CompBucket)

 

or 

 

 

Heartbeat
| where Computer contains "aks" 
     or Computer contains "Con"
| project Computer

View solution in original post