Public/Get-FatAdlsAccessIntoCsv.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
Function Get-FatAdlsAccessIntoCsv {
    param(
        [parameter(Mandatory = $true)] [string]$dataLakeStoreName,
        [parameter(Mandatory = $true)] [string]$adlsPath,
        [parameter(Mandatory = $true)] [string]$csvOutputPath,
        [parameter(Mandatory = $true)] [string]$csvFileName,
        [parameter(Mandatory = $false)] [switch]$recurse
    )
    if ($dataLakeStoreName.EndsWith(".azuredatalakestore.net") -eq $false) {
        Write-Verbose "Adding FQDN to value of `$dataLakeStoreName"
        $dataLakeStoreName = $dataLakeStoreName + ".azuredatalakestore.net"
    }
    Write-Verbose "Azure DataLake Store Name: $dataLakeStoreName"
    $ErrorActionPreference = "Continue"

    if ($csvFileName.EndsWith("csv") -eq $false) {
        Write-Verbose "Adding .csv to value of `$csvFileName"
        $csvFileName = $csvFileName + ".csv"
    }
    if ($adlsPath.EndsWith("/") -eq $false) {
        Write-Verbose "Adding terminating / to value of `$adlsPath"
        $adlsPath = $adlsPath + "/"
    }
    if ($adlsPath.StartsWith("/") -eq $false) {
        Write-Verbose "Adding leading / to value of `$adlsPath"
        $adlsPath = "/" + $adlsPath
    }
    try {
        $csvOutputPath = Resolve-Path $csvOutputPath 
    }
    catch {
        Write-Host "Creating $csvOutputPath as it does not exist..."
        New-Item -path $csvOutputPath -ItemType Directory
        $csvOutputPath = Resolve-Path $csvOutputPath
    }
    $csvFullDirectoryPath = (Join-Path -Path $csvOutputPath -ChildPath $csvFileName)
    Write-Verbose "Full directory path set to $csvFullDirectoryPath." 
    $csvStreamWriter = New-Object IO.StreamWriter $csvFullDirectoryPath
    $csvStreamWriter.WriteLine("Folder, ADGroup, ADGroupID, Permission, IncludeInDefault, Recurse")
    if ($PSBoundParameters.ContainsKey('recurse') -eq $true) {
        Get-FatAdlsFolderPermissions -dataLakeStoreName $dataLakeStoreName -path $adlsPath -csvStreamWriter $csvStreamWriter -recurse
    }
    else {
        Get-FatAdlsFolderPermissions -dataLakeStoreName $dataLakeStoreName -path $adlsPath -csvStreamWriter $csvStreamWriter
    }
    if ($csvStreamWriter.BaseStream) {
        $csvStreamWriter.Dispose()
    }
}