Skip to content

Instantly share code, notes, and snippets.

@KirillPashkov
Created August 8, 2017 11:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save KirillPashkov/82f9813ed7a1044145b74f3e018f3fb8 to your computer and use it in GitHub Desktop.
Save KirillPashkov/82f9813ed7a1044145b74f3e018f3fb8 to your computer and use it in GitHub Desktop.
function Get-MongoEventLog (
[Parameter(Position=0)][string] $Regex = '.*',
[Parameter(Position=1)][string] $Log = $null
)
{
Add-Type -Path "bin\MongoDB.Bson.dll";
Add-Type -Path "bin\MongoDB.Driver.dll";
$db = [MongoDB.Driver.MongoDatabase]::Create('mongodb://localhost/powershell');
[MongoDB.Driver.MongoCollection] $collection = $db['eventlogs']
[MongoDB.Driver.QueryDocument] $query = [MongoDB.Driver.QueryDocument]@{ 'Source' = @{ '$regex' = $Regex; '$options' = 'i' } }
if (-not [String]::IsNullOrEmpty($Log)) { $query['Log'] = $Log }
$collection.Find($query).SetLimit(10) | ForEach-Object {
# Notice how ToHashTable() saves us a lot of keystrikes.
New-Object PSObject -Property $_.ToHashTable()
} | Select-Object HostName, Log, Source, InstanceId, Index, Message
}
# Example
Get-MongoEventLog 'service'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment