I am trying to connect to SQL Azure via powershell connection strings. I have no issue connecting using a local SQL Server account but when I try to use Azure Active directory I get a login failed or cannot connect to server. I have setup the AAD user as
a contained database user and am able to connect to the Azure database via SSMS using all forms of authentication. MFA, with password and Integrated. Here is the code I use
function Get-SqLOLEDBData ($SQLconnString, $SQLqry) {
$SQLconnString
$OLEDBConn = New-Object System.Data.OleDb.OleDbConnection($SQLconnString)
$OLEDBConn.open()
$readcmd = New-Object system.Data.OleDb.OleDbCommand($SQLqry,$OLEDBConn)
$readcmd.CommandTimeout = "300"
$da = New-Object system.Data.OleDb.OleDbDataAdapter($readcmd)
$dt = New-Object system.Data.datatable
[void]$da.fill($dt)
$OLEDBConn.close()
return $dt
}
$SQLqry = 'select * from dbo.mytable'
$SQLconnString = 'Data Source=myserver.database.windows.net;Initial Catalog=mydatabase;Authentication=ActiveDirectoryPassword;User Id=myuser@mydomain.com;Password=mypassword;Encrypt=True;TrustServerCertificate=True;Connection Timeout=30;Provider=SQLNCLI11'
$DB_Results = Get-SqLOLEDBData "$SQLconnString" $SQLqry
$DB_Results
if i try connect this way, the @ symbol in the username thinks its trying to connect the user to mydomain.com which clearly doesn't exist.
If I change the User ID = svcAzSQLfacuat@mydomain.com@myserver or svcAzSQLfacuat@mydomain.com@myserver.database.windows.net
I get a Login failed for user svcAzSQLfacuat@mydomain.com but like I said I can connect via ssms with the same username and password i am passing in the connection string.
Any help is greatly appreciated.
Meachel Carnahan BSc. Computer Science Database Administrator