Skip to content

Commit

Permalink
Start-Log - adjust file validation, add proper dispose fixes #206
Browse files Browse the repository at this point in the history
  • Loading branch information
wsmelton committed Jul 21, 2021
1 parent d3911dd commit ca4e98f
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 82 deletions.
18 changes: 1 addition & 17 deletions docs/commands/logging/Start-TssLog.md
Expand Up @@ -12,8 +12,7 @@ Creates an initial log file with a header.
## SYNTAX

```
Start-TssLog [[-LogFilePath] <String>] [[-LogFormat] <String>] [[-ScriptVersion] <String>] [-Force]
[<CommonParameters>]
Start-TssLog [[-LogFilePath] <String>] [[-LogFormat] <String>] [[-ScriptVersion] <String>] [<CommonParameters>]
```

## DESCRIPTION
Expand All @@ -31,21 +30,6 @@ Generate a new temporary file to use as a log, writing Script version of 1.0.4.

## PARAMETERS

### -Force
{{ Fill Force Description }}

```yaml
Type: SwitchParameter
Parameter Sets: (All)
Aliases:

Required: False
Position: 3
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
```

### -LogFilePath
Full file path to the log file to create

Expand Down
70 changes: 28 additions & 42 deletions src/Thycotic.SecretServer/cmdlets/logging/StartLogCmdlet.cs
@@ -1,5 +1,6 @@
using System;
using System.IO;
using System.Text;
using System.Globalization;
using System.Collections.Generic;
using System.Management.Automation;
Expand Down Expand Up @@ -35,16 +36,10 @@ public class StartLogCmdlet : PSCmdlet
[Parameter(Position = 2)]
public string ScriptVersion { get; set; }

/// <summary>
/// <para type="description">Force will recreate the provided LogFilePath, if it exists</para>
/// </summary>
[Parameter(Position = 3)]
public SwitchParameter Force { get; set; }

