Functions/Export-DatabricksJobsFromWorkspace.ps1

<#
.SYNOPSIS
Exports DataBricks Jobs and Saves as json.
 
.DESCRIPTION
Exports Databricks Jobs and saves as json.
 
.PARAMETER config
Configuration json file
 
.PARAMETER localOutputPath
The folder path of the jobs to export as a json file(s).
 
 
.EXAMPLE
Export-DatabricksJobsFromWorkspace -config $config-LocalOutputPath '.\output'
 
.NOTES
Author: Sabin IO
 
#>
 
Function Export-DatabricksJobsFromWorkspace {
    [cmdletbinding()]
    Param(
        [parameter(Mandatory = $true)]$config,  
        [parameter(Mandatory = $true)][string]$localOutputPath
    )
    
    try {
        $jobs = Get-DatabricksJobs 
        $noneExistantJobs = @()
        $existentJobs = @()

        if (($config.PSObject.Properties.Item('getJobsByName') -and ($config.PSObject.Properties.Item('getJobsLikeByName')))) {
            Write-Error "getJobsByName and getJobsLikeByName have been supplied. Please only use one or the other!"
            Throw
        }
    
        if ($config.getJobsByName) {
            Write-Output "getJobsByName config key supplied"
            if ($config.getJobsByName.Length -ge 1) {
                if ($config.getJobsByName[0].ToString().ToUpper() -eq "ALL") {
                    Write-Output "All parameter supplied. Pulling all cluster configurations."
                    foreach ($job in $jobs) {
                        if ($job.settings.name -ne "AzDO Execution" -and $job.settings.name -ne "Untitled") {
                            Export-DatabricksJobConfigAsJson -LocalOutputPath $localOutputPath -JobConfig $job.settings -Verbose 
                        }
                    }                            
                }
                else {
                    # Check if jobs exist in the first place
                    foreach ($jobName in $config.getJobsByName) {
                        if ($jobName -notin $jobs.settings.name) {
                            $noneExistantJobs += $jobName
                        }          
                        else {
                            $existentJobs += $jobName
                        }           
                    }
                    if ($noneExistantJobs) {
                        Write-Warning "There are jobs that do not exist to get, These will be skipped. `n`n $($noneExistantJobs)"
                    }
                    foreach ($job in $jobs) {
                        if ($job.settings.name -in $existentJobs) {

                            Export-DatabricksJobConfigAsJson `
                                -LocalOutputPath $localOutputPath `
                                -JobConfig $job.settings `
                                -Verbose 
                                
                        }
                    }                
                }           
            } 
        }     
        else {
            Write-Output "getJobsByName config key not supplied"
        }

        if ($config.getJobsLikeByName) {
            Write-Output "getJobsLikeByName config key supplied"
            if ($config.getJobsLikeByName.Length -ge 1) {
                foreach ($job in $jobs) {
                    foreach ($jobLike in $config.getJobsLikeByName) {
                        if ($job.settings.name -like "$($jobLike)") {

                            Export-DatabricksJobConfigAsJson `
                                -LocalOutputPath $localOutputPath `
                                -JobConfig $job.settings `
                                -Verbose 

                        }
                    }
                }
            } 
        }     
        else {
            Write-Output "getJobsLikeByName config key not supplied"
        }
        
    }    
    catch {
        #uh oh
        throw $_.Exception
    }
}