Skip to content

Commit 70567ce

Browse files
committed
Add some minor features.
1 parent 961f068 commit 70567ce

7 files changed

Lines changed: 57 additions & 10 deletions

File tree

‎.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,3 +213,4 @@ pip-log.txt
213213

214214
#Mr Developer
215215
.mr.developer.cfg
216+
/Module/PowerShellLogging/PowerShellLoggingModule.dll

‎Commands/EnableLogFileCommand.cs‎

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,15 @@
33
// ReSharper disable UnusedAutoPropertyAccessor.Global
44
// ReSharper disable UnusedMember.Global
55

6+
using System;
7+
using System.IO;
8+
using System.Linq;
9+
10+
611
namespace PSLogging.Commands
712
{
813
using System.Management.Automation;
9-
14+
1015
[Cmdlet(VerbsLifecycle.Enable, "LogFile")]
1116
public class EnableLogFileCommand : PSCmdlet
1217
{
@@ -34,7 +39,7 @@ public ScriptBlock OnError
3439
set { errorCallback = value; }
3540
}
3641

37-
[Parameter(Mandatory = true,
42+
[Parameter(Mandatory = false,
3843
Position = 0,
3944
ParameterSetName = "New")]
4045
public string Path
@@ -61,17 +66,42 @@ protected override void EndProcessing()
6166

6267
if (ParameterSetName == "New")
6368
{
64-
logFile = new LogFile(path, streams, errorCallback);
69+
70+
logFile = new LogFile(string.IsNullOrEmpty(path) ? GetDefaultLogFileName() : path, streams, errorCallback);
6571
WriteObject(logFile);
6672
}
6773
else
6874
{
6975
logFile = inputObject;
7076
}
7177

78+
// Initiate a log file
79+
logFile.CheckDirectory();
80+
if (!File.Exists(logFile.Path) && !string.IsNullOrEmpty(MyInvocation.ScriptName))
81+
{
82+
File.AppendAllText(logFile.Path, $"******************\r\nScript Location: {MyInvocation.ScriptName} \r\n******************\r\n");
83+
}
7284
HostIOInterceptor.Instance.AttachToHost(Host);
7385
HostIOInterceptor.Instance.AddSubscriber(logFile);
7486
}
87+
88+
private string GetDefaultLogFileName()
89+
{
90+
var dateString = DateTime.Now.ToString(@"yyyy-MM-dd_HHmm");
91+
92+
var scriptName = string.Empty;
93+
var directoryName = ".\\Log";
94+
95+
if (!string.IsNullOrEmpty(this.MyInvocation.ScriptName))
96+
{
97+
var directoryInfo = new DirectoryInfo(MyInvocation.ScriptName);
98+
scriptName = directoryInfo.Name.Remove(directoryInfo.Name.IndexOf(".", StringComparison.Ordinal));
99+
directoryName = directoryInfo.Parent?.FullName;
100+
}
101+
directoryName = $@"{directoryName}\Log\{scriptName}_Log_{dateString}.txt";
102+
103+
return directoryName;
104+
}
75105
} // End AddLogFileCommand class
76106
}
77107

‎LogFile.cs‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ public LogFile(string filename, StreamType streams = StreamType.All, ScriptBlock
2828
ErrorCallback = errorCallback;
2929
}
3030

31+
~LogFile()
32+
{
33+
File.AppendAllText(System.IO.Path.Combine(path, fileName), $"\r\n******************\r\nScript Ended: {DateTime.UtcNow.ToString(DateTimeFormat)} \r\n******************\r\n");
34+
}
3135
#endregion
3236

3337
#region Properties
@@ -188,7 +192,7 @@ public override void WriteWarning(string message)
188192

189193
#region Private Methods
190194

191-
private void CheckDirectory()
195+
public void CheckDirectory()
192196
{
193197
if (!String.IsNullOrEmpty(path) && !Directory.Exists(path))
194198
{

‎Module/Demo/PSL_Demo.ps1‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Import-Module C:\Repos\PowerShellLoggingModule\Module\PowerShellLogging
2+
3+
$LogFile = Enable-LogFile #-Path C:\Log\ScriptName_Log_$(Get-Date -Format "yyyy-MM-dd_HHmmss").txt
4+
5+
Write-Host "REX"
6+
7+
rex
8+
9+
Write-Host "REX"

‎Module/PSLogging.zip‎

30.4 KB
Binary file not shown.

‎Module/PowerShellLogging/PowerShellLogging.psd1‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ ModuleVersion = '1.2.1'
1717
# ID used to uniquely identify this module
1818
GUID = '345320b5-bdc3-4ab6-a13e-fcb019362fe6'
1919

20-
CompanyName = 'Home'
21-
CopyRight = 'Copyright 2014 David Wyatt'
20+
CompanyName = 'Avanade Inc.'
21+
CopyRight = 'Copyright 2016 Avanade Inc.'
2222

2323
# Author of this module
24-
Author = 'Dave Wyatt'
24+
Author = 'Avanade Inc.'
2525

2626
# Description of the functionality provided by this module
2727
Description = 'Captures PowerShell console output to a log file.'

‎PSLogging.csproj‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<AppDesignerFolder>Properties</AppDesignerFolder>
1010
<RootNamespace>PSLogging</RootNamespace>
1111
<AssemblyName>PowerShellLoggingModule</AssemblyName>
12-
<TargetFrameworkVersion>v2.0</TargetFrameworkVersion>
12+
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
1313
<FileAlignment>512</FileAlignment>
1414
<TargetFrameworkProfile>
1515
</TargetFrameworkProfile>
@@ -18,18 +18,21 @@
1818
<DebugSymbols>true</DebugSymbols>
1919
<DebugType>full</DebugType>
2020
<Optimize>false</Optimize>
21-
<OutputPath>bin\Debug\</OutputPath>
21+
<OutputPath>Module\PowerShellLogging\</OutputPath>
2222
<DefineConstants>DEBUG;TRACE</DefineConstants>
2323
<ErrorReport>prompt</ErrorReport>
2424
<WarningLevel>4</WarningLevel>
25+
<GenerateSerializationAssemblies>On</GenerateSerializationAssemblies>
26+
<Prefer32Bit>false</Prefer32Bit>
2527
</PropertyGroup>
2628
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
2729
<DebugType>pdbonly</DebugType>
2830
<Optimize>true</Optimize>
29-
<OutputPath>bin\Release\</OutputPath>
31+
<OutputPath>Module\PowerShellLogging\</OutputPath>
3032
<DefineConstants>TRACE</DefineConstants>
3133
<ErrorReport>prompt</ErrorReport>
3234
<WarningLevel>4</WarningLevel>
35+
<Prefer32Bit>false</Prefer32Bit>
3336
</PropertyGroup>
3437
<ItemGroup>
3538
<Reference Include="System" />

0 commit comments

Comments
 (0)