1 Function Invoke-RemoveExceptions { 2 3 <# 4 .Synopsis 5 This function is used to remove the exceptions from the source file. 6 7 .Description 8 The source file is compared to the exception file and similar lines are removed. The processed 9 source file is saved with the same name as the original source file with extension new being added 10 to the file. 11 12 #> 13 14 try { 15 16 if (([String]::IsNullOrEmpty($eVetoUI.Control_SourceFile.Text)) -or ([String]::IsNullOrEmpty($eVetoUI.Control_ExceptionFile.Text))) { 17 18 [System.Windows.MessageBox]::Show("Please import a source file and exception file for processing") 19 return 20 } 21 22 $result = [System.Windows.MessageBox]::Show("Please ensure that you have formatted the exception file before continuing", "Warning", [System.Windows.MessageBoxButton]::YesNo) 23 24 25 if ($result -eq 'Yes') { 26 27 28 # Check that we have not already created a new source file 29 $NewSourceFile = ("{0}.new") -f , $eVetoUI.Control_SourceFile.Text 30 31 # If the backup file already exists then bail out 32 if (Test-Path $NewSourceFile) { 33 $FileExistMessage = "The file $NewSourceFile already exists.`n" 34 $FileExistMessage += "Please remove or rename the file inorder to continue." 35 [System.Windows.MessageBox]::Show($FileExistMessage) 36 return 37 } 38 39 # Check that the exception file has been properly formatted 40 41 # If the formatting file does not exist bail out 42 $FormatExceptionFile = '.\formatexceptionfile.config' 43 if (-not(Test-Path $FormatExceptionFile)) { 44 $FileExistMessage = "The formatting file $FormatExceptionFile does not exist.`n" 45 $FileExistMessage += "Please create the formatting file inorder to continue." 46 [System.Windows.MessageBox]::Show($FileExistMessage) 47 return 48 49 } 50 51 # Import the exception file 52 $ExceptionFileTest = Get-Content $eVetoUI.Control_ExceptionFile.Text -ErrorAction Stop | Out-String 53 54 $ExceptionFileIsOk = $true 55 56 # Import the exceptions formatting rules from a config file 57 $xmlformatting = [xml](Get-Content $FormatExceptionFile) 58 59 # Check that the exception file has been correctly formatted 60 if ($xmlformatting.formatting.StringFormatting.HasChildNodes){ 61 foreach ($string in $xmlformatting.formatting.StringFormatting.String) { 62 63 $stringformatting = $string.Split(";") 64 65 if ($ExceptionFileTest.Contains($stringformatting[0].ToString())) { 66 $ExceptionFileIsOk = $false 67 } 68 69 } 70 71 } 72 73 if ($xmlformatting.formatting.RegexFormatting.HasChildNodes){ 74 foreach ($regex in $xmlformatting.formatting.RegexFormatting.Regex) { 75 76 $regexformatting = $regex.Split(";") 77 if ($ExceptionFileTest.Contains($regexformatting[0].ToString())) { 78 $ExceptionFileIsOk = $false 79 } 80 81 } 82 83 } 84 85 if (-not ($ExceptionFileIsOk)) { 86 [System.Windows.MessageBox]::Show('The exception file is not in the correct format. Please format the exception file using the Format Exception File button.') 87 return 88 } 89 90 91 92 $eVetoUI.Control_Output.AppendText("`r`n`r`n") 93 $eVetoUI.Control_Output.ScrollToEnd() 94 $eVetoUI.Window.Cursor = [System.Windows.Input.Cursors]::Wait 95 $eVetoUI.Control_Output.Cursor = [System.Windows.Input.Cursors]::Wait 96 97 98 # The hash table used to process the files 99 $Source = @{} 100 $Exception = @{} 101 102 # Import the files into the hash tables 103 104 Write-PSOutput -ConsoleText "Importing Exception file ... `r`n" 105 [IO.File]::ReadAllLines($eVetoUI.Control_ExceptionFile.Text) | % { $Exception[$_] = $true } 106 107 Write-PSOutput -ConsoleText "Importing Source file ... `r`n" 108 [IO.File]::ReadAllLines($eVetoUI.Control_SourceFile.Text) | % { $Source[$_] = $true } 109 110 111 Write-PSOutput -ConsoleText "Removing exceptions from the source file ... `r`n" 112 113 $ProcessedExceptions = $false 114 115 116 foreach ($key in $Exception.keys) { 117 118 #$eVetoUI.Control_Output.AppendText(".") 119 120 #Remove row if it exists in source file 121 if ($Source.ContainsKey($key)) { 122 $ProcessedExceptions = $true 123 $dupline = ("Removing duplicate line {0} `r`n") -f , $key 124 Write-PSOutput -ConsoleText $dupline 125 $Source.Remove($key) 126 } 127 128 } 129 130 131 # Save the source file 132 if ($ProcessedExceptions) { 133 Write-PSOutput -ConsoleText "Saving the new source file as $NewSourceFile ... `r`n" 134 $Source.Keys | Add-Content $NewSourceFile -Force -ErrorAction Stop 135 [System.Windows.MessageBox]::Show(" Removal of exceptions completed.`n The file was saved as $NewSourceFile. ") 136 } 137 138 else { 139 [System.Windows.MessageBox]::Show("No exceptions were removed from the source file.") 140 } 141 142 $eVetoUI.Control_Output.ScrollToEnd() 143 144 $eVetoUI.Control_Output.Cursor = [System.Windows.Input.Cursors]::Arrow 145 $eVetoUI.Window.Cursor = [System.Windows.Input.Cursors]::Arrow 146 147 148 149 150 } 151 else { 152 153 return 154 } 155 } 156 157 catch { 158 if ($_.Exception.InnerException) { 159 160 $ExceptionMessage = $_.Exception.InnerException 161 } 162 else { 163 164 $ExceptionMessage = $_.Exception.Message 165 } 166 167 $eVetoUI.Control_Output.AppendText($ExceptionMessage) 168 169 $eVetoUI.Control_Output.ScrollToEnd() 170 171 $eVetoUI.Control_Output.Cursor = [System.Windows.Input.Cursors]::Arrow 172 $eVetoUI.Window.Cursor = [System.Windows.Input.Cursors]::Arrow 173 } 174 175 finally { 176 177 Remove-Variable -Name $Exception 178 Remove-Variable -Name $Source 179 } 180 181 182 }