Public/Get-DBConn.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
function Get-DBConn {
    [OutputType([string[]])]
    param(
        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [string]$hostname, 
        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [string]$username, 
        [Parameter(Mandatory=$true)]
        [ValidateNotNullOrEmpty()]
        [string]$password
    )
    $ErrorActionPreference = 'Stop'
    $connArgs = Get-DBConnArgs -hostname $hostname -username $username -password $password
    Write-Warning "Connection Arguments: $connArgs"
    $connected = Connect-DB -arguments $connArgs
    if($connected -eq $true) {
        return $connArgs
    } else {
        throw "Database connection failed."
    }
}