89 lines
2.9 KiB
PowerShell
89 lines
2.9 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.
|
|
#>
|
|
param(
|
|
[Parameter(Mandatory=$true)]
|
|
[string]$ServerInstance
|
|
)
|
|
|
|
$connWarnings = $null
|
|
try {
|
|
# Using Test-DbaConnection from dbatools and suppressing console warnings while capturing them
|
|
$testResult = Test-DbaConnection -SqlInstance $ServerInstance -ErrorAction Stop -WarningAction SilentlyContinue -WarningVariable connWarnings
|
|
|
|
if ($testResult -and $testResult.Connect -eq $true) {
|
|
# If connection is successful, get some basic instance info
|
|
$instanceInfo = Get-DbaInstance -SqlInstance $ServerInstance -ErrorAction Stop -WarningAction SilentlyContinue
|
|
return @{
|
|
Success = $true
|
|
Instance = $instanceInfo.Name
|
|
Version = $instanceInfo.VersionString
|
|
Edition = $instanceInfo.Edition
|
|
Status = "Online"
|
|
}
|
|
} else {
|
|
$errDetail = "Connection failed."
|
|
if ($connWarnings) {
|
|
$errDetail = "$errDetail ($($connWarnings -join '; '))"
|
|
}
|
|
return @{ Success = $false; Error = $errDetail }
|
|
}
|
|
} catch {
|
|
$errMsg = $_.Exception.Message
|
|
if ($connWarnings) {
|
|
$errMsg = "$errMsg ($($connWarnings -join '; '))"
|
|
}
|
|
return @{ Success = $false; Error = $errMsg }
|
|
}
|
|
}
|
|
|
|
function Get-SQLDatabases {
|
|
<#
|
|
.SYNOPSIS
|
|
Gets database information for a SQL Server instance.
|
|
#>
|
|
param(
|
|
[Parameter(Mandatory=$true)]
|
|
[string]$ServerInstance
|
|
)
|
|
|
|
$dbWarnings = $null
|
|
try {
|
|
# Using Get-DbaDatabase from dbatools and suppressing console warnings while capturing them
|
|
$databases = Get-DbaDatabase -SqlInstance $ServerInstance -ErrorAction Stop -WarningAction SilentlyContinue -WarningVariable dbWarnings
|
|
$dbList = @()
|
|
foreach ($db in $databases) {
|
|
$dbList += [pscustomobject]@{
|
|
Name = $db.Name
|
|
Status = $db.Status
|
|
Size = $db.Size
|
|
RecoveryModel = $db.RecoveryModel
|
|
}
|
|
}
|
|
return $dbList
|
|
} catch {
|
|
$errMsg = $_.Exception.Message
|
|
if ($dbWarnings) {
|
|
$errMsg = "$errMsg ($($dbWarnings -join '; '))"
|
|
}
|
|
throw "Failed to get databases: $errMsg"
|
|
}
|
|
}
|
|
|
|
Export-ModuleMember -Function Import-DBAToolBoxDependencies, Test-SQLConnection, Get-SQLDatabases |