Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/azure/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Azure Powershell Runbook将AzureAD用户作为db_所有者添加到Azure SQL数据库_Azure_Powershell_Azure Active Directory_Azure Sql Database_Azure Runbook - Fatal编程技术网

使用Azure Powershell Runbook将AzureAD用户作为db_所有者添加到Azure SQL数据库

使用Azure Powershell Runbook将AzureAD用户作为db_所有者添加到Azure SQL数据库,azure,powershell,azure-active-directory,azure-sql-database,azure-runbook,Azure,Powershell,Azure Active Directory,Azure Sql Database,Azure Runbook,我有一个Powershell Runbook,试图将AzureAD用户添加为Azure SQL数据库的数据库所有者 ## Connect $servicePrincipalConnection = Get-AutomationConnection -Name "AzureRunAsConnection" Connect-AzureAD ` -TenantId $servicePrincipalConnection.TenantId ` -ApplicationId $servic

我有一个Powershell Runbook,试图将AzureAD用户添加为Azure SQL数据库的数据库所有者

## Connect
$servicePrincipalConnection = Get-AutomationConnection -Name "AzureRunAsConnection"
Connect-AzureAD `
    -TenantId $servicePrincipalConnection.TenantId `
    -ApplicationId $servicePrincipalConnection.ApplicationId `
    -CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint 

## Generate new access token
$cert = Get-AutomationCertificate -Name 'AzureRunAsCertificate'
# Set Resource URI to Azure Database
$resourceAppIdURI = 'https://database.windows.net/'
# Set Authority to Azure AD Tenant
$authority = 'https://login.windows.net/' + $servicePrincipalConnection.TenantId
$ClientCred = [Microsoft.IdentityModel.Clients.ActiveDirectory.ClientAssertionCertificate]::new($servicePrincipalConnection.ApplicationId, $cert)
$authContext = [Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext]::new($authority)
$authResult = $authContext.AcquireTokenAsync($resourceAppIdURI, $ClientCred)
$AccessToken = $authResult.Result.AccessToken

## Execute sql
$AccessToken
$connectionString = "Data Source=MYCOOLSQLSERVER.database.windows.net;Initial Catalog=MYCOOLDATABASE;Connect Timeout=30"
$connection = New-Object -TypeName System.Data.SqlClient.SqlConnection($connectionString)
$query = "Create User [abc@xyz.com] From EXTERNAL PROVIDER;"

$command = New-Object -TypeName System.Data.SqlClient.SqlCommand($query, $connection)
$connection.AccessToken = $AccessToken
$connection.Open()
$command.ExecuteNonQuery()
$connection.Close()
我最终得到下面的错误,其中
abc@xyz.com
是AzureAD用户

校长的abc@xyz.com"无法解决

有什么我错过的吗

一旦用户被创建,我打算使用
alterrole
使他成为
db\u所有者

参考资料:


对于这种情况,我们应该使用用户密码流使该命令工作。请尝试下面的PS:

$tenant='<your tenant name/id>'
$username='<SQL admin account>'
$password='<SQL admin password>'
$appid='<app id>'
$appsec='<app secret>'

$SQLServerName = '<azure sql servername>'
$DatabaseName='<db name>'

$body=@{
    "grant_type"="password";
    "resource"="https://database.windows.net/";
    "client_id"=$appid;
    "client_secret"=$appsec;
    "username"=$username;
    "password" = $password;
}
 
$result=Invoke-RestMethod -Uri "https://login.windows.net/$tenant/oauth2/token" -Method POST -Body $body 



$conn = New-Object System.Data.SqlClient.SQLConnection 
$conn.ConnectionString = "Data Source=$SQLServerName.database.windows.net;Initial Catalog=$DatabaseName;Connect Timeout=30"
$conn.AccessToken = $result.access_token

Write-Verbose "Connect to database and execute SQL script"
$conn.Open() 

$query = 'CREATE USER [Account] FROM EXTERNAL PROVIDER;'

$command = New-Object -TypeName System.Data.SqlClient.SqlCommand($query, $conn)     
$Result = $command.ExecuteScalar()
$Result
$conn.Close() 
$tenant=''
$username=''
$password=''
$appid='0
添加以下内容后授予权限:

顺便说一句,请确保SQL管理员帐户和SPN都是Active Directory管理员组的成员:

它在我这边起作用,并且已成功添加记录:


如果有任何不清楚的地方,请随时告诉我:)

事实证明,有一种未记录的方法可以做到这一点。在Azure支持团队的帮助下发现了它

要使用的SQL查询实际上是:

创建用户[abc@xyz.com]SID=$hexSid时,键入E

这里可以通过以下SQL查询获得
$hexSid

  • 获取AzureAD用户的ObjectId
  • $objectId=(获取AzureADUser-objectId)abc@xyz.com“”。ObjectId

  • 使用下面的SQL获取
    sid
  • 将@sid=uniqueidentifier=cast(“$objectId”声明为 uniqueidentifier)选择cast(@sid作为varbinary(max))

  • 现在,理想情况下,有效查询将直接使用
    @sid
    ,但这是不可能的,因为带有sid的
    需要硬编码值。因此,我必须单独处理sid查询的输出,如下所示:
  • 有了这些更改,问题中的代码就可以正常工作了


    实际上,我们只在Create User查询中传递AzureAD用户的objectId。

    您能告诉我是否可以运行命令“Create User”吗[abc@xyz.com]来自外部提供商;“在SSMS中?是的,它通过SSMS工作。但是在SSMS中,我使用我自己的AzureAD用户进行连接,而上面的脚本使用自动化帐户的SPN。实际上,我正在尝试直接向我们提供SPN,文档表明这是可能的。我不允许在客户端租户中更新API定义下的权限。我明白了,恐怕您应该使用user+app auth方式来完成此操作。。。在此场景中需要用户\模拟权限。我理解方法,并且在得出相同结论时对答案进行投票。但是,我无法生成十六进制值。你能再详细一点吗?什么样的麻烦?您只需执行SQL即可首先获取hexSid,然后将其转换为十六进制格式。
    $result = $command.ExecuteScalar() # result is byte array
    $hexSid = ($result | ForEach-Object ToString X2) -Join ''  # convert to hex format
    $hexSid = "0x$hexSid"