Skip to content

Instantly share code, notes, and snippets.

@bohdanszymanik
Created December 22, 2016 20:46
Show Gist options
  • Save bohdanszymanik/11d715600c511ae058a477994926c7a4 to your computer and use it in GitHub Desktop.
Save bohdanszymanik/11d715600c511ae058a477994926c7a4 to your computer and use it in GitHub Desktop.
Get-Credential example for sql command
$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