Public/Set-FatAdlsAccess.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
Function Set-FatAdlsAccess {
    [CmdletBinding()]
    param(
        [parameter(Mandatory = $true)] [string]$subscriptionName,
        [parameter(Mandatory = $true)] [Alias("RGName")] [string]$resourceGroupName,
        [parameter(Mandatory = $true)] [string]$dataLakeStoreName,
        [parameter(Mandatory = $true)] $aclFolders,
        [parameter(Mandatory = $true)][ValidateSet('Acl', 'Permission')][string]$entryType,
        [switch]$UseConnectedAccount,
        [switch]$WhatIf
    )
    if (($PSBoundParameters.ContainsKey('UseConnectedAccount')) -eq $False) {
        $storageAccount = Get-AzStorageAccount -ResourceGroupName $resourceGroupName -AccountName $dataLakeStoreName
        $ctx = $storageAccount.Context
    }
    else{
        $ctx = New-AzStorageContext -StorageAccountName $dataLakeStoreName -UseConnectedAccount
    }

    if ($null -eq $ctx) {
        Write-Error "no context."
    }
    Write-Verbose "Azure DataLake Store Name: $dataLakeStoreName"
    $ErrorActionPreference = "Stop"

    foreach ($folder in $aclFolders) {
        Write-Verbose "[*] Checking if $($folder.Folder) exists in container $($folder.Container)..."

        if (-Not (Get-AzDataLakeGen2Item -context $ctx -FileSystem $folder.Container -Path $folder.Folder -ErrorAction "SilentlyContinue")) {
            Write-Verbose "[*] Creating folder $($folder.Folder) in $dataLakeStoreName..."
            $Params = @{
                context    = $ctx;
                FileSystem = $folder.Container
                Path       = $folder.Folder
                Directory  = $True

            }
            New-AzDataLakeGen2Item @Params | out-null
        }
        $FatAdlsAclEntryOnItem = @{
            ctx               = $ctx;
            subscriptionName  = $subscriptionName;
            dataLakeStoreName = $dataLakeStoreName;
            aclEntry          = $folder;
        }
        if (($PSBoundParameters.ContainsKey('WhatIf')) -eq $True) {
            Write-Host "Running WhatIf"
            $FatAdlsAclEntryOnItem.Add('WhatIf', $True)
        }
        if (($PSBoundParameters.ContainsKey('removeAcls')) -eq $True) {
            Write-Host "Removing ACL's"
            $FatAdlsAclEntryOnItem.Add('removeAcls', $True)
        }
        Set-FatAdlsAclEntryOnItem @FatAdlsAclEntryOnItem
    }
}