Another Handy SQL function in PowerShell
Written by Niklas Goude on January 30, 2009 – 12:29 pm
The Get-SQL function retrieved data from SQL and presented it in a HashTable Array.
Let’s create a similar function that we can use when updating, inserting or deleting data from an SQL server.
function Set-SQL ([string]$Query,[string]$ConnString) {
if ($ConnString) {
if($ConnString -match ‘”*”‘) {
$ConnString = $ConnString.TrimStart(‘”‘)
$ConnString = $ConnString.TrimEnd(‘”‘)
}
} else {
# Default ConnectionString
$ConnString =
“server=SQL;database=master;trusted_connection=true;”
}
$Connection = New-Object System.Data.SQLClient.SQLConnection
$Connection.ConnectionString = $ConnString
$Connection.Open()
$Command = [...]

