Functions/Remove-WorkspaceNotebooks.ps1

Function Remove-WorkspaceNotebooks {
    [cmdletbinding()]
    
    param($FolderContents, $Path, $localPath, $Format = "SOURCE", $results)
    $myArray = @()
    if ($PSBoundParameters.ContainsKey('results') -eq $false) {
        $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"
            try {
                If ((Test-Path $LocalExportFolderPath) -eq $true) {
                    $subFolderContents = Get-DatabricksWorkspaceFolder -Path $Object.path 
                    Remove-WorkspaceNotebooks -FolderContents $subFolderContents -Path $Path -localPath $localPath -results $results | Out-Null
                }
                else {
                    $myArray += $currentLocation
                    Write-Host "Folder $($currentLocation) doesn't exist locally, removing..."
                    Remove-DatabricksNotebook -Path $FolderName -Recursive
                    $results.RemovedFolders ++
                }
            }
            catch {
                Write-Host "Ran into an issue: "
                Write-Host "Object Path: $($Object.path)"
                Write-Host "Folder Name: $FolderName"
                Write-Host "Current location $currentLocation"
                Write-Host "Local export path: $LocalExportFolderPath"
                $PSItem
                $Object
                Throw

            }
        }
        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 ++
            }
        }
    }
    return $results
}