Functions/Remove-WorkspaceNotebooks.ps1

Function Remove-WorkspaceNotebooks {
    [cmdletbinding()]
    
    param($FolderContents, $Path, $Region, $bearerToken, $localPath, $Format = "SOURCE")
    $myArray = @()
    $FolderContent = $FolderContents.objects

    ForEach ($Object In $FolderContent) {
        if ($Object.object_type -eq "DIRECTORY") {
            $FolderName = ($Object.path).Replace($Path, "")
            if (($FolderName.startswith("/")) -eq $false) {
                $FolderName = "/" + $FolderName
            }
            $currentLocation = ($Object.path).Replace($config.dataBricksPath, "")
            Write-Verbose "Object Path: $($Object.path)"
            Write-Verbose "Folder Name: $FolderName"
            Write-Verbose "Current location $currentLocation"
            Write-Verbose "Local export path: $LocalExportFolderPath"
            $LocalExportFolderPath = $currentLocation.Replace($config.dataBricksPath + "/", "")
            $LocalExportFolderPath = Join-Path $LocalOutputPath $LocalExportFolderPath
            If ((Test-Path $LocalExportFolderPath) -eq $true) {
                $subFolderContents = . .\adb.cicd.tools\GetFolderContents.ps1 -region $region -Path $Object.path -bearerToken $bearerToken
                . .\adb.cicd.tools\deleteWorkspaceNotebook.ps1 -FolderContents $subFolderContents -localPath $localPath -region $region -Path ($Object.path + "/") -bearerToken $bearerToken
            }
            else {
                $myArray += $currentLocation
                Write-Host "Folder $($currentLocation) doesn't exist locally, removing..."
                Remove-DatabricksNotebook -BearerToken $BearerToken -Region $Region -Path $FolderName -Recursive
            }
        }
        elseif ($Object.object_type -eq "NOTEBOOK") {
            $Notebook = $Object.path
            $NotebookLanguage = $Object.language
            switch ($format) {
                "SOURCE" {
                    $FileExtentions = @{"PYTHON" = ".py"; "SCALA" = ".scala"; "SQL" = ".sql"; "R" = ".r" }
                    $FileExt = $FileExtentions[$notebookLanguage]
                }
                "HTML" {
                    $FileExt = ".html"
                }
                "JUPYTER" {
                    $FileExt = ".ipynb"
                }
                "DBC" {
                    $FileExt = ".dbc"
                }
            }
            $LocalExportPath = $Notebook.Replace($config.dataBricksPath + "/", "") + $FileExt
            $LocalExportPath = Join-Path $LocalOutputPath $LocalExportPath
            Write-Verbose "Object Path: $($Object.path)"
            Write-Verbose "Local export path: $LocalExportPath"
            If ((Test-Path $LocalExportPath) -ne $true) {
                $myArray += $object.Path
                Write-Host "File $($object.Path) doesn't exist locally, removing..."
                Remove-DatabricksNotebook -BearerToken $BearerToken -Region $Region -Path $Object.Path
            }
        }
    }
}