[ad_1]
The following script works locally, using the same connection details as the Azure DevOps pipeline
param ($server, $database, $username, $password, $script)
#
# Create the temporary file to contain the modified script
#
$newscript = Join-Path -Path (Split-Path -Path $script) -ChildPath ("fixed_" + (Split-Path -Path $script -Leaf));
Write-Output $newscript
Set-Content -Path $newscript -Value "" -Encoding 'utf8'
#
# Fetch the currently applied migrations
#
$migrationIds = ""
$qry = Invoke-Sqlcmd -ServerInstance "$server" -Database "$database" -Username "$username" -Password "$password" -Query "SELECT DISTINCT [MigrationId] FROM [__EFMigrationsHistory]" -ErrorAction SilentlyContinue
Write-Output($qry)
if ($null -ne $qry) {
$migrationIds = ($qry | Select-Object -ExpandProperty MigrationId) -Join "|"
}
#
# Match the chunks in the script with the list of applied migrations, and comment them out
#
if ($migrationIds -ne "") {
$regex = "(?ms)^IF NOT EXISTS\(SELECT \* FROM \[__EFMigrationsHistory\] WHERE \[MigrationId\] = N'(" + $migrationIds + ")'\).*?END;\s+GO"
$c = (Get-Content $script -Raw) -replace $regex, "/*`r`n`$0`r`n*/";
Set-Content -Path $newscript -Value $c -Encoding 'utf8'
}
else {
Copy-Item $script $newscript
}
When it’s run through an Azure DevOps pipeline, it throws a ParserException.
Invoke-Sqlcmd :
At D:\a\1\s\devops\azure\ef-idempotent-fixer.ps1:14 char:8
+ $qry = Invoke-Sqlcmd -ServerInstance "$server" -Database "$database" ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ParserError: (:) [Invoke-Sqlcmd], ParserException
+ FullyQualifiedErrorId : ExecutionFailureException,Microsoft.SqlServer.Management.PowerShell.GetScriptCommand
I’ve checked the variables being passed through from the pipeline all look OK, yet I can’t see why this would be breaking in the pipeline.
[ad_2]