Functions/Remove-WorkspaceNotebooks.ps1

Function Remove-WorkspaceNotebooks {
    [cmdletbinding()]
    
    param($FolderContents, $Path, $localPath, $Format = "SOURCE")
    $myArray = @()
    ForEach ($Object In $FolderContents) {
        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 = Get-DatabricksWorkspaceFolder -Path $Object.path 
                Remove-WorkspaceNotebooks -FolderContents $subFolderContents -localPath $localPath -Path ($Object.path + "/") 
            }
            else {
                $myArray += $currentLocation
                Write-Host "Folder $($currentLocation) doesn't exist locally, removing..."
                Remove-DatabricksNotebook -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 -Path $Object.Path
            }
        }
    }
}