В одном из предыдущих постов была описана установка и настройка связки Logstash + Elasticsearch + Kibana. А здесь приведу примеры поиска в Elasticsearch из powershell.

elastic-logo

Работа с индексами

Состояние индексов:

$result = Invoke-RestMethod -Method Get -Uri "http://192.168.0.208:9200/_cat/indices?format=json&pretty" -ContentType 'application/json'

$result

Удаление индексов старше 8 дней:

$result = Invoke-RestMethod -Method Get -Uri "http://192.168.0.208:9200/_cat/indices?format=json&pretty" -ContentType 'application/json'

$today = [datetime]::Today
foreach ( $r in $result ) {
    if ( $r.index.StartsWith('winlogbeat-') -eq $true ) {
        $date = [datetime]::Parse( $r.index.SubString(11) )
        if(($today-$date).Days -gt 8) {
            $delindex = $r.index
            $uri = "http://192.168.0.208:9200/"+$delindex+"?pretty"
            Invoke-RestMethod -Method Delete -Uri $uri -ContentType 'application/json'
        }
    }
}
Поиск событий

С помощью POST и json:

$json_body = '{
    "query": {
        "constant_score": {
            "filter" : {
                "bool" : {
                    "must" : [
                        {"term" : {"computer_name" : "DC01E.PSHOME.local"}}
                    ]
                }
            }
        }
    }
}'

$result= Invoke-RestMethod -URI 'http://192.168.0.208:9200/winlogbeat-*/wineventlog/_search' -Method 'POST' -ContentType 'application/json' -Body $json_body


$result.hits.hits._source
Результат:
```powershell

PS C:\Scripts> $result.hits.hits._source | ft

computer_name    level            log_name         record_number              count message         type            tags            @timestamp             event_id
-------------    -----            --------         -------------              ----- -------         ----            ----            ----------             --------
DC01E.PSHOME.... Сведения         Security         73043027                       1 Новому сеанс... wineventlog     {beats_input... 2017-06-15T0...            4672
DC01E.PSHOME.... Сведения         Security         73043030                       1 Новому сеанс... wineventlog     {beats_input... 2017-06-15T0...            4672
DC01E.PSHOME.... Сведения         Security         73043035                       1 Выполнен вых... wineventlog     {beats_input... 2017-06-15T0...            4634
DC01E.PSHOME.... Сведения         Security         73043038                       1 Выполнен вых... wineventlog     {beats_input... 2017-06-15T0...            4634
DC01E.PSHOME.... Сведения         Security         73043039                       1 Новому сеанс... wineventlog     {beats_input... 2017-06-15T0...            4672
DC01E.PSHOME.... Сведения         Security         73043051                       1 Новому сеанс... wineventlog     {beats_input... 2017-06-15T0...            4672
DC01E.PSHOME.... Сведения         Security         73043054                       1 Новому сеанс... wineventlog     {beats_input... 2017-06-15T0...            4672
DC01E.PSHOME.... Сведения         Security         73043056                       1 Выполнен вых... wineventlog     {beats_input... 2017-06-15T0...            4634
DC01E.PSHOME.... Сведения         Security         73043059                       1 Выполнен вых... wineventlog     {beats_input... 2017-06-15T0...            4634
DC01E.PSHOME.... Сведения         Security         73043061                       1 Вход с учетн... wineventlog     {beats_input... 2017-06-15T0...            4624


```

С помощью GET:

$result = Invoke-RestMethod -Method Get -Uri "http://192.168.0.208:9200/winlogbeat-*/wineventlog/_search?q=computer_name=DC01E" -ContentType 'application/json'

$messages = $result.hits.hits._source
Результат:
```powershell
PS C:\Sсripts> $messages | ft

computer_na log_name    level       record_numb       count message     type        tags        @timestamp     event_id
me                                  er
----------- --------    -----       -----------       ----- -------     ----        ----        ----------     --------
DC01E.PS... Security    Сведения    73027347              1 Выполнен... wineventlog {beats_i... 2017-06-...        4648
DC01E.PS... Security    Сведения    73027356              1 Выполнен... wineventlog {beats_i... 2017-06-...        4648
DC01E.PS... Security    Сведения    73023887              1 Запрошен... wineventlog {beats_i... 2017-06-...        4769
DC01E.PS... Security    Сведения    73023869              1 Запрошен... wineventlog {beats_i... 2017-06-...        4769
DC01E.PS... Security    Сведения    73023872              1 Запрошен... wineventlog {beats_i... 2017-06-...        4769
DC01E.PS... Security    Сведения    73023883              1 Запрошен... wineventlog {beats_i... 2017-06-...        4769
DC01E.PS... Security    Сведения    73024602              1 Запрошен... wineventlog {beats_i... 2017-06-...        4769
DC01E.PS... Security    Сведения    73027346              1 Запрошен... wineventlog {beats_i... 2017-06-...        4769
DC01E.PS... Security    Сведения    73025145              1 Запрошен... wineventlog {beats_i... 2017-06-...        4769
DC01E.PS... Security    Сведения    73023888              1 Запрошен... wineventlog {beats_i... 2017-06-...        4769


```

Ссылки