Last active
September 4, 2019 21:09
-
-
Save leandrosilva/46bacc05c8965a29191f0b8586869875 to your computer and use it in GitHub Desktop.
PowerShell module to send log to ElasticSearch
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
############################### | |
# YOUR LOGS TO ELASTIC SEARCH # | |
############################### | |
function Get-ESConfig () { | |
[xml] $log4netConfig = Get-Content -Path $env:CONFIG_LOGGER_FILE | |
return $log4netConfig.log4net.appender | Where-Object { $_.name -eq "ElasticSearchAppender" } | |
} | |
$ESConfig = Get-ESConfig | |
function Get-ESBaseUri () { | |
$esServer = $ESConfig.Server | |
$esPort = $ESConfig.Port | |
$esProtocol = if ($esPort -eq "443") { "https" } else { "http" } | |
return "${esProtocol}://${esServer}:${esPort}" | |
} | |
$ESBaseUri = Get-ESBaseUri | |
function Get-ESIndexPrefix () { | |
return $ESConfig.IndexName.Split("%")[0] | |
} | |
$ESIndexPrefix = Get-ESIndexPrefix | |
function Get-ESIndexUri ( | |
[Parameter(Mandatory=$true, ValueFromPipeline=$true)] | |
[ValidateNotNullOrEmpty()] | |
[String] | |
$IndexSuffix | |
) { | |
return "$ESBaseUri/${ESIndexPrefix}${IndexSuffix}/_doc" | |
} | |
function Send-ESLog ( | |
[Parameter(Mandatory=$true, ValueFromPipeline=$true)] | |
[ValidateNotNullOrEmpty()] | |
[String] | |
$IndexUri, | |
[Parameter(Mandatory=$true, ValueFromPipeline=$true)] | |
[ValidateNotNullOrEmpty()] | |
[String] | |
$Body | |
) { | |
return Invoke-RestMethod -Method Post ` | |
-Uri $IndexUri ` | |
-ContentType: 'application/json' ` | |
-Body $Body | |
} | |
function Get-ESLog ( | |
[Parameter(Mandatory=$true, ValueFromPipeline=$true)] | |
[ValidateNotNullOrEmpty()] | |
[String] | |
$IndexUri, | |
[Parameter(Mandatory=$true, ValueFromPipeline=$true)] | |
[ValidateNotNullOrEmpty()] | |
[String] | |
$DocId | |
) { | |
return Invoke-RestMethod -Method Get -Uri $IndexUri/$DocId | |
} | |
function Search-ESLog ( | |
[Parameter(Mandatory=$true, ValueFromPipeline=$true)] | |
[ValidateNotNullOrEmpty()] | |
[String] | |
$IndexUri | |
) { | |
return Invoke-RestMethod -Method Get -Uri $IndexUri/_search | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment