Created
August 8, 2017 11:06
-
-
Save KirillPashkov/82f9813ed7a1044145b74f3e018f3fb8 to your computer and use it in GitHub Desktop.
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
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