I want to create users in Azure SQL database. I am running this PowerShell through Azure Functions.
I have set up my own name as server admin. Also I have configured the firewall.
But I am not able to connect to SQL database.
It gives me error : Cannot open server "domain.com" requested by the login. The login failed.
This is my script:
$serverName = "servername.database.windows.net"
$databaseName = "databasename"
$adminLogin = '[email protected]'
$PWord = "password"
$query = "CREATE USER [[email protected]] FROM EXTERNAL PROVIDER;";
Invoke-Sqlcmd -ServerInstance $serverName -Database $databaseName -U $adminLogin -Password $PWord -Query $query
I have tried this script as well:
Although below script works in Windows PowerShell, but does not work in Azure functions PowerShell.
$Creds = Get-Credential -Credential '[email protected]'
$Username = $($Creds.GetNetworkCredential().UserName)
$Password = $($Creds.GetNetworkCredential().Password)
$Database = "testg"
$Server = 'test.database.windows.net'
$Port = 1433
$cxnString = "Server=tcp:$Server,$Port;Database=$Database;Authentication=Active Directory Password;UID=$UserName;PWD=$Password;Trusted_Connection=False;Encrypt=True;Connection Timeout=30;"
$query = "CREATE USER YourUser WITH PASSWORD = 'JRxuerCc(q'"
$cxn = New-Object System.Data.SqlClient.SqlConnection($cxnString)
$cxn.Open()
$cmd = New-Object System.Data.SqlClient.SqlCommand($query, $cxn)
$cmd.CommandTimeout = 120
$cmd.ExecuteNonQuery()
$cxn.Close()
But this does not work in Azure functions. It say keyword not supported: 'authentication'
Similar way I tried with .NET approach as well. But it gives the same error
How can I achieve this?