Skip to content

Instantly share code, notes, and snippets.

@mikaelweave
Created February 1, 2016 16:54
Show Gist options
  • Save mikaelweave/fb6751ad6c213b77d820 to your computer and use it in GitHub Desktop.
Save mikaelweave/fb6751ad6c213b77d820 to your computer and use it in GitHub Desktop.
Database Connection Powershell snippits
function Invoke-SQL {
param(
[string] $dataSource = $(throw "Please specify a datasource."),
[string] $database = $(throw "Please specify a database."),
[string] $sqlCommand = $(throw "Please specify a query.")
)
$connectionString = "Data Source=$dataSource; " +
"Integrated Security=SSPI; " +
"Initial Catalog=$database"
$connection = new-object system.data.SqlClient.SQLConnection($connectionString)
$command = new-object system.data.sqlclient.sqlcommand($sqlCommand,$connection)
$connection.Open()
$adapter = New-Object System.Data.sqlclient.sqlDataAdapter $command
$dataset = New-Object System.Data.DataSet
$adapter.Fill($dataSet) | Out-Null
$connection.Close()
$dataSet.Tables
}
$sqlServerName = "SPF-SV-DYNDVDB1"
###############
#Get any active output actions with attachments. Catalog them
###############
$database = "CaseTrakker"
$sSQL = "SELECT TOP 10 * FROM ct_note"
$dataTable = Invoke-SQL -dataSource:$sqlServerName -database:$database -sqlCommand:$sSQL
#Query the database to get any attachemnts
foreach ($row in $dataTable | Where {$_.object_type -eq "111"} | Sort-Object ct_id)
{
Write-Host $row["ct_id"]
}
Write-Host "DONE"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment