0

My task is to execute Azure MI database SP using Powershell.

I am using connection string. I believe I am able to login to the SQLMI database using the connection string with PowerShell.

However, I am getting an error while running the SQL. Below is the code and exception. Could you please help me?

$managedInstanceName = "MI"

$databaseName = "DB"

$serverAdminLogin = "t1"

$serverAdminPassword = "t1"

$query = "execute dbo.SP1"

$connectionString = "Server=tcp:$managedInstanceName.database.windows.net,1433;Persist Security Info=False;UserID=$serverAdminLogin;Password=$serverAdminPassword;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;"

Invoke-Sqlcmd -ServerInstance $managedInstanceName -Database $databaseName -Query "execute dbo.SP1"

Error:'Invoke-Sqlcmd' is not recognized as the name of a cmdlet.

jarlh
  • 42,561
  • 8
  • 45
  • 63
user112359
  • 11
  • 2
  • 5
  • Can you connect to DB with SQL Server Management Studio? If so use the server\instance in the SSMS login window. In login windows do you have Window Credential or SQL Credential. If windows credential than remove from connection string username and password and replace with Integrated Security = True. For debug info check log files with SSMS using the explorer under management. – jdweng Jun 20 '23 at 03:32

1 Answers1

0

Error:'Invoke-Sqlcmd' is not recognized as the name of a cmdlet.

The error message you got is that the cmdlet Invoke-Sqlcmd is not recognized. This can be the result of a module missing or a bad installation.

  • Try installing the SqlServer module by executing the following PowerShell command:
Install-Module -Name SqlServer

enter image description here

  • Also, It appears that you are not using the $connectionString variable that you previously specified in your code. Instead, you are utilising the Invoke-Sqlcmd cmdlet's -ServerInstance and -Database arguments.

Instead, you may try supplying the $connectionString variable as the value of the -ConnectionString option.

Example:

Invoke-Sqlcmd  -ConnectionString "Data Source=$managedInstanceName;User Id=$serverAdminLogin; Password =$serverAdminPassword; Integrated Security=False;" -Query "$Query"
Pratik Lad
  • 4,343
  • 2
  • 3
  • 11