protected override void EndProcessing()
protected override void BeginProcessing()
{
// if exists and force provided, try to remove the file
if (File.Exists(LogFilePath) && this.MyInvocation.BoundParameters.ContainsKey("Force"))
if (File.Exists(LogFilePath))
{
// try to remove file if exists
try
Expand All @@ -57,46 +52,36 @@ protected override void EndProcessing()
throw new System.Exception("Could not remove file: " + LogFilePath);
}
}
else if (!File.Exists(LogFilePath))
{
try
{
File.Create(LogFilePath);
}
catch
{
throw new System.Exception("Issue creating log file: " + LogFilePath);
}
}
else
{
throw new System.Exception("Could not find file provided: " + LogFilePath);
}

}
protected override void ProcessRecord()
{
// get current timestamp
var currentTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ffffff");

if (LogFormat.Equals("log"))
{
// write initial header to the file
using (StreamWriter sw = File.AppendText(LogFilePath))
{
// write line with timestamp included
sw.WriteLine(currentTime + ";INFO;************************************************************************************************************************");
sw.WriteLine(currentTime + ";INFO;..######..########..######..########..########.########.....######..########.########..##.....##.########.########.");
sw.WriteLine(currentTime + ";INFO;.##....##.##.......##....##.##.....##.##..........##.......##....##.##.......##.....##.##.....##.##.......##.....##");
sw.WriteLine(currentTime + ";INFO;.##.......##.......##.......##.....##.##..........##.......##.......##.......##.....##.##.....##.##.......##.....##");
sw.WriteLine(currentTime + ";INFO;..######..######...##.......########..######......##........######..######...########..##.....##.######...########.");
sw.WriteLine(currentTime + ";INFO;.......##.##.......##.......##...##...##..........##.............##.##.......##...##....##...##..##.......##...##..");
sw.WriteLine(currentTime + ";INFO;.##....##.##.......##....##.##....##..##..........##.......##....##.##.......##....##....##.##...##.......##....##.");
sw.WriteLine(currentTime + ";INFO;..######..########..######..##.....##.########....##........######..########.##.....##....###....########.##.....##");
sw.WriteLine(currentTime + ";INFO;************************************************************************************************************************");
sw.WriteLine(currentTime + ";INFO;Started processing at" + "[" + currentTime + "]");
sw.WriteLine(currentTime + ";INFO;************************************************************************************************************************");
sw.WriteLine(currentTime + ";INFO;Script version: [" + ScriptVersion + "].");
sw.WriteLine(currentTime + ";INFO;************************************************************************************************************************");
sw.WriteLine(currentTime + ";INFO;");
}
FileStream logFile = File.Create(LogFilePath);
var sw = new StreamWriter(logFile);

// write line with timestamp included
sw.WriteLine(currentTime + ";INFO;************************************************************************************************************************");
sw.WriteLine(currentTime + ";INFO;..######..########..######..########..########.########.....######..########.########..##.....##.########.########.");
sw.WriteLine(currentTime + ";INFO;.##....##.##.......##....##.##.....##.##..........##.......##....##.##.......##.....##.##.....##.##.......##.....##");
sw.WriteLine(currentTime + ";INFO;.##.......##.......##.......##.....##.##..........##.......##.......##.......##.....##.##.....##.##.......##.....##");
sw.WriteLine(currentTime + ";INFO;..######..######...##.......########..######......##........######..######...########..##.....##.######...########.");
sw.WriteLine(currentTime + ";INFO;.......##.##.......##.......##...##...##..........##.............##.##.......##...##....##...##..##.......##...##..");
sw.WriteLine(currentTime + ";INFO;.##....##.##.......##....##.##....##..##..........##.......##....##.##.......##....##....##.##...##.......##....##.");
sw.WriteLine(currentTime + ";INFO;..######..########..######..##.....##.########....##........######..########.##.....##....###....########.##.....##");
sw.WriteLine(currentTime + ";INFO;************************************************************************************************************************");
sw.WriteLine(currentTime + ";INFO;Started processing at" + "[" + currentTime + "]");
sw.WriteLine(currentTime + ";INFO;************************************************************************************************************************");
sw.WriteLine(currentTime + ";INFO;Script version: [" + ScriptVersion + "].");
sw.WriteLine(currentTime + ";INFO;************************************************************************************************************************");
sw.WriteLine(currentTime + ";INFO;");

sw.Dispose();
logFile.Close();
}
if (LogFormat.Equals("csv"))
{
Expand All @@ -116,6 +101,7 @@ protected override void EndProcessing()
using (var csvWriter = new CsvWriter(writer, CultureInfo.InvariantCulture))
{
csvWriter.WriteRecords(data);
csvWriter.Dispose();
}
}
}
Expand Down
23 changes: 0 additions & 23 deletions src/en-us/thycotic.secretserver.dll-help.xml
Expand Up @@ -55,32 +55,9 @@
</dev:type>
<dev:defaultValue>None</dev:defaultValue>
</command:parameter>
<command:parameter required="false" variableLength="true" globbing="false" pipelineInput="False" position="3" aliases="none">
<maml:name>Force</maml:name>
<maml:description>
<maml:para>{{ Fill Force Description }}</maml:para>
</maml:description>
<dev:type>
<maml:name>SwitchParameter</maml:name>
<maml:uri />
</dev:type>
<dev:defaultValue>False</dev:defaultValue>
</command:parameter>
</command:syntaxItem>
</command:syntax>
<command:parameters>
<command:parameter required="false" variableLength="true" globbing="false" pipelineInput="False" position="3" aliases="none">
<maml:name>Force</maml:name>
<maml:description>
<maml:para>{{ Fill Force Description }}</maml:para>
</maml:description>
<command:parameterValue required="false" variableLength="false">SwitchParameter</command:parameterValue>
<dev:type>
<maml:name>SwitchParameter</maml:name>
<maml:uri />
</dev:type>
<dev:defaultValue>False</dev:defaultValue>
</command:parameter>
<command:parameter required="false" variableLength="true" globbing="false" pipelineInput="False" position="0" aliases="none">
<maml:name>LogFilePath</maml:name>
<maml:description>
Expand Down

0 comments on commit ca4e98f

Please sign in to comment.