param( [string] $database = "MYdb", [string] $dataSource="ServerA" ) $Path = "c:\powershell\ss\$dataSource.XML" # Import credential file $import = Import-Clixml $Path if ($import.AuthenticationType -eq "Integrated") { $authentication = "Integrated Security=SSPI;" } else { # Test for valid import if ( !$import.UserName -or !$import.Password ) { Throw "Input is not a valid ExportedPSCredential object, exiting." } $username = $import.Username # Decrypt the password and store as a SecureString object for safekeeping $SecurePass = $import.Password | ConvertTo-SecureString # Build the new credential object $Credential = New-Object System.Management.Automation.PSCredential $Username, $SecurePass $plainCred = $credential.GetNetworkCredential() $authentication = ("uid={0};pwd={1};" -f $plainCred.Username,$plainCred.Password) } $connectionString = "Persist Security Info=True;Initial Catalog=$database;Data Source=$dataSource;$authentication" ## Connect to the data source and open it $connection = new-object System.Data.SqlClient.SqlConnection $connectionString $connection