I have a PowerShell script the with get data and then run a SQL file, parsing in the data it have got. However I keep getting the following error:
Invoke-Sqlcmd : The format used to define the new variable for Invoke-Sqlcmd cmdlet is invalid. Please use the 'var=value' format for defining a new variable.
PowerShell Script:
Write-host $ID, $XMLParam, $SourceID
$DBParam1 = "ID="+ $ID
$DBParam2 = "XMLPARAM="+ $XMLParam
$DBParam3 = "SOURCEID="+ $SourceID
$DBParams = @($DBParam1, $DBParam2, $DBParam3)
$Results = Invoke-Sqlcmd -InputFile "D:\SamHelp\marksheet.sql" -Variable $DBParams -Server $SQLServer -Database 'DB' -Username $Username -Password $Password
SQL File:
declare @id int, @xmlParam xml, @sourceID int
SET @id = '$(ID)'
SET @xmlParam = '$(XMLPARAM)'
SET @sourceID = '$(SOURCEID)'
...
The Write-Host in the powershell outputs:
001 <data><destSu id="000001"/><destSu id="000002"/></data> 0000001
$DBParam2 = "XMLPARAM='"+ $XMLParam + "'". Not sure if you'll need to do it for the other two or not, since they're numeric$DBParam2 = "XMLPARAM="+ $XMLParam.Replace('"', '""'). This will escape the double quotes in the SQLCMD scripting variable value.