101 lines
3.5 KiB
PowerShell
101 lines
3.5 KiB
PowerShell
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 using native ADO.NET.
|
|
#>
|
|
param(
|
|
[Parameter(Mandatory=$true)]
|
|
[string]$ServerInstance
|
|
)
|
|
|
|
$connString = "Server=$ServerInstance;Database=master;Integrated Security=True;Connection Timeout=5;TrustServerCertificate=True;"
|
|
$connection = New-Object System.Data.SqlClient.SqlConnection($connString)
|
|
try {
|
|
$connection.Open()
|
|
|
|
# Query basic server properties
|
|
$cmd = $connection.CreateCommand()
|
|
$cmd.CommandText = "SELECT @@VERSION as Version, SERVERPROPERTY('Edition') as Edition, SERVERPROPERTY('MachineName') as Instance, SERVERPROPERTY('ProductVersion') as VerString"
|
|
$reader = $cmd.ExecuteReader()
|
|
|
|
if ($reader.Read()) {
|
|
$ver = $reader["VerString"].ToString()
|
|
$edition = $reader["Edition"].ToString()
|
|
$instance = $reader["Instance"].ToString()
|
|
|
|
return @{
|
|
Success = $true
|
|
Instance = if ([string]::IsNullOrEmpty($instance)) { $ServerInstance } else { $instance }
|
|
Version = $ver
|
|
Edition = $edition
|
|
Status = "Online"
|
|
}
|
|
} else {
|
|
return @{ Success = $false; Error = "Failed to read server properties." }
|
|
}
|
|
} catch {
|
|
return @{ Success = $false; Error = $_.Exception.Message }
|
|
} finally {
|
|
if ($null -ne $connection -and $connection.State -eq [System.Data.ConnectionState]::Open) {
|
|
$connection.Close()
|
|
}
|
|
}
|
|
}
|
|
|
|
function Get-SQLDatabases {
|
|
<#
|
|
.SYNOPSIS
|
|
Gets database information for a SQL Server instance using native ADO.NET.
|
|
#>
|
|
param(
|
|
[Parameter(Mandatory=$true)]
|
|
[string]$ServerInstance
|
|
)
|
|
|
|
$connString = "Server=$ServerInstance;Database=master;Integrated Security=True;Connection Timeout=5;TrustServerCertificate=True;"
|
|
$connection = New-Object System.Data.SqlClient.SqlConnection($connString)
|
|
try {
|
|
$connection.Open()
|
|
|
|
# Query databases from sys.databases
|
|
$cmd = $connection.CreateCommand()
|
|
$cmd.CommandText = "SELECT name, state_desc, (size * 8.0 / 1024.0) as size_mb, recovery_model_desc FROM sys.databases"
|
|
|
|
$adapter = New-Object System.Data.SqlClient.SqlDataAdapter($cmd)
|
|
$table = New-Object System.Data.DataTable
|
|
$null = $adapter.Fill($table)
|
|
|
|
$dbList = @()
|
|
foreach ($row in $table.Rows) {
|
|
$dbList += [pscustomobject]@{
|
|
Name = $row.name
|
|
Status = $row.state_desc
|
|
Size = "$([Math]::Round([double]$row.size_mb, 2)) MB"
|
|
RecoveryModel = $row.recovery_model_desc
|
|
}
|
|
}
|
|
return $dbList
|
|
} catch {
|
|
throw "Failed to get databases: $($_.Exception.Message)"
|
|
} finally {
|
|
if ($null -ne $connection -and $connection.State -eq [System.Data.ConnectionState]::Open) {
|
|
$connection.Close()
|
|
}
|
|
}
|
|
}
|
|
|
|
Export-ModuleMember -Function Import-DBAToolBoxDependencies, Test-SQLConnection, Get-SQLDatabases |