Functions/Save-DbSettingsToFile.ps1

<#
.SYNOPSIS
    Saves database deployment settings to a JSON file.
 
.DESCRIPTION
    The Save-DbSettingsToFile function serializes deployment settings to a JSON file for later comparison
    to determine if database redeployment is needed. The function creates the parent directory if it doesn't exist.
 
.PARAMETER settings
    Specifies the settings object containing database deployment configuration. This object will be
    serialized to JSON format.
 
.PARAMETER DBDeploySettingsFile
    Specifies the path where the settings file will be saved. The parent directory will be created
    if it doesn't exist.
 
.OUTPUTS
    None
    This function does not return output but creates a JSON file with the deployment settings.
 
.EXAMPLE
    $settings = @{ ServerName = "localhost"; DatabaseName = "MyDB" }
    Save-DbSettingsToFile -settings $settings -DBDeploySettingsFile "C:\Deploy\settings.json"
     
    Saves the settings object to the specified JSON file.
 
.NOTES
    The parent directory is automatically created if it doesn't exist.
    The file will be overwritten if it already exists.
#>

function Save-DbSettingsToFile{
    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true, HelpMessage = "Settings object to save")]
        $settings,
        
        [Parameter(Mandatory = $true, HelpMessage = "Path to the settings file")]
        [string]$DBDeploySettingsFile
    )

    Write-Verbose "Saving DB deployment settings to $DBDeploySettingsFile"
    
    $parentFolder = Split-Path -Parent $DBDeploySettingsFile

    if (-not (Test-Path $parentFolder)){ New-Item -ItemType Directory $parentFolder -Force | Out-Null}

    Get-SettingsAsJson $settings | Out-File $DBDeploySettingsFile -Force
}