Forum Discussion

ScottAllison's avatar
ScottAllison
Iron Contributor
Feb 10, 2020
Solved

query multiple "contains"

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.

  • CliveWatson's avatar
    CliveWatson
    Feb 10, 2020

    ScottAllison 

     

    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

11 Replies

  • steffen_zeidler's avatar
    steffen_zeidler
    Copper Contributor

    ScottAllison 

    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:

    https://docs.microsoft.com/en-us/azure/data-explorer/kusto/query/has-anyoperator
    https://docs.microsoft.com/en-us/azure/data-explorer/kusto/query/datatypes-string-operators#what-is-a-term
    • SocInABox's avatar
      SocInABox
      Iron Contributor
      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!
  • CliveWatson's avatar
    CliveWatson
    Silver Contributor

    ScottAllison 

     

    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

     

     

    • ScottAllison's avatar
      ScottAllison
      Iron Contributor

      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).

      • CliveWatson's avatar
        CliveWatson
        Silver Contributor

        ScottAllison 

         

        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?   

Resources