【问题标题】:Assigning Purview roles to Service Principal through Powershell script通过 Powershell 脚本将权限角色分配给服务主体
【发布时间】:2022-01-01 08:54:33
【问题描述】:

我正在尝试向我的权限帐户添加一个集合,为此我需要我的服务原则在我的权限帐户中具有“数据源管理员”角色。我已经使用这个 Powershell 脚本部署了我的权限帐户:

function deployPurviewAccount([string]$resourceGroup, [string]$location, [string]$purviewName){
New-AzResourceGroup -Name $resourceGroup -Location $location

New-AzPurviewAccount -Name $purviewName `
-ResourceGroupName $resourceGroup `
-Location $location `
-IdentityType SystemAssigned `
-SkuCapacity 1 `
-SkuName Standard `
}
$location = getLocation
$resourceGroup = getResourceGroupName
$purviewName = getPurviewAccountName
"Deploying Purview Account ${purviewName} in Resource Group ${resourceGroup}"
deployPurviewAccount $resourceGroup $location $purviewName

getLocationgetResourceGroupNamegetPurviewAccountName 是基本输入函数,我只是在询问名称。例如,centralindia 表示位置等。

部署成功,现在我想向我正在使用脚本的权限帐户添加一个新集合:

<#-CREATES A SERVICE PRINCIPAL-#>
function createSP([string]$subscriptionId, [string]$resourceGroup, [string]$spName) {
$scope = "/subscriptions/${subscriptionId}/resourceGroups/${resourceGroup}"
$sp = New-AzADServicePrincipal -DisplayName "${spName}" -Role "Owner" -Scope $scope
return $sp
}
<#-Generates an Access Token#->
function genAccessTok([string]$tenantId, [string]$clientid, [string]$UnsecureSecret) {
$purres = "https://purview.azure.net"
$reqUrl = "https://login.microsoftonline.com/${tenantId}/oauth2/token"
$postmanBody = "grant_type=client_credentials&client_id=${clientId}&client_secret=${UnsecureSecret}&resource=${purres}"
$token = Invoke-RestMethod -Method Post -Uri $reqUrl -Body $postmanBody -ContentType 'application/x-www-form-urlencoded'
$tmpAcsToken = $token.access_token
return "Bearer " + ($tmpAcsToken).ToString()
}

function createSubCollection([string]$accessToken, [string]$endpoint, [string]$collectionName, [string]$parentCollectionName) {
$reqPath = "${endpoint}/account/collections/${purviewName}?api-version=2019-11-01-preview"
$postBody = @{
    "name" = $purviewName
    "parentCollection"= @{
        "type" = "CollectionReference"
        "referenceName" = $parentCollectionName
    }
    "friendlyName" = $collectionName
}
$parameter = @{
    ContentType = "application/json"
    Headers = @{"Authorization"=$accessToken}
    Body = {$postBody | ConvertTo-Json -Depth 10}
    Method = "PUT"
    URI = $reqPath
}

Invoke-RestMethod @parameter <#--- Throws error here ---#>
}

$tenantId = (Get-AzContext).Tenant.Id
$subscriptionId = (Get-AzContext).Subscription.Id
$resourceGroup = getResourceGroupName
$purviewName = getPurviewAccountName

$endpoint = "https://${purviewName}.purview.azure.com"
$spName = getServicePrincipleName

$collectionName = getColName
$parentCollectionName = getParentCol
$sp = createSP $subscriptionid $resourceGroup $spName
$BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($sp.Secret)
$UnsecureSecret = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)
$clientid = $sp.ApplicationId

$accessToken = genAccessTok $tenantId $clientid $UnsecureSecret


"Adding Collection to the purview account"

createSubCollection $accessToken $atlas_endpoint $collectionName $parentCollectionName

现在要将集合添加到我的权限帐户,我需要在权限帐户中提供在上述脚本“数据源管理员”角色中创建的服务主体。如何通过我的 Powershell 脚本做到这一点?如果我在不提供角色的情况下运行上述脚本,则会引发以下错误。

【问题讨论】:

    标签: azure powershell azure-active-directory azure-powershell azure-purview


    【解决方案1】:

    截至目前,您只能添加对 root 集合管理员的添加权限可用。您可以使用 Azure CLI (az purview account add-root-collection-admin) 和 PowerShell (Add-AzPurviewAccountRootCollectionAdmin)

    一旦您有权访问权限帐户,您就可以根据 Azure Purview Studio 的要求添加权限,例如(集合管理员、数据源管理员、数据管理者、数据读者)。

    注意:用于添加权限的 PowerShell/cli 命令,例如(集合管理员、数据源管理员、数据管理员、数据读取器)即将推出

    更多详情,请参阅Access control in Azure Purview,您可以查看我之前的回答“azure purview access permission”,其中介绍了如何授予对 Azure Purview 帐户的访问权限。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-14
      • 1970-01-01
      • 2022-11-30
      • 1970-01-01
      • 2014-01-04
      • 1970-01-01
      • 2023-02-14
      • 2019-06-28
      相关资源
      最近更新 更多