From a6824dbd33def8f6f5bd22b3eb379b371c027957 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82=20Doma=C5=84ski?= Date: Thu, 28 May 2026 06:53:37 +0000 Subject: [PATCH] May 28, 2026, 8:53 AM --- toolbox/DBAToolBox.ps1 | 71 +++++++++++++++++++++++++++++++++ toolbox/Modules/SQLManager.psm1 | 47 ++++++++++++++++++++++ toolbox/UI/MainWindow.xaml | 30 ++++++++++++++ 3 files changed, 148 insertions(+) create mode 100644 toolbox/DBAToolBox.ps1 create mode 100644 toolbox/Modules/SQLManager.psm1 create mode 100644 toolbox/UI/MainWindow.xaml diff --git a/toolbox/DBAToolBox.ps1 b/toolbox/DBAToolBox.ps1 new file mode 100644 index 0000000..9146840 --- /dev/null +++ b/toolbox/DBAToolBox.ps1 @@ -0,0 +1,71 @@ +Add-Type -AssemblyName PresentationFramework, PresentationCore, WindowsBase + +# Import Custom Modules +Import-Module (Join-Path $PSScriptRoot "Modules\SQLManager.psm1") -Force + +# 1. Path to XAML +$xamlFile = Join-Path $PSScriptRoot "UI\MainWindow.xaml" + +# 2. Load XAML +[xml]$xml = Get-Content $xamlFile +$reader = New-Object System.Xml.XmlNodeReader($xml) +$Window = [Windows.Markup.XamlReader]::Load($reader) + +# 3. Extract controls from XAML +$btnConnect = $Window.FindName("btnConnect") +$txtServerName = $Window.FindName("txtServerName") +$txtLogs = $Window.FindName("txtLogs") +$dgServerInfo = $Window.FindName("dgServerInfo") + +# 4. Helper function for logging +function Write-Log { + param([string]$Message) + $timestamp = Get-Date -Format "HH:mm:ss" + $txtLogs.Dispatcher.Invoke({ + $txtLogs.AppendText("[$timestamp] $Message`r`n") + $txtLogs.ScrollToEnd() + }) +} + +# 5. Connection logic +$btnConnect.Add_Click({ + $server = $txtServerName.Text + if ([string]::IsNullOrWhiteSpace($server)) { + Write-Log "Error: Please enter a server name." + return + } + + $btnConnect.IsEnabled = $false + Write-Log "Attempting to connect to $server..." + + # Running connection test in a separate thread/job would be better for UI responsiveness, + # but for this prototype we'll keep it simple or use a minimal background approach if possible. + # For now, let's just do it directly. + + try { + $result = Test-SQLConnection -ServerInstance $server + + if ($result.Success) { + Write-Log "Connected successfully to $($result.Instance)!" + $dgServerInfo.ItemsSource = @($result) | Select-Object Instance, Version, Edition, Status + } else { + Write-Log "Connection failed: $($result.Error)" + } + } catch { + Write-Log "Critical Error: $($_.Exception.Message)" + } finally { + $btnConnect.IsEnabled = $true + } +}) + +# Startup Dependency Check +try { + Import-DBAToolBoxDependencies + Write-Log "DBAToolBox started. dbatools module loaded." +} catch { + [System.Windows.MessageBox]::Show($_.Exception.Message, "Dependency Error", "OK", "Error") + $Window.Close() +} + +# 6. Show Window +$Window.ShowDialog() | Out-Null \ No newline at end of file diff --git a/toolbox/Modules/SQLManager.psm1 b/toolbox/Modules/SQLManager.psm1 new file mode 100644 index 0000000..1a44c77 --- /dev/null +++ b/toolbox/Modules/SQLManager.psm1 @@ -0,0 +1,47 @@ +function Import-DBAToolBoxDependencies { + <# + .SYNOPSIS + Ensures the dbatools module is available and imported. + #> + if (-not (Get-Module -ListAvailable dbatools)) { + throw "The 'dbatools' module is not installed. Please install it using 'Install-Module dbatools'." + } + + if (-not (Get-Module dbatools)) { + Import-Module dbatools -ErrorAction Stop + } +} + +function Test-SQLConnection { + <# + .SYNOPSIS + Tests connection to a SQL Server instance and returns basic info. + #> + param( + [Parameter(Mandatory=$true)] + [string]$ServerInstance + ) + + try { + # Using Test-DbaConnection from dbatools + $testResult = Test-DbaConnection -SqlInstance $ServerInstance -ErrorAction Stop + + if ($testResult.Connect -eq $true) { + # If connection is successful, get some basic instance info + $instanceInfo = Get-DbaInstance -SqlInstance $ServerInstance -ErrorAction Stop + return @{ + Success = $true + Instance = $instanceInfo.Name + Version = $instanceInfo.VersionString + Edition = $instanceInfo.Edition + Status = "Online" + } + } else { + return @{ Success = $false; Error = "Connection failed." } + } + } catch { + return @{ Success = $false; Error = $_.Exception.Message } + } +} + +Export-ModuleMember -Function Import-DBAToolBoxDependencies, Test-SQLConnection \ No newline at end of file diff --git a/toolbox/UI/MainWindow.xaml b/toolbox/UI/MainWindow.xaml new file mode 100644 index 0000000..e38ab03 --- /dev/null +++ b/toolbox/UI/MainWindow.xaml @@ -0,0 +1,30 @@ + + + + + + + + + + + + + + +