May 28, 2026, 8:53 AM

This commit is contained in:
Paweł Domański
2026-05-28 06:53:37 +00:00
parent 04d97e61fb
commit a6824dbd33
3 changed files with 148 additions and 0 deletions
+71
View File
@@ -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
+47
View File
@@ -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
+30
View File
@@ -0,0 +1,30 @@
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="DBAToolBox - MS SQL Management" Height="450" Width="600" Background="#F0F0F0">
<Grid Margin="10">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<!-- Header -->
<TextBlock Text="DBAToolBox" FontSize="24" FontWeight="Bold" Foreground="#2D3E50" Margin="0,0,0,10"/>
<!-- Connection Panel -->
<StackPanel Grid.Row="1" Orientation="Horizontal" Margin="0,0,0,10">
<Label Content="Server Name:" VerticalAlignment="Center"/>
<TextBox x:Name="txtServerName" Width="250" VerticalAlignment="Center" Margin="5,0,10,0" Padding="3"/>
<Button x:Name="btnConnect" Content="Connect &amp; Test" Width="120" Padding="5" Background="#007ACC" Foreground="White" BorderThickness="0"/>
</StackPanel>
<!-- Main Content Area -->
<GroupBox Grid.Row="2" Header="Server Information" Margin="0,0,0,10">
<DataGrid x:Name="dgServerInfo" AutoGenerateColumns="True" IsReadOnly="True" Background="White"/>
</GroupBox>
<!-- Log Area -->
<TextBox Grid.Row="3" x:Name="txtLogs" Height="100" IsReadOnly="True" VerticalScrollBarVisibility="Auto" Background="#E8E8E8" FontFamily="Consolas" FontSize="11" TextWrapping="Wrap"/>
</Grid>
</Window>