Functions/Remove-LocalNotebooks.ps1

Function Remove-LocalNotebooks {
    [cmdletbinding()]
    param(
        [parameter(Mandatory = $true)][string]$localPath,
        $config
    )
    $results = @{
        RemovedFolders   = 0
        RemovedNotebooks = 0
    }
    $folderContent = Get-ChildItem $localPath -Directory -Recurse 
    foreach ($object in $folderContent) {
        $WorkspaceFolder = $object.FullName.Replace($localPath, '')
        $WorkspaceFolder = Join-Path $config.dataBricksPath $WorkspaceFolder
        $workspaceFolder = $WorkspaceFolder.Replace('\', '/')
        Write-Verbose "**DIRECTORY**"
        Write-Host "Checking for $WorkspaceFolder and its contents exist on the workspace..."
        Write-Verbose $object.FullName
        Try {
            Get-DatabricksWorkspaceFolder -Path $WorkspaceFolder | Out-Null
        }
        Catch {     
            if ($_.Exception.Response.StatusCode -eq "Forbidden") {
                Write-Error $_.Exception.Response
                Throw
            }
            elseif ($_.Exception.Response.StatusCode -eq "NotFound") {
                Write-Verbose "Folder $WorkspaceFolder not found in workspace. Deleting locally."
                Remove-Item $object.FullName -Force -Recurse
                $results.RemovedFolders = $results.RemovedFolders + 1
            }
        }
    }

    $folderContent = Get-ChildItem $localPath -File -Recurse -Include *.py
    foreach ($object in $folderContent) {
        $notebook = $object.FullName.Replace($localPath, '')
        $notebook = Join-Path $config.dataBricksPath $notebook
        $notebook = $notebook.Replace('\', '/')
        $notebook = $notebook.Replace('.py', '')
        Write-Verbose "**NOTEBOOK**"
        Write-Verbose $notebook
        Write-Verbose $object.FullName
        Try {
            Write-Verbose "Running Get-DatabricksWorkspaceFolder"
            Get-DatabricksWorkspaceFolder -Path $notebook | Out-Null
        }
        Catch {
            if ($_.Exception.Response.StatusCode -eq "Forbidden") {
                Write-Error $_.Exception.Response
                Throw
            }
            elseif ($_.Exception.Response.StatusCode -eq "NotFound") {
                Write-Verbose "File $notebook not found! Removing from local directory."
                Remove-Item $object.FullName -Force   
                $results.RemovedNotebooks = $results.RemovedNotebooks + 1
            }
            else{
                Write-Host $_.Exception.Response
                Throw
            }
        }
    }
    return $results
}