Functions/Remove-WorkspaceNotebooks.ps1
Function Remove-WorkspaceNotebooks { [cmdletbinding()] param($FolderContents, $Path, $localPath, $Format = "SOURCE", $results, [array]$IgnoreFolderArray) if ($IgnoreFolderArray -notcontains "__pycache__") { $IgnoreFolderArray += "__pycache__" } $myArray = @() if ($PSBoundParameters.ContainsKey('results') -eq $false) { $results = @{ RemovedFolders = 0 RemovedNotebooks = 0 SkippedFolders = 0 SkippedNotebooks = 0 } } ForEach ($Object In $FolderContents) { if ($Object.object_type -eq "DIRECTORY") { $PathName = ($Object.path).Replace($Path, "") if (($PathName.startswith("/")) -eq $false) { $PathName = "/" + $PathName } $currentLocation = ($Object.path).Replace($Path, "") $LocalExportFolderPath = Join-Path $LocalPath $PathName $FolderName = $PathName.split('/')[-1] Write-Verbose "Object Path: $($Object.path)" Write-Verbose "Folder Name: $FolderName" Write-Verbose "Path Name: $PathName" Write-Verbose "Current location $currentLocation" Write-Verbose "Local export path: $LocalExportFolderPath" if ($IgnoreFolderArray -notcontains $FolderName) { 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 $PathName -Recursive $results.RemovedFolders ++ } } catch { Write-Host "Ran into an issue: " Write-Host "Object Path: $($Object.path)" Write-Verbose "Folder Name: $FolderName" Write-Host "Path Name: $PathName" Write-Host "Current location $currentLocation" Write-Host "Local export path: $LocalExportFolderPath" $PSItem $Object Throw } } else { Write-Verbose "$FolderName is included in `$IgnoreFolderArray param, skipping." $results.SkippedFolders ++ } } elseif ($Object.object_type -eq "NOTEBOOK") { $Skip = 0 $Notebook = $Object.path $FolderNames = $Object.path.Split("/") $IgnoreFolderArray | ForEach-Object { if ($FolderNames -contains $_) { Write-Verbose "`Directory path of notebook contains the `$IgnoreFolderArray folder [$_]; as this notebook exists under this folder it will not be removed." $Skip = 1 $results.SkippedNotebooks ++ } } if ($Skip -ne 1) { $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 } |