I’m trying to read a SQL script file from github using
Invoke-WebRequest -Uri $ScriptUrl -UseBasicParsing
and then trying to execute this script in my local SQL Server using
Invoke-Sqlcmd -ServerInstance $sqlinstance -database $db -query $qry
but am getting error:
Invoke-SqlCmd: Incorrect Syntax near ‘?’
I tried to use
Invoke-RestMethod -uri $ScriptUrl -UseBasicParsing
but this time also getting the same issue.
I found the fix for the issue. I used Remove(0,1) as '?' mark was appended at the starting. Let me show the cmdlets:
Add-PSSnapin SqlServerCmdletSnapin100
Add-PSSnapin SqlServerProviderSnapin100
#set variables
$GitRepository = "/XYZ/ProjectName/main/DB"
$GitManifestArray =@("Script1.sql","Script2.sql","Script3.sql","StoredProcedures.sql","Script4.sql","Script5.sql")
$SQLInstances = ".\SQLEXPRESS"
ForEach ($SQLInstance in $SQLInstances.Split(","))
{
echo "Running Scripts on " $SQLInstance
ForEach ($file in $GitManifestArray)
{
$ScriptURL = ("https://raw.githubusercontent.com" +
$GitRepository+"/"+$file.Replace("/blob/","/"))
$ScriptFromGit = Invoke-WebRequest -Uri $ScriptURL -UseBasicParsing
$qry=$($ScriptFromGit.Content).Remove(0,1)
Invoke-Sqlcmd -ServerInstance $SQLInstance -Query $qry
}}
But still I don't know why '?' is being added at the starting of Script1.sql(For me its Database.sql).