Jun 8, 2026, 10:09 AM
This commit is contained in:
@@ -1,3 +1,9 @@
|
||||
try {
|
||||
Add-Type -AssemblyName System.Data
|
||||
} catch {
|
||||
# Ignore if already loaded or not available in some environments
|
||||
}
|
||||
|
||||
function Import-DBAToolBoxDependencies {
|
||||
<#
|
||||
.SYNOPSIS
|
||||
@@ -33,9 +39,9 @@ function Test-SQLConnection {
|
||||
$reader = $cmd.ExecuteReader()
|
||||
|
||||
if ($reader.Read()) {
|
||||
$ver = $reader["VerString"].ToString()
|
||||
$edition = $reader["Edition"].ToString()
|
||||
$instance = $reader["Instance"].ToString()
|
||||
$ver = if ($reader["VerString"] -ne [System.DBNull]::Value -and $reader["VerString"] -ne $null) { $reader["VerString"].ToString() } else { "Unknown" }
|
||||
$edition = if ($reader["Edition"] -ne [System.DBNull]::Value -and $reader["Edition"] -ne $null) { $reader["Edition"].ToString() } else { "Unknown" }
|
||||
$instance = if ($reader["Instance"] -ne [System.DBNull]::Value -and $reader["Instance"] -ne $null) { $reader["Instance"].ToString() } else { "" }
|
||||
|
||||
return @{
|
||||
Success = $true
|
||||
@@ -50,7 +56,7 @@ function Test-SQLConnection {
|
||||
} catch {
|
||||
return @{ Success = $false; Error = $_.Exception.Message }
|
||||
} finally {
|
||||
if ($null -ne $connection -and $connection.State -eq [System.Data.ConnectionState]::Open) {
|
||||
if ($null -ne $connection -and $connection.State.ToString() -eq "Open") {
|
||||
$connection.Close()
|
||||
}
|
||||
}
|
||||
@@ -71,9 +77,19 @@ function Get-SQLDatabases {
|
||||
try {
|
||||
$connection.Open()
|
||||
|
||||
# Query databases from sys.databases
|
||||
# Query databases from sys.databases joined with sys.master_files to correctly sum file sizes.
|
||||
# sys.databases does not have a "size" column. We must group and sum size from sys.master_files.
|
||||
$cmd = $connection.CreateCommand()
|
||||
$cmd.CommandText = "SELECT name, state_desc, (size * 8.0 / 1024.0) as size_mb, recovery_model_desc FROM sys.databases"
|
||||
$cmd.CommandText = @"
|
||||
SELECT
|
||||
d.name,
|
||||
d.state_desc,
|
||||
COALESCE(SUM(CAST(f.size AS FLOAT) * 8.0 / 1024.0), 0.0) as size_mb,
|
||||
d.recovery_model_desc
|
||||
FROM sys.databases d
|
||||
LEFT JOIN sys.master_files f ON d.database_id = f.database_id
|
||||
GROUP BY d.name, d.state_desc, d.recovery_model_desc
|
||||
"@
|
||||
|
||||
$adapter = New-Object System.Data.SqlClient.SqlDataAdapter($cmd)
|
||||
$table = New-Object System.Data.DataTable
|
||||
@@ -81,18 +97,27 @@ function Get-SQLDatabases {
|
||||
|
||||
$dbList = @()
|
||||
foreach ($row in $table.Rows) {
|
||||
$sizeVal = 0.0
|
||||
if ($row.size_mb -ne [System.DBNull]::Value -and $row.size_mb -ne $null) {
|
||||
$sizeVal = [double]$row.size_mb
|
||||
}
|
||||
|
||||
$dbName = if ($row.name -ne [System.DBNull]::Value -and $row.name -ne $null) { $row.name.ToString() } else { "Unknown" }
|
||||
$dbStatus = if ($row.state_desc -ne [System.DBNull]::Value -and $row.state_desc -ne $null) { $row.state_desc.ToString() } else { "Unknown" }
|
||||
$dbRecovery = if ($row.recovery_model_desc -ne [System.DBNull]::Value -and $row.recovery_model_desc -ne $null) { $row.recovery_model_desc.ToString() } else { "Simple" }
|
||||
|
||||
$dbList += [pscustomobject]@{
|
||||
Name = $row.name
|
||||
Status = $row.state_desc
|
||||
Size = "$([Math]::Round([double]$row.size_mb, 2)) MB"
|
||||
RecoveryModel = $row.recovery_model_desc
|
||||
Name = $dbName
|
||||
Status = $dbStatus
|
||||
Size = "$([Math]::Round($sizeVal, 2)) MB"
|
||||
RecoveryModel = $dbRecovery
|
||||
}
|
||||
}
|
||||
return $dbList
|
||||
} catch {
|
||||
throw "Failed to get databases: $($_.Exception.Message)"
|
||||
} finally {
|
||||
if ($null -ne $connection -and $connection.State -eq [System.Data.ConnectionState]::Open) {
|
||||
if ($null -ne $connection -and $connection.State.ToString() -eq "Open") {
|
||||
$connection.Close()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user