我有一个Powershell脚本,用于用Azure AD用户查询Azure SQL数据库。我可以在本地计算机上运行poweshell脚本。但是,当我将Powershell脚本托管在Azure函数上时,总是出现错误:不支持的关键字:“身份验证”
我的剧本
$Username = “”
$Password = “”
$Port = 1433
$cxnString = "Server=tcp:$serverName,$Port;Database=$databaseName;UID=$UserName;PWD=$Password;Trusted_Connection=False;Encrypt=True;Connection Timeout=30;Authentication=Active Directory Password"
$query = ''
Invoke-Sqlcmd -ConnectionString $cxnString - Query $query
根据我的测试,现在我们无法在Azure Function中使用PowerShell实施它。现在,我们可以使用.Net Core Function来实现它。我们需要包装Microsoft.Data.SqlClient
例如
SqlConnectionStringBuilder builder = new SqlConnectionStringBuilder();
string userName = "<>";
string password = "<>";
string server = "<>.database.windows.net";
string database = "test";
builder.ConnectionString = "Server=tcp:" + server + ",1433;Initial Catalog=" + database + ";Authentication=Active Directory Password;User ID=" + userName + ";Password=" + password + ";MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;";
using (SqlConnection connection = new SqlConnection(builder.ConnectionString))
{
connection.Open();
string sql = "SELECT * FROM StarWars";
using (SqlCommand command = new SqlCommand(sql, connection))
{
using (SqlDataReader reader = command.ExecuteReader())
{
while (reader.Read())
{
log.LogInformation(reader.GetString(2));
}
}
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句