Azure SQL Server invoke-sqlcmd Login failed for user token-identified principal

Hoàng Lê Hiệp Minh 51 Reputation points
2022-11-24T15:08:48.95+00:00

0

I'm trying setup Pipeline Release for automation to create User Managed Identity and add it to SQL, I have finished CREATE USER + ALTER ROLE with user AAD login to SQL Server on VM, but when I try setup on Release it stuck in auth with the error below (user AAD don't set MFA):

Anyone has ever met that before, please give me some method to resolve it. Thanks!

![enter image description here]2

enter image description here

$ResourceGroup ='AAC-BYC-NNY'
$SQLServer = 'azcacaufd1devsql01.database.windows.net'
$Database = 'Core'
$MyIdentity = 'user15'

Create User Managed Identity

az identity create --resource-group $ResourceGroup --name $MyIdentity

add UMI to sql

$query = "

CREATE USER [$MyIdentity] FROM EXTERNAL PROVIDER;

ALTER ROLE db_datareader ADD MEMBER [$MyIdentity];

ALTER ROLE db_datawriter ADD MEMBER [$MyIdentity];

GRANT EXECUTE TO [$MyIdentity]

GO
"

write-output "Create DB Account named $MyIdentity"

$connectString="Data Source=tcp:$SQLServer,1433;Initial Catalog=$Ddatabase;Authentication=Active Directory Password;User ID='devops@hosting.com';Password='123456';Trusted_Connection=False;Encrypt=True;Connection Timeout=30"

Invoke-Sqlcmd -ConnectionString $connectString -Query $query

Azure SQL Database
SQL Server Integration Services
SQL Server Integration Services
A Microsoft platform for building enterprise-level data integration and data transformations solutions.
2,452 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Alberto Morillo 32,886 Reputation points MVP
    2022-11-24T19:44:23.84+00:00

    Please make sure you create the user database first, you get connected to the user database after that (and you are not connected to the master database) and then you execute the CREATE USER statements. Please read this documentation for more details and requirements.


  2. Hoàng Lê Hiệp Minh 51 Reputation points
    2022-11-29T03:39:53.68+00:00

    The script is correct, the root cause is the value variable I have setting incorrect.
    Thanks!