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 {
|
function Import-DBAToolBoxDependencies {
|
||||||
<#
|
<#
|
||||||
.SYNOPSIS
|
.SYNOPSIS
|
||||||
@@ -33,9 +39,9 @@ function Test-SQLConnection {
|
|||||||
$reader = $cmd.ExecuteReader()
|
$reader = $cmd.ExecuteReader()
|
||||||
|
|
||||||
if ($reader.Read()) {
|
if ($reader.Read()) {
|
||||||
$ver = $reader["VerString"].ToString()
|
$ver = if ($reader["VerString"] -ne [System.DBNull]::Value -and $reader["VerString"] -ne $null) { $reader["VerString"].ToString() } else { "Unknown" }
|
||||||
$edition = $reader["Edition"].ToString()
|
$edition = if ($reader["Edition"] -ne [System.DBNull]::Value -and $reader["Edition"] -ne $null) { $reader["Edition"].ToString() } else { "Unknown" }
|
||||||
$instance = $reader["Instance"].ToString()
|
$instance = if ($reader["Instance"] -ne [System.DBNull]::Value -and $reader["Instance"] -ne $null) { $reader["Instance"].ToString() } else { "" }
|
||||||
|
|
||||||
return @{
|
return @{
|
||||||
Success = $true
|
Success = $true
|
||||||
@@ -50,7 +56,7 @@ function Test-SQLConnection {
|
|||||||
} catch {
|
} catch {
|
||||||
return @{ Success = $false; Error = $_.Exception.Message }
|
return @{ Success = $false; Error = $_.Exception.Message }
|
||||||
} finally {
|
} 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()
|
$connection.Close()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -71,9 +77,19 @@ function Get-SQLDatabases {
|
|||||||
try {
|
try {
|
||||||
$connection.Open()
|
$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 = $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)
|
$adapter = New-Object System.Data.SqlClient.SqlDataAdapter($cmd)
|
||||||
$table = New-Object System.Data.DataTable
|
$table = New-Object System.Data.DataTable
|
||||||
@@ -81,18 +97,27 @@ function Get-SQLDatabases {
|
|||||||
|
|
||||||
$dbList = @()
|
$dbList = @()
|
||||||
foreach ($row in $table.Rows) {
|
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]@{
|
$dbList += [pscustomobject]@{
|
||||||
Name = $row.name
|
Name = $dbName
|
||||||
Status = $row.state_desc
|
Status = $dbStatus
|
||||||
Size = "$([Math]::Round([double]$row.size_mb, 2)) MB"
|
Size = "$([Math]::Round($sizeVal, 2)) MB"
|
||||||
RecoveryModel = $row.recovery_model_desc
|
RecoveryModel = $dbRecovery
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return $dbList
|
return $dbList
|
||||||
} catch {
|
} catch {
|
||||||
throw "Failed to get databases: $($_.Exception.Message)"
|
throw "Failed to get databases: $($_.Exception.Message)"
|
||||||
} finally {
|
} 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()
|
$connection.Close()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user