jayrobot/Powershell-Settings — pull request #2 by vaishalidbpatil (2014-11-11 11:10:28+00:00)
Title: function New-SqlConnection

function New-SqlConnection breaks when executed for sql login given that no there is no user with windows login.
here is the change required

function New-SqlConnection(
[string] $Server = ".",
[string] $Database = "master",
[System.Management.Automation.PSCredential] $Credential,
[int] $ConnectionTimeout = 15
)
{
$ci = "Data Source=$Server;Initial Catalog=$Database;Connect Timeout=$ConnectionTimeout;"

if ($Credential -eq $null) {
$ci = $ci + "Integrated Security=true;";
}
else {
$BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($Credential.Password)
$PlainPassword = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)
$ci = $ci + "Integrated Security=false;User ID="+$Credential.UserName+";Password="+$PlainPassword+";"
}

$cnn = new-object System.Data.SqlClient.SqlConnection;
$cnn.ConnectionString = $ci;
$cnn.Open();
$cnn;
}