Jun 11, 2026, 1:20 PM
This commit is contained in:
+32
-2
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user