Functions/Remove-LocalNotebooks.ps1

Function Remove-LocalNotebooks {
    [cmdletbinding()]

    param(
        [parameter(Mandatory = $true)][string]$localPath,
        $bearerToken,
        $config
    )

    $folderContent = Get-ChildItem $localPath -Directory -Recurse 
    foreach ($object in $folderContent) {
        $WorkspaceFolder = $object.FullName.Replace($localPath, '')
        Write-Host $WorkspaceFolder "wf"
        $WorkspaceFolder = Join-Path $config.dataBricksPath $WorkspaceFolder
        $workspaceFolder = $WorkspaceFolder.Replace('\', '/')
        Write-Host $WorkspaceFolder
        Try {
            $uri = "https://$($config.region).azuredatabricks.net/api/2.0/workspace/list?path=$($WorkspaceFolder)"
            Write-Host "Requesting URI $uri"
            Invoke-RestMethod -Method Get -Uri $uri -Headers @{Authorization = "Bearer $bearerToken" } 
        }
        Catch {
           
            Write-Host "Folder $WorkspaceFolder not found in workspace. Deleting locally." -ForegroundColor DarkGreen -BackgroundColor Yellow
            Remove-Item $object.FullName -Force -Recurse
        }
    }

    $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-Host $notebook
        Try {
            $uri = "https://$($config.region).azuredatabricks.net/api/2.0/workspace/list?path=$($notebook)"
            Write-Host "Requesting URI $uri"
            Invoke-RestMethod -Method Get -Uri $uri -Headers @{Authorization = "Bearer $bearerToken" } 
        }
        Catch {
           
            Write-Host "File $notebook not found! Removing from local directory." -ForegroundColor DarkGreen -BackgroundColor Yellow
            Remove-Item $object.FullName -Force   
        }
    }
}