I've figured out how to run SQL queries with PowerShell and turn them into arrays I can work with. I've also figured out how to pass specific subsets of variables or arrays to the query. Like this:
$LastMonth = [DateTime]::Now#.AddMonths((-1))
# define target database and server
$TargetServer = 'server name'
$TargetDatabase = 'db name'
$ConnectionString = "Server=" + $TargetServer + ";Database=" + $TargetDatabase + ";Trusted_Connection=$true;";
$TargetConnection = New-Object System.Data.SqlClient.SqlConnection($ConnectionString);
$TargetConnection.Open();
$sql = "SELECT [AVAILABILITY_SAID_ID]
,[AVAILABILITY_MINUTES]
FROM [dbo].[AVAILABILITY_MINUTES]
where [AVAILABILITY_MONTH] = $($LastMonth.Month) and [AVAILABILITY_YEAR] = $($LastMonth.Year);"
# execute SQL command
$TargetCommand = New-Object System.Data.SqlClient.SqlCommand($sql, $TargetConnection);
$reader = $TargetCommand.ExecuteReader()
$availability = @()
while ($reader.Read())
{
$row = @{}
for ($i = 0; $i -lt $reader.FieldCount; $i++)
{
$row[$reader.GetName($i)] = $reader.GetValue($i)
}
$availability += new-object psobject -property $row
}
$reader.Close()
What I can't figure out is how to do it as an IN statement with multiple items. I can get the list to be passed, but I can't figure out how to get the parentheses around it so that the query actually works.
$TargetServer = 'server name'
$TargetDatabase = 'db name'
$ConnectionString = "Server=" + $TargetServer + ";Database=" + $TargetDatabase + ";Trusted_Connection=$true;";
$TargetConnection = New-Object System.Data.SqlClient.SqlConnection($ConnectionString);
$TargetConnection.Open();
$sql = "SELECT
incidentid AS EVENT_LIFECYCLE_VALUE,
Priority AS Priority_Code,
keycode AS ASSET_KEY,
Description AS EVENT_DATA,
CreatedDateTime AS [Opened],
ClosedDateTime AS [Closed]
FROM incident
WHERE keycode IN $("'" + ($said.Saidkeycode -join "','") + "'")
AND Priority IN (1, 2)
AND CONVERT (date, CreatedDateTime) <= DATEADD(DAY, -2, CONVERT (date, GETDATE()))
AND CreatedDateTime >= DATEADD(DAY, -90, GETDATE())
ORDER BY EVENT_LIFECYCLE_VALUE";
$SourceCommand = New-Object System.Data.SqlClient.SqlCommand($sqlCmd, $SourceConnection);
$reader = $SourceCommand.ExecuteReader()
$incidents = @()
while ($reader.Read())
{
$row = @{}
for ($i = 0; $i -lt $reader.FieldCount; $i++)
{
$row[$reader.GetName($i)] = $reader.GetValue($i)
}
$incidents += new-object psobject -property $row
}
$reader.Close()
From what I've found, parametrisation is the answer, but I haven't been able to wrap my head around how to get it to actually work. I'm not sure if this is broken or if I just can't figure out how to get the results out of the dataset.
$TargetServer = 'server name'
$TargetDatabase = 'db name'
$ConnectionString = "Server=" + $TargetServer + ";Database=" + $TargetDatabase + ";Trusted_Connection=$true;";
$TargetConnection = New-Object System.Data.SqlClient.SqlConnection($ConnectionString);
$TargetConnection.Open();
$sqlCmd = $SourceConnection.CreateCommand()
$sqlCmd.Connection = $SourceConnection
$sql = "SELECT
incidentid AS EVENT_LIFECYCLE_VALUE,
Priority AS Priority_Code,
keycode AS ASSET_KEY,
Description AS EVENT_DATA,
CreatedDateTime AS [Opened],
ClosedDateTime AS [Closed]
FROM Cherwell.dbo.Incident
WHERE keycode IN (@SAID)
AND Priority IN (1, 2)
AND CONVERT (date, CreatedDateTime) <= DATEADD(DAY, -2, CONVERT (date, GETDATE()))
AND CreatedDateTime >= DATEADD(DAY, -90, GETDATE())
ORDER BY EVENT_LIFECYCLE_VALUE";
$sqlCmd.CommandText = $sql
$sqlCmd.Parameters.Add("@SAID", [Data.SQLDBType]::VarChar, 4).Value = $("'" + ($said.Saidkeycode -join "','") + "'")
$sqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter $sqlcmd
$dataSet = New-Object System.Data.DataSet
$sqlAdapter.Fill($dataSet)