Functions/Export-DatabricksClusterLibrariesByNameAsJson.ps1

<#
.SYNOPSIS
Exports DataBricks Clusters Libraries and Saves as json.
 
.DESCRIPTION
Exports Databricks Clusters Libraries and saves as json.
 
.PARAMETER BearerToken
Your Databricks Bearer token to authenticate to your workspace (see User Settings in Datatbricks WebUI)
 
.PARAMETER Region
Azure Region - must match the URL of your Databricks workspace, example northeurope
 
.PARAMETER ClusterName
The name of the cluster to export as a json file.
 
.PARAMETER LocalOutputPath
Local directroy to save json files.
 
.EXAMPLE
 
.NOTES
Author: Sabin IO
 
#>
 
Function Export-DatabricksClusterLibrariesByNameAsJson {  
    [cmdletbinding()]
    Param(
        [parameter(Mandatory = $true)][string]$ClusterName,
        [parameter(Mandatory = $true)][string]$LocalOutputPath
    )
    $invalidChars = [System.IO.Path]::GetInvalidFileNameChars() -join ''
    $re = "[{0}]" -f [regex]::Escape($invalidChars)

    $cluster = Get-DatabricksClusterSingletonByName  -ClusterName $ClusterName
    
    $clusterLibraryFileName =  ($cluster.cluster_name -replace $re)+ '.cluster.libraries.config.json'

    
    $clusterLibraries = Get-DatabricksLibraries -ClusterId $cluster.cluster_id
    # $clusterLibraries | Add-Member -NotePropertyName 'cluster_name' -NotePropertyValue $cluster.cluster_name
    # $clusterLibraries | Add-Member -NotePropertyName library_statuses -NotePropertyValue $null
    # Remove meta data from libraries as these change all the time and return what the api expects.
    # IE state
    $allLibs = @()
    $clusterLibraries | Foreach-Object {
        $allLibs += @{library = $_.library; status = $_.status;is_library_for_all_clusters = $_.is_library_for_all_clusters}}

    $outputLibraries = New-Object psobject -Property @{
        cluster_name = $cluster.cluster_name
        library_statuses = $allLibs
    }

    $tmp = $outputLibraries | Remove-ClusterLibrariesMetaDataAsPSObject
    $clusterAsJson = $tmp | ConvertTo-Json -Depth 100
    
    $LocalExportPath = Join-Path ( -join ($LocalOutputPath, 'libraries')) $clusterLibraryFileName
    Write-Verbose "Exporting cluster libraries to $LocalExportPath"
    New-Item -Force -Path $LocalExportPath -Value $clusterAsJson -Type file | Out-Null 
}