Skip to content

Instantly share code, notes, and snippets.

@KirillPashkov
Created August 8, 2017 11:06
Show Gist options
  • Save KirillPashkov/55062d4c14403dde035e61014f1cf8e2 to your computer and use it in GitHub Desktop.
Save KirillPashkov/55062d4c14403dde035e61014f1cf8e2 to your computer and use it in GitHub Desktop.
function Get-MongoEventLog (
[Parameter(Position=0)][string] $Regex = '.*',
[Parameter(Position=1)][string] $Log = $null
)
{
# Check to see if we are running the 64 bit version of Powershell.
# See http://stackoverflow.com/questions/2897569/visual-studio-deployment-project-error-when-writing-to-registry
if ([intptr]::size -eq 8) { $mongoDriverPath = (Get-ItemProperty "HKLM:\SOFTWARE\Wow6432Node\Microsoft\.NETFramework\v3.5\AssemblyFoldersEx\MongoDB CSharpDriver 1.2").'(default)'; }
else { $mongoDriverPath = (Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\.NETFramework\v3.5\AssemblyFoldersEx\MongoDB CSharpDriver 1.2").'(default)'; }
Add-Type -Path "$($mongoDriverPath)\MongoDB.Bson.dll";
Add-Type -Path "$($mongoDriverPath)\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 {
$obj = @{};
$ht = $_.Elements
$ht | ForEach-Object { $obj[$_.Name] = $_.Value; }
New-Object PSObject -Property $obj
} | Select-Object HostName, Log, Source, InstanceId, Index, Message
#} | Select-Object HostName, Log, Source, InstanceId, Message
}
# Example
Get-MongoEventLog 'service' | Out-GridView
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment