From 0db62b84eb2349050c7b8d84dfc25a324ff6fa00 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Doma=C5=84ski?= Date: Thu, 11 Jun 2026 11:20:08 +0000 Subject: [PATCH] Jun 11, 2026, 1:20 PM --- toolbox/DBAToolBox.ps1 | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/toolbox/DBAToolBox.ps1 b/toolbox/DBAToolBox.ps1 index bf25d58..cdb3d77 100644 --- a/toolbox/DBAToolBox.ps1 +++ b/toolbox/DBAToolBox.ps1 @@ -155,9 +155,17 @@ $tvServers.Add_SelectedItemChanged({ if ($null -ne $connState -and $connState.Success) { # Server is connected: show its version/details in the DataGrid $dgServerInfo.ItemsSource = @([PSCustomObject]$connState | Select-Object Instance, Version, Edition, Status) + + # Show cached error logs if available + if ($null -ne $connState.Errors) { + $dgServerErrors.ItemsSource = @($connState.Errors) + } else { + $dgServerErrors.ItemsSource = $null + } } else { - # Server is not connected: clear the DataGrid so no old server details are visible + # Server is not connected: clear the DataGrids so no old server details/errors are visible $dgServerInfo.ItemsSource = $null + $dgServerErrors.ItemsSource = $null } } elseif ($selectedItem.Tag -is [System.Management.Automation.PSCustomObject]) { # It's a database node @@ -166,6 +174,7 @@ $tvServers.Add_SelectedItemChanged({ # Show database details in the main information grid $dgServerInfo.ItemsSource = @($db | Select-Object Name, Status, Size, RecoveryModel) + $dgServerErrors.ItemsSource = $null # Also, update txtServerName with the parent server's tag so they see which server it belongs to $parentItem = $selectedItem.Parent @@ -177,6 +186,7 @@ $tvServers.Add_SelectedItemChanged({ # Hide detail panel if selection is null or ROOT $gridDetails.Visibility = "Collapsed" $dgServerInfo.ItemsSource = $null + $dgServerErrors.ItemsSource = $null } }) @@ -256,17 +266,37 @@ $btnConnect.Add_Click({ if ($result.Success) { Write-Log "Connected successfully to $($result.Instance)!" $dgServerInfo.ItemsSource = @([PSCustomObject]$result | Select-Object Instance, Version, Edition, Status) - # Cache successful connection state + + # Fetch errors from the last hour using Get-SQLErrors + Write-Log "Fetching error logs (last hour) for $server..." + try { + $errors = Get-SQLErrors -ServerInstance $server + if ($errors -and $errors.Count -gt 0) { + $dgServerErrors.ItemsSource = @($errors) + $result.Errors = $errors + Write-Log "Loaded $($errors.Count) error log entries." + } else { + $dgServerErrors.ItemsSource = $null + Write-Log "No errors found in the last hour." + } + } catch { + Write-Log "Warning: Could not load error log: $($_.Exception.Message)" + $dgServerErrors.ItemsSource = $null + } + + # Cache successful connection state with its fetched errors $script:ServerConnectionStates[$server] = $result } else { Write-Log "Connection failed: $($result.Error)" $script:ServerConnectionStates[$server] = $null $dgServerInfo.ItemsSource = $null + $dgServerErrors.ItemsSource = $null } } catch { Write-Log "Critical Error: $($_.Exception.Message)" $script:ServerConnectionStates[$server] = $null $dgServerInfo.ItemsSource = $null + $dgServerErrors.ItemsSource = $null } finally { $btnConnect.IsEnabled = $true }