使用参数化脚本授予对 Azure Data Lake Gen2 的访问权限

Grant access to Azure Data Lake Gen2 using a parameterized script

我们正在尝试授予 read/write 访问我们 Azure 数据湖第 2 代容器中的许多文件夹的权限,尽管我们可以通过 UI 执行此操作,但它非常乏味并且必须重复所有环境。有没有人使用更好的方法使用 Powershell 来自动化或至少参数化授予对 Azure Data Lake gen 2 容器的访问权限的过程,并避免手动授予访问权限?

不幸的是,我无法使用以下 link 或其他文档使其正常工作,因为它适用于第 1 代,但它与我需要为第 2 代做的非常相似。 https://www.sqlchick.com/entries/2018/3/17/assigning-data-permissions-for-azure-data-lake-store-part-3

根据我的测试,我们可以使用PowerShell来管理Azure Data Lake Gen2权限。详情请参考document

  1. 安装所需的模块
install-Module PowerShellGet –Repository PSGallery –Force
install-Module Az.Storage -Repository PSGallery -RequiredVersion 1.9.1-preview –AllowPrerelease –AllowClobber –Force

Besides, please note that if you want to install the module, you need to meet some conditions

  • .NET Framework is 4.7.2 or greater installed
  • PowerShell is 5.1 or higher
  1. 脚本
Connect-AzAccount

$groupName=""
$accountName=""
$account= Get-AzStorageAccount -ResourceGroupName $groupName -Name $accountName
$ctx = $account.Context

$filesystemName = "test"
$dirname="template/"
$Id = "<the Object ID of user, group or service principal>"
$dir=Get-AzDataLakeGen2Item -Context $ctx -FileSystem $filesystemName -Path $dirname
$acl = New-AzDataLakeGen2ItemAclObject -AccessControlType user -EntityId $id -Permission "rw-" -InputObject $dir.ACL
Update-AzDataLakeGen2Item -Context $ctx -FileSystem $filesystemName -Path $dirname -Acl $acl
$dir=Get-AzDataLakeGen2Item -Context $ctx -FileSystem $filesystemName -Path $dirname
$dir.ACL

感谢Jim Xu 提供以上脚本。我只是用以下项目补充代码:

  • 从容器中获取所有文件夹
  • 为所有文件夹分配 ACL
  • 将 ACL 传播到所有子文件夹
$groupName="resource group name"
$accountName="storage account name"
$account= Get-AzStorageAccount -ResourceGroupName $groupName -Name $accountName
$ctx = $account.Context

$filesystemName = "container name"
$Id = (Get-AzADGroup -DisplayName '<type user / group name here>').Id
$items = Get-AzDataLakeGen2ChildItem -Context $ctx -FileSystem $filesystemName

foreach ( $item in $items) {
    
    $dir = Get-AzDataLakeGen2Item -Context $ctx -FileSystem $filesystemName -Path "$($item.Path)/"
    $acl = New-AzDataLakeGen2ItemAclObject -AccessControlType group -EntityId $id -Permission "rwx" -InputObject $dir.ACL -DefaultScope
     
    # Update ACL on blob item
    Update-AzDataLakeGen2Item -Context $ctx -FileSystem $filesystemName -Path "$($item.Path)/" -Acl $acl
    
    # Propagate ACL to child blob items
    Set-AzDataLakeGen2AclRecursive -Context $ctx -FileSystem $filesystemName -Path "$($item.Path)/" -Acl $acl
}