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 = $false)][string]$BearerToken,    
        [parameter(Mandatory = $false)][string]$Region,
        [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 -BearerToken $BearerToken -Region $Region -ClusterName $ClusterName
    $clusterName = ($cluster.cluster_name -replace $re)
    $clusterLibraryFileName = $clusterName + '.cluster.libraries.config.json'

    
    $clusterLibraries = Get-DatabricksLibraryStatuses -BearerToken $BearerToken -Region $Region -ClusterId $cluster.cluster_id
    $clusterLibraries.PSObject.Properties.Remove('cluster_id')  
    $clusterLibraries | Add-Member -NotePropertyName 'cluster_name' -NotePropertyValue $cluster.cluster_name

    # Remove meta data from libraries as these change all the time and return what the api expects.
    # IE state
    $tmp = $clusterLibraries | 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 
}