PuttyLogCleaner.ps1


<#PSScriptInfo
 
.VERSION 1.0
 
.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")

    #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)
    
    #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