My SQL Server stored procedure is as follows:
CREATE PROCEDURE ProcessAssessments
@assessmentIds AssessmentList READONLY,
@failed BIT
AS
UPDATE dbo.table
SET IsProcessed = 1,
dbo.ProcessFailed = @failed,
dbo.ProcessDate = GETDATE()
WHERE
dbo.ID IN (SELECT AssessmentId FROM @assessmentIds)
GO
In Powershell: under global definition, I am declaring the $connection, and ArrayList.
$connection = New-Object System.Data.SqlClient.SqlConnection($connectionString)
$connection.Open()
$processedAssessments.Add("1")
$processedAssessments.Add("2")
$processedAssessments.Add("3")
UpdateAssessments($processedAssessments, 0)
Inside of the Method, I'm converting the ArrayList to a DataTable, and supplying it as a parameter to my stored procedure.
function UpdateAssessments([System.Collections.ArrayList] $assessments,[bool] $failed) {
$table = New-Object System.Data.DataTable
$table.Columns.Add("AssessmentId", "System.String") | Out-Null
foreach($assessmentId in $assessments) {
$row = $table.NewRow()
$row.AssessmentId = $assessmentId
$table.Rows.Add($row)
}
$command = New-Object System.Data.SqlClient.SqlCommand
$command.CommandType = [System.Data.CommandType]::StoredProcedure
$command.CommandText = "ProcessKySpecialAssessments"
$command.Connection = $connection
$command.Parameters.Add("@assessmentIds", $table)
$command.Parameters.Add("@failed", $failed)
$Command.ExecuteNonQuery()
}
However, when I execute this, I get the following exception:
Exception calling "ExecuteNonQuery" with "0" argument(s): "Conversion failed when converting the nvarchar value 'System.Collections.ArrayList' to data type int.
The data for table-valued parameter "@assessmentIds" doesn't conform to the table type of the parameter.
SQL Server error is: 245, state: 1
I'm really not sure what I'm doing wrong.
New-Object SomeType(arg1, ...), useNew-Object SomeType [-ArgumentList] arg1, ...- PowerShell cmdlets and functions are invoked like shell commands (no parentheses, whitespace-separated arguments), not like methods.