Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge branch to main for: new PowerShell commands Get-Records and Get-Insight #445

Merged
merged 2 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>BlueDotBrigade.Weevil.PowerShell</RootNamespace>
<AssemblyName>BlueDotBrigade.Weevil.PowerShell</AssemblyName>
<OutputType>Library</OutputType>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<Version>2.11.0</Version>
<Authors>BlueDotBrigade;</Authors>
<Copyright>© 2022 Blue Dot Brigade. All rights reserved.</Copyright>
<Owners>Blue Dot Brigade</Owners>
<Company>Blue Dot Brigade</Company>
<Description>PowerShell Cmdlets for the Weevil log viewer application.</Description>
<PackageId>BlueDotBrigade.Weevil.PowerShell</PackageId>
<PackageTags>BlueDotBrigade;Weevil;Log Viewer;</PackageTags>
<RepositoryType>git</RepositoryType>
<PackageProjectUrl>https://github.com/BlueDotBrigade/weevil</PackageProjectUrl>
<RepositoryUrl>https://github.com/BlueDotBrigade/weevil.git</RepositoryUrl>
<PackageVersion>1.0.0</PackageVersion>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
<AssemblyVersion>2.11.0.0</AssemblyVersion>
<FileVersion>2.11.0.0</FileVersion>
<Nullable>enable</Nullable>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\BlueDotBrigade.Weevil.Common\BlueDotBrigade.Weevil.Common.csproj" />
<ProjectReference Include="..\BlueDotBrigade.Weevil.Core\BlueDotBrigade.Weevil.Core.csproj" />
<ProjectReference Include="..\BlueDotBrigade.Weevil.Windows\BlueDotBrigade.Weevil.Windows.csproj" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.PowerShell.SDK" Version="7.3.4" />
</ItemGroup>
</Project>
53 changes: 53 additions & 0 deletions Src/BlueDotBrigade.Weevil.PowerShell/GetInsightCmdlet.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using System;
using System.Collections.Generic;
using System.Management.Automation;
using BlueDotBrigade.Weevil.Core;
using BlueDotBrigade.Weevil.Core.Analysis;
using BlueDotBrigade.Weevil.Core.Filtering;

namespace BlueDotBrigade.Weevil.PowerShell
{

/*
# Assuming you have already loaded the BlueDotBrigade.Weevil.PowerShell module
$logFilePath = "C:\Temp\Application.log"

$insights = Get-Insight -FilePath $logFilePath

$insights | ForEach-Object {
Write-Host "Title: $_.Title"
Write-Host "Metric: $_.MetricValue $_.MetricUnit"
Write-Host "Details: $_.Details"
Write-Host "Attention Required: $_.IsAttentionRequired"
Write-Host "-------------------------------------"
}
*/
[Cmdlet(VerbsCommon.Get, "Insight")]
public class GetInsightCmdlet : Cmdlet
{
[Parameter(Position = 0, Mandatory = true)]
public string FilePath { get; set; }

protected override void ProcessRecord()
{
try
{
IEngine engine = Engine
.UsingPath(this.FilePath)
.Open();

IEnumerable<IInsight> insights = engine.Analyzer.GetInsights();

foreach (var insight in insights)
{
this.WriteObject(insight);
}
}
catch (Exception ex)
{
// Handle any errors that occur during processing
this.WriteError(new ErrorRecord(ex, "InsightError", ErrorCategory.InvalidOperation, this));
}
}
}
}
50 changes: 50 additions & 0 deletions Src/BlueDotBrigade.Weevil.PowerShell/GetRecordsCmdlet.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System;
using System.Management.Automation;
using BlueDotBrigade.Weevil.Core;
using BlueDotBrigade.Weevil.Core.Filtering;

namespace BlueDotBrigade.Weevil.PowerShell
{
/*
# Assuming you have already loaded the BlueDotBrigade.Weevil.PowerShell module
$logFilePath = "C:\Temp\Application.log"
$records = Get-Records -FilePath $logFilePath -Include "Id=2" -Exclude "Error"
$records | ForEach-Object { Write-Host $_ }
*/
[Cmdlet(VerbsCommon.Get, "Records")]
public class GetRecordsCmdlet : Cmdlet
{
[Parameter(Position = 0, Mandatory = true)]
public string FilePath { get; set; }

[Parameter(Position = 1, Mandatory = false)]
public FilterType FilterType { get; set; } = FilterType.RegularExpression;

[Parameter(Position = 2, Mandatory = false)]
public string Include { get; set; } = string.Empty;

[Parameter(Position = 3, Mandatory = false)]
public string Exclude { get; set; } = string.Empty;

protected override void ProcessRecord()
{
try
{
IEngine engine = Engine
.UsingPath(this.FilePath)
.Open();

var filterCriteria = new FilterCriteria(this.Include, this.Exclude);

var filteredEntries = engine.Filter.Apply(this.FilterType, filterCriteria);

this.WriteObject(filteredEntries, enumerateCollection: true);
}
catch (Exception ex)
{
// Handle any errors that occur during processing
this.WriteError(new ErrorRecord(ex, "FilterError", ErrorCategory.InvalidOperation, this));
}
}
}
}
Loading