Saturday, April 4, 2020

Invoke Invoke-SqlCmd inside a Linux Agent in Azure DevOps Pipelines

I was trying to execute Invoke-Sqlcmd command inside a Linux Agent in Azure DevOps Pipelines and getting this error.

"The term 'Invoke-Sqlcmd' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again."
The term 'Invoke-Sqlcmd' is not recognized...
Then according to this post: Invoke-Sqlcmd is Now Available Supporting Cross-Platform, installed SqlServer module from another PowerShell@2 task.
- task: PowerShell@2
  inputs:
    targetType: 'inline'
    script: 'Install-Module -Name SqlServer -AllowPrerelease'
That task passed, but still, the PS Invoke-SqlCmd task was throwing the same error. But noticed this warning inside PS Install-Module task.

"WARNING: User declined to install module (SqlServer)."
WARNING: User declined to install module (SqlServer)
Then I modified the PS Install-Module task to pass in an additional parameter -Scope CurrentUser.
- task: PowerShell@2
  inputs:
    targetType: 'inline'
    script: 'Install-Module -Name SqlServer -AllowPrerelease -Force -Verbose -Scope CurrentUser'
This time it got installed successfully.
Install-Module -Name SqlServer
And finally I was able to run Invoke-SqlCmd inside a Linux Agent in Azure DevOps Pipeline.
Invoke-SqlCmd is running
Hope this helps!

Happy Coding.

Regards,
Jaliya

No comments:

Post a Comment