Jun 12, 2026, 8:29 AM
This commit is contained in:
+51
-2
@@ -44,6 +44,7 @@ $btnConnect = $Window.FindName("btnConnect")
|
||||
$txtServerName = $Window.FindName("txtServerName")
|
||||
$txtLogs = $Window.FindName("txtLogs")
|
||||
$dgServerInfo = $Window.FindName("dgServerInfo")
|
||||
$dgDatabases = $Window.FindName("dgDatabases")
|
||||
$dgServerErrors = $Window.FindName("dgServerErrors")
|
||||
$tvServers = $Window.FindName("tvServers")
|
||||
$btnAddServer = $Window.FindName("btnAddServer")
|
||||
@@ -107,6 +108,21 @@ function Refresh-ServerTree {
|
||||
try {
|
||||
$dbs = Get-SQLDatabases -ServerInstance $srvName
|
||||
if ($dbs -and $dbs.Count -gt 0) {
|
||||
# Cache the loaded databases
|
||||
if ($null -eq $script:ServerConnectionStates[$srvName]) {
|
||||
$script:ServerConnectionStates[$srvName] = @{
|
||||
Success = $true
|
||||
Instance = $srvName
|
||||
Version = "Unknown (Expanded)"
|
||||
Edition = "Unknown"
|
||||
Status = "Online"
|
||||
Databases = $dbs
|
||||
Errors = $null
|
||||
}
|
||||
} else {
|
||||
$script:ServerConnectionStates[$srvName].Databases = $dbs
|
||||
}
|
||||
|
||||
foreach ($db in $dbs) {
|
||||
$dbNode = New-Object System.Windows.Controls.TreeViewItem
|
||||
$dbNode.Header = "$($db.Name) ($($db.Status))"
|
||||
@@ -156,6 +172,13 @@ $tvServers.Add_SelectedItemChanged({
|
||||
# Server is connected: show its version/details in the DataGrid
|
||||
$dgServerInfo.ItemsSource = @([PSCustomObject]$connState | Select-Object Instance, Version, Edition, Status)
|
||||
|
||||
# Show cached databases if available
|
||||
if ($null -ne $connState.Databases) {
|
||||
$dgDatabases.ItemsSource = @($connState.Databases)
|
||||
} else {
|
||||
$dgDatabases.ItemsSource = $null
|
||||
}
|
||||
|
||||
# Show cached error logs if available
|
||||
if ($null -ne $connState.Errors) {
|
||||
$dgServerErrors.ItemsSource = @($connState.Errors)
|
||||
@@ -165,6 +188,7 @@ $tvServers.Add_SelectedItemChanged({
|
||||
} else {
|
||||
# Server is not connected: clear the DataGrids so no old server details/errors are visible
|
||||
$dgServerInfo.ItemsSource = $null
|
||||
$dgDatabases.ItemsSource = $null
|
||||
$dgServerErrors.ItemsSource = $null
|
||||
}
|
||||
} elseif ($selectedItem.Tag -is [System.Management.Automation.PSCustomObject]) {
|
||||
@@ -173,7 +197,8 @@ $tvServers.Add_SelectedItemChanged({
|
||||
Write-Log "Selected database: $($db.Name) (Size: $($db.Size), Status: $($db.Status))"
|
||||
|
||||
# Show database details in the main information grid
|
||||
$dgServerInfo.ItemsSource = @($db | Select-Object Name, Status, Size, RecoveryModel)
|
||||
$dgServerInfo.ItemsSource = @($db | Select-Object Name, Status, Size, RecoveryModel, LastFullBackup, LastLogBackup)
|
||||
$dgDatabases.ItemsSource = $null
|
||||
$dgServerErrors.ItemsSource = $null
|
||||
|
||||
# Also, update txtServerName with the parent server's tag so they see which server it belongs to
|
||||
@@ -186,6 +211,7 @@ $tvServers.Add_SelectedItemChanged({
|
||||
# Hide detail panel if selection is null or ROOT
|
||||
$gridDetails.Visibility = "Collapsed"
|
||||
$dgServerInfo.ItemsSource = $null
|
||||
$dgDatabases.ItemsSource = $null
|
||||
$dgServerErrors.ItemsSource = $null
|
||||
}
|
||||
})
|
||||
@@ -267,6 +293,25 @@ $btnConnect.Add_Click({
|
||||
Write-Log "Connected successfully to $($result.Instance)!"
|
||||
$dgServerInfo.ItemsSource = @([PSCustomObject]$result | Select-Object Instance, Version, Edition, Status)
|
||||
|
||||
# Fetch databases list using Get-SQLDatabases
|
||||
Write-Log "Fetching databases list for $server..."
|
||||
try {
|
||||
$dbs = Get-SQLDatabases -ServerInstance $server
|
||||
if ($dbs -and $dbs.Count -gt 0) {
|
||||
$dgDatabases.ItemsSource = @($dbs)
|
||||
$result.Databases = $dbs
|
||||
Write-Log "Loaded $($dbs.Count) databases."
|
||||
} else {
|
||||
$dgDatabases.ItemsSource = $null
|
||||
$result.Databases = $null
|
||||
Write-Log "No databases found on $server."
|
||||
}
|
||||
} catch {
|
||||
Write-Log "Warning: Could not load databases: $($_.Exception.Message)"
|
||||
$dgDatabases.ItemsSource = $null
|
||||
$result.Databases = $null
|
||||
}
|
||||
|
||||
# Fetch errors from the last hour using Get-SQLErrors
|
||||
Write-Log "Fetching error logs (last hour) for $server..."
|
||||
try {
|
||||
@@ -277,25 +322,29 @@ $btnConnect.Add_Click({
|
||||
Write-Log "Loaded $($errors.Count) error log entries."
|
||||
} else {
|
||||
$dgServerErrors.ItemsSource = $null
|
||||
$result.Errors = $null
|
||||
Write-Log "No errors found in the last hour."
|
||||
}
|
||||
} catch {
|
||||
Write-Log "Warning: Could not load error log: $($_.Exception.Message)"
|
||||
$dgServerErrors.ItemsSource = $null
|
||||
$result.Errors = $null
|
||||
}
|
||||
|
||||
# Cache successful connection state with its fetched errors
|
||||
# Cache successful connection state with its fetched errors and databases
|
||||
$script:ServerConnectionStates[$server] = $result
|
||||
} else {
|
||||
Write-Log "Connection failed: $($result.Error)"
|
||||
$script:ServerConnectionStates[$server] = $null
|
||||
$dgServerInfo.ItemsSource = $null
|
||||
$dgDatabases.ItemsSource = $null
|
||||
$dgServerErrors.ItemsSource = $null
|
||||
}
|
||||
} catch {
|
||||
Write-Log "Critical Error: $($_.Exception.Message)"
|
||||
$script:ServerConnectionStates[$server] = $null
|
||||
$dgServerInfo.ItemsSource = $null
|
||||
$dgDatabases.ItemsSource = $null
|
||||
$dgServerErrors.ItemsSource = $null
|
||||
} finally {
|
||||
$btnConnect.IsEnabled = $true
|
||||
|
||||
Reference in New Issue
Block a user