Private/Get-SQLServerConnection.ps1

Function Get-SQLServerConnection {
    param (
        [parameter(Mandatory=$True)]
            [ValidateNotNullOrEmpty()]
            [string] $SQLServer,
        [parameter(Mandatory=$True)]
            [ValidateNotNullOrEmpty()]
            [string] $DBName
    )
    try {
            $conn = New-Object System.Data.SqlClient.SqlConnection
            $conn.ConnectionString = "Data Source=$SQLServer;Initial Catalog=$DBName;Integrated Security=SSPI;"
            return $conn
    }
    catch {
            $errorMessage = $_.Exception.Message
            $errorCode = "0x{0:X}" -f $_.Exception.ErrorCode
            Write-Log -Message "The following error happen, no futher action taken" -LogFile $logfile
            Write-Log -Message "Error $errorCode : $errorMessage connecting to $SQLServer" -Severity 3 -LogFile $logfile
            $Error.Clear()
            Throw "Error $errorCode : $errorMessage connecting to $SQLServer"
    }
}