Created
December 22, 2016 20:46
-
-
Save bohdanszymanik/11d715600c511ae058a477994926c7a4 to your computer and use it in GitHub Desktop.
Get-Credential example for sql command
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$Cred = Get-Credential -Message "Enter database credentials" -UserName $DBUser | |
function Invoke-SqlCmd { | |
param( | |
[string] $sqlCommand = $(throw "Please specify a query.") | |
) | |
$connectionString = "Server = someServer,1234; Network Library=DBMSSOCN; Initial catalog = $db; User Id = $($Cred.UserName); Password = $($Cred.GetNetworkCredential().Password);" | |
$connection = new-object system.data.SqlClient.SQLConnection($connectionString) | |
$command = new-object system.data.sqlclient.sqlcommand($sqlCommand,$connection) | |
$command.CommandTimeout = 0 # infinite wait | |
$connection.Open() | |
$adapter = New-Object System.Data.sqlclient.sqlDataAdapter $command | |
$dataset = New-Object System.Data.DataSet | |
$adapter.Fill($dataSet) | Out-Null | |
$connection.Close() | |
$dataSet | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment