Functions/Remove-WorkspaceNotebooks.ps1

Function Remove-WorkspaceNotebooks {
    [cmdletbinding()]
    
    param($FolderContents, $Path, $localPath, $Format = "SOURCE", $results)
    $myArray = @()
    if ($null -eq $results) {
        $results = @{
            RemovedFolders   = 0
            RemovedNotebooks = 0
        }
    }
    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($Path, "")
            $LocalExportFolderPath = Join-Path $LocalPath $FolderName
            Write-Verbose "Object Path: $($Object.path)"
            Write-Verbose "Folder Name: $FolderName"
            Write-Verbose "Current location $currentLocation"
            Write-Verbose "Local export path: $LocalExportFolderPath"
            If ((Test-Path $LocalExportFolderPath) -eq $true) {
                $subFolderContents = Get-DatabricksWorkspaceFolder -Path $Object.path 
                $results = Remove-WorkspaceNotebooks -FolderContents $subFolderContents -Path $Path -localPath $localPath -results $results
            }
            else {
                $myArray += $currentLocation
                Write-Host "Folder $($currentLocation) doesn't exist locally, removing..."
                Remove-DatabricksNotebook -Path $FolderName -Recursive
                $results.RemovedFolders = $results.RemovedFolders + 1 
            }
        }
        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($Path + "/", "") + $FileExt
            $LocalExportPath = Join-Path $LocalPath $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
                $results.RemovedNotebooks = $results.RemovedNotebooks + 1 
            }
        }
    }
    return $results
}