PuttyLogCleaner.ps1
<#
.NOTES Information on running PowerShell scripts can be found here: -http://ss64.com/ps/syntax-run.html -https://technet.microsoft.com/en-us/library/bb613481.aspx File Name: PuttyLogCleaner.ps1 .SYNOPSIS Version: 1.2 - Changed to using select-string as that worked better in Powershell 7 Accounted for lines that might start with a backspace character 1.1 - Added GUI for selecting files to open and save 1.0 - Original release .DESCRIPTION This script will read in a log file generated from the Putty application and remove any backspace characters (along with the characters the backspace was intended to remove) for an easier to read log. .PARAMETER OriginalFile The name of a Putty generated log file located in the same directory as this script. .EXAMPLE .\PuttyLogCleaner.ps1 Running without any parameters will prompt for all necessary values .EXAMPLE .\PuttyLogCleaner.ps1 -OriginalFile MyLogFile.txt Parses through the Putty log file named MyLogFile.txt #> <#PSScriptInfo .VERSION 1.2 .GUID 5290f432-65f9-4fb2-b291-65d848fea68e .AUTHOR Tim McGue .COMPANYNAME .COPYRIGHT .TAGS .LICENSEURI .PROJECTURI .ICONURI .EXTERNALMODULEDEPENDENCIES .REQUIREDSCRIPTS .EXTERNALSCRIPTDEPENDENCIES .RELEASENOTES #> <# .DESCRIPTION This script will read in a log file generated from the Putty application and remove any backspace characters (along with the characters the backspace was intended to remove) for an easier to read log. #> [CmdletBinding(PositionalBinding=$False)] Param( [Parameter(Mandatory=$False)] [string]$OriginalFile ) Function RemoveBackspace ($InputString) { #Find the location of the backspace in the passed string #$BackspaceLocation = $InputString.IndexOf("`b") #Changed to using select-string cmdlet as it better behaves with PowerShell 7 $BackspaceLocation = ($InputString | Select-String "`b").Matches.Index #There could be a string that starts with a backspace If ($BackspaceLocation -gt 0) { #Get the left characters of the line without including the backspace nor the character immediately to the left of the backspace #which removes the character the backspace originally intended to delete $BackspaceRemovedStart = $InputString.substring(0,$BackspaceLocation-1) } else { $BackspaceRemovedStart = "" } #Get all the characters to the right of the first backspace $BackspaceRemovedEnd = $InputString.substring($BackspaceLocation+1,$InputString.length-$BackspaceLocation-1) #Join the two strings together $BackspaceRemoved = $BackspaceRemovedStart + $BackspaceRemovedEnd #Keep parsing until no backspaces remain if ($BackspaceRemoved.Contains("`b")) { RemoveBackspace $BackspaceRemoved } else { #Return back the cleaned string Return $BackspaceRemoved } } Function Get-OpenFile($initialDirectory) { [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") | Out-Null $OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog $OpenFileDialog.initialDirectory = $initialDirectory $OpenFileDialog.filter = "All files (*.*)| *.*" $OpenFileDialog.ShowDialog() | Out-Null $OpenFileDialog.filename } Function Get-SaveFile($initialDirectory) { [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") | Out-Null $SaveFileDialog = New-Object System.Windows.Forms.SaveFileDialog $SaveFileDialog.initialDirectory = $initialDirectory $SaveFileDialog.filter = "All files (*.*)| *.*" $SaveFileDialog.ShowDialog() | Out-Null $SaveFileDialog.filename } #Get-SaveFile If ($OriginalFile.Length -eq 0) { Write-Host "Select a saved Putty output file" $OriginalFile = Get-OpenFile } # Read in the file $RawOriginalText = Get-Content $OriginalFile #Setup the array $CleanedText = @() #Go through each line read from the text file ForEach ($Line in $RawOriginalText) { #Check for backspace characters if ($Line.Contains("`b")) { #Call the function $Line = RemoveBackspace $Line } #BEL character if ($Line.Contains("`a")) { #Remove the BEL $Line = $Line.replace("`a","") } #Some newer BIOS output will use a wide variety of escape characters. #There is no reason to clean those ESC items. #Add the cleaned line into the array $CleanedText += $Line } Write-Host "Select a file to save cleaned output" $CleanedOutputFile = Get-SaveFile #Output the array to the new file name Out-File -FilePath $CleanedOutputFile -InputObject $CleanedText |