Skip to content

Instantly share code, notes, and snippets.

@leandrosilva
Last active September 4, 2019 21:09
Show Gist options
  • Save leandrosilva/46bacc05c8965a29191f0b8586869875 to your computer and use it in GitHub Desktop.
Save leandrosilva/46bacc05c8965a29191f0b8586869875 to your computer and use it in GitHub Desktop.
PowerShell module to send log to ElasticSearch
###############################
# 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