@@ -13,11 +13,12 @@ function Export-CalLogExcel {
1313 # Suppress EPPlus warnings and errors that occur when writing to an existing file
1414 # (AutoNameRange validation, named range conflicts, title character warnings)
1515 $savedErrorActionPreference = $ErrorActionPreference
16- $ErrorActionPreference = ' SilentlyContinue'
17-
18- $excel = $GCDOResults | Export-Excel @ExcelParamsArray - PassThru 3> $null
19-
20- $ErrorActionPreference = $savedErrorActionPreference
16+ try {
17+ $ErrorActionPreference = ' SilentlyContinue'
18+ $excel = $GCDOResults | Export-Excel @ExcelParamsArray - PassThru 3> $null
19+ } finally {
20+ $ErrorActionPreference = $savedErrorActionPreference
21+ }
2122
2223 FormatHeader ($excel )
2324 SortByLogTimestamp ($excel )
@@ -33,9 +34,12 @@ function Export-CalLogExcel {
3334
3435 # Export Raw Logs for Developer Analysis
3536 Write-Host - ForegroundColor Cyan " Exporting Raw CalLogs to Excel Tab [$ ( $ShortId + " _Raw" ) ]..."
36- $ErrorActionPreference = ' SilentlyContinue'
37- $rawExcel = $script :GCDO | Export-Excel - Path $FileName - WorksheetName $ ($ShortId + " _Raw" ) - AutoFilter - FreezeTopRow - BoldTopRow - MoveToEnd - PassThru 3> $null
38- $ErrorActionPreference = $savedErrorActionPreference
37+ try {
38+ $ErrorActionPreference = ' SilentlyContinue'
39+ $rawExcel = $script :GCDO | Export-Excel - Path $FileName - WorksheetName $ ($ShortId + " _Raw" ) - AutoFilter - FreezeTopRow - BoldTopRow - MoveToEnd - PassThru 3> $null
40+ } finally {
41+ $ErrorActionPreference = $savedErrorActionPreference
42+ }
3943 $rawExcel.Workbook.Worksheets [$ShortId + " _Raw" ].TabColor = $script :TabColor
4044 Export-Excel - ExcelPackage $rawExcel - WorksheetName $ ($ShortId + " _Raw" )
4145}
@@ -186,52 +190,57 @@ function LogScriptInfo {
186190
187191 # Write Script Info into the existing workbook (Enhanced tab was already saved)
188192 $savedEAP = $ErrorActionPreference
189- $ErrorActionPreference = ' SilentlyContinue'
190-
191- # Open existing package and add/append the Script Info sheet
192193 try {
193- $pkg = Open-ExcelPackage - Path $FileName - ErrorAction Stop
194- $sheetExists = $null -ne $pkg.Workbook.Worksheets [" Script Info" ]
194+ $ErrorActionPreference = ' SilentlyContinue'
195195
196- if ($sheetExists ) {
197- # Append rows after existing data
198- $ws = $pkg.Workbook.Worksheets [" Script Info" ]
199- $startRow = $ws.Dimension.End.Row + 1
200- } else {
201- # Create a new sheet
202- $ws = $pkg.Workbook.Worksheets.Add (" Script Info" )
203- $startRow = 1
204- # Add header row
205- $ws.Cells [$startRow , 1 ].Value = " Key"
206- $ws.Cells [$startRow , 2 ].Value = " Value"
207- $startRow = 2
208- }
196+ # Open existing package and add/append the Script Info sheet
197+ try {
198+ $pkg = Open-ExcelPackage - Path $FileName - ErrorAction Stop
199+ $sheetExists = $null -ne $pkg.Workbook.Worksheets [" Script Info" ]
209200
210- foreach ($item in $RunInfo ) {
211- $ws.Cells [$startRow , 1 ].Value = $item.Key
212- $ws.Cells [$startRow , 2 ].Value = [string ]$item.Value
213- $startRow ++
214- }
201+ if ($sheetExists ) {
202+ # Append rows after existing data
203+ $ws = $pkg.Workbook.Worksheets [" Script Info" ]
204+ $startRow = $ws.Dimension.End.Row + 1
205+ } else {
206+ # Create a new sheet
207+ $ws = $pkg.Workbook.Worksheets.Add (" Script Info" )
208+ $startRow = 1
209+ # Add header row
210+ $ws.Cells [$startRow , 1 ].Value = " Key"
211+ $ws.Cells [$startRow , 2 ].Value = " Value"
212+ $startRow = 2
213+ }
215214
216- $ws.Column (1 ).Width = 25
217- $ws.Column (2 ).Width = 80
218- $ws.TabColor = [System.Drawing.Color ]::Gray
219- $pkg.Save ()
220- $pkg.Dispose ()
221- } catch {
222- Write-Warning " Unable to write Script Info tab: $_ "
223- if ($null -ne $pkg ) { $pkg.Dispose () }
224- }
215+ foreach ($item in $RunInfo ) {
216+ $ws.Cells [$startRow , 1 ].Value = $item.Key
217+ $ws.Cells [$startRow , 2 ].Value = [string ]$item.Value
218+ $startRow ++
219+ }
225220
226- $ErrorActionPreference = $savedEAP
221+ $ws.Column (1 ).Width = 25
222+ $ws.Column (2 ).Width = 80
223+ $ws.TabColor = [System.Drawing.Color ]::Gray
224+ $pkg.Save ()
225+ $pkg.Dispose ()
226+ } catch {
227+ Write-Warning " Unable to write Script Info tab: $_ "
228+ if ($null -ne $pkg ) { $pkg.Dispose () }
229+ }
230+ } finally {
231+ $ErrorActionPreference = $savedEAP
232+ }
227233}
228234
229235function Export-TimelineExcel {
230236 Write-Host - ForegroundColor Cyan " Exporting Timeline to Excel..."
231237 $savedEAP = $ErrorActionPreference
232- $ErrorActionPreference = ' SilentlyContinue'
233- $tlExcel = $script :TimeLineOutput | Export-Excel - Path $FileName - WorksheetName $ ($ShortId + " _TimeLine" ) - Title " Timeline for $Identity " - AutoSize - FreezeTopRow - BoldTopRow - PassThru 3> $null
234- $ErrorActionPreference = $savedEAP
238+ try {
239+ $ErrorActionPreference = ' SilentlyContinue'
240+ $tlExcel = $script :TimeLineOutput | Export-Excel - Path $FileName - WorksheetName $ ($ShortId + " _TimeLine" ) - Title " Timeline for $Identity " - AutoSize - FreezeTopRow - BoldTopRow - PassThru 3> $null
241+ } finally {
242+ $ErrorActionPreference = $savedEAP
243+ }
235244 $tlExcel.Workbook.Worksheets [$ShortId + " _TimeLine" ].TabColor = $script :TabColor
236245 Export-Excel - ExcelPackage $tlExcel - WorksheetName $ ($ShortId + " _TimeLine" )
237246}
0 commit comments