Functions/Remove-DatabricksJobFromWorkspaceByName.ps1

<#
.SYNOPSIS
Removes DataBricks job(s) from environment configuration
 
.DESCRIPTION
Removes DataBricks job(s) from environment configuration.
 
.PARAMETER config
Configuration json file from the environment.
 
.PARAMETER bearerToken
Your Databricks Bearer token to authenticate to your workspace (see User Settings in Datatbricks WebUI)
 
.PARAMETER jobsConfig
The job name(s) you want to remove
The format has to be specified here: https://docs.microsoft.com/en-us/azure/databricks/dev-tools/api/latest/jobs#--delete
 
.EXAMPLE
Remove-DatabricksJobFromWorkspaceByName -config $config -bearerToken 'dapi1234567890' -jobsConfig '<PSObject>'
 
.NOTES
Author: Sabin IO
 
#>
 
Function Remove-DatabricksJobFromWorkspaceByName {
    [cmdletbinding()]
    Param(
        [parameter(Mandatory = $true)]$config,
        [parameter(Mandatory = $true)][string]$bearerToken,
        [parameter(Mandatory = $true)][string]$jobName,
        $databricksURI
    )
    try {

        Connect-Databricks -BearerToken $bearerToken -Region $config.region
        $JobId = (Get-DatabricksJobs -BearerToken $bearerToken -Region $config.Region | Where-Object { $_.settings.name -eq $jobName }).job_id

        if ($JobId) {

            Write-Output "Job found with this name `"$($jobName)`" - Job Id [$($JobId)]..."
   
            [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
            $Headers = GetHeaders $PSBoundParameters
        
            $uri = "$databricksURI/api/2.0/jobs/delete"

            $Body = @{}
            $Body['job_id'] = $JobId
        
            $BodyText = $Body | ConvertTo-Json -Depth 100
    
            Write-Output "Request Body: $($BodyText)"
            Invoke-RestMethod -Uri $uri -Body $BodyText -Method 'POST' -Headers $Headers
        }
        else {
            Write-Warning "No job found with name `"$($job.name)`" - skipping delete..."
        }    
    }    
    catch {
        #uh oh
        throw $_.Exception
    }
}