47 lines
1.5 KiB
PowerShell
47 lines
1.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.
|
|
#>
|
|
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 |