Functions/Export-DqChecks.ps1

Function Export-DqChecks
{
  <#
    .SYNOPSIS
      Deze functie haalt alle eigen gemaakte controles op uit een DQ Monitor Webservice en exporteert deze naar een checks .JSON bestand.
      Tevens worden de T-SQL statements in een submap geplaatst.
   
    .DESCRIPTION
      De Export-DqChecks functie haalt een lijst op van eigen gemaakte controles die bekend zijn bij de DQ Monitor Webservice.
      Van deze lijst wordt een export gemaakt, opgeslagen als .JSON bestand inclusief de T-SQL statements, welke in een submap 'T-SQL' worden geplaatst.
      Voordat deze functie gebruikt wordt, zorg ervoor dat de Context is gezet via functie Set-DqContext.
   
    .PARAMETER CheckJsonFile
      Het volledige pad naar een niet bestaand .JSON bestand waarnaar de resultaten worden opgeslagen.
      Alle statements die worden opgehaald behorende bij de individuele controles worden geplaatst in een subfolder genaamd 't-sql' naast het JSON bestand.
 
    .PARAMETER Force (Optioneel)
      Indien het bestand 'CheckJsonFile' bestaat geeft deze parameter de mogelijkheid om deze te overschrijven.
 
    .INPUTS
      Geen.
   
    .OUTPUTS
      Geen.
  
    .EXAMPLE
      PS> Export-DqChecks -CheckJsonFile "C:\dq\checks.json"
      4 controles weggeschreven naar 'C:\dq\checks.json'. T-SQL statement bestanden zijn weggeschreven naar locatie 'C:\dq\t-sql\'.
   
    .LINK
      Set-DqContext
  #>

  [CmdletBinding()]
  Param(
    [Parameter(Mandatory=$True, Position=1)]
    [ValidateNotNullOrEmpty()]
    [String] $CheckJsonFile,

    [Parameter(Mandatory=$False, Position=2)]
    [Switch] $Force
  )

  Validate-Context

  Function Remove-InvalidFileNameChars 
  {
    Param(
      [Parameter(Mandatory=$true, Position=0, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)] [String]$Name
    )

    $InvalidChars = [System.IO.Path]::GetInvalidFileNameChars() -Join ''
    $Res = "[{0}]" -f [RegEx]::Escape($InvalidChars)
    Return ($Name -replace $Res)
  }

  $Overwrite = $False
  If ($Force.IsPresent -and $Force)
  {
    $Overwrite = $True 
  }

  If (Test-Path -Path $CheckJsonFile -PathType Leaf)
  {
    If (-not $Overwrite)
    {
      Write-Host "Controle JSON bestand '$($CheckJsonFile)' bestaat al. Indien deze overschreven moet worden, gebruik parameter 'Force'." -ForegroundColor Red
      Return;
    }
  }
  
  $CheckJsonPath = Split-Path -Path $CheckJsonFile -Parent
  $CheckStatementPath = Join-Path -Path $CheckJsonPath -ChildPath "T-SQL"

  If (-not(Test-Path -Path $CheckStatementPath -PathType Container))
  {
    Write-Verbose "Creating folder '$($CheckJsonPath)'..."
    New-Item -Path $CheckJsonPath -Name "T-SQL" -ItemType "Directory" -Force | Out-Null
  }

  $AllCustomChecks = Get-DqChecks -CustomOnly -IncludeStatement
  $CustomChecks = @()
  ForEach($CustomCheck in $AllCustomChecks)
  {
    $SqlStatementFile = "$($CustomCheck.Name.ToUpperInvariant() | Remove-InvalidFileNameChars).sql"
    $SqlStatementFullPath = Join-Path -Path $CheckStatementPath -ChildPath $SqlStatementFile
    $Check = [PSCustomObject] @{
      name = $CustomCheck.Name;
      caption = $CustomCheck.Documentation.Caption;
      description = $CustomCheck.Documentation.Description;
      technicalDescription = $CustomCheck.Documentation.TechnicalDescription; 
      tags = $CustomCheck.Tags;
      entity = $CustomCheck.Entity;
      isSensitive = $CustomCheck.ResultCaptionIsSensitive;
      source = $CustomCheck.Source;
      statementFile = $SqlStatementFullPath 
    };

    # Check if statement file already exists. When overwrite is not set, we will not include this check.
    If ((Test-Path -Path $SqlStatementFullPath -PathType Leaf) -and -not($Overwrite))
    {
      Write-Warning "T-SQL file '$($SqlStatementFullPath)' voor controle '$($CustomCheck.Name)' bestaat al. Indien deze overschreven moet worden, gebruik parameter 'Force'. Controle wordt genegeerd."
      Continue;
    }

    Try
    {
      Set-Content -Path $SqlStatementFullPath -Value $CustomCheck.Statement -Encoding UTF8 -Force | Out-Null
    }
    Catch
    {
      Write-Warning "Er is een fout opgetreden bij het wegschrijven van bestand '$($SqlStatementFullPath)' voor controle '$($CustomCheck.Name)'. De controle wordt niet meegenomen."
      Continue;
    }

    $CustomChecks += $Check
  }

  $CustomChecksObject = [PSCustomObject] @{
    checks = $CustomChecks 
  }
  
  If ($CustomChecks.Count -gt 0)
  {
    Set-Content -Path $CheckJsonFile -Value ($CustomChecksObject | ConvertTo-Json) -Encoding UTF8 -ErrorAction Stop | Out-Null
    Write-Host "$($CustomChecks.Count) controles weggeschreven naar '$($CheckJsonFile)'. T-SQL statement bestanden zijn weggeschreven naar locatie '$($CheckStatementPath)'."
  }
  Else
  {
    Write-Host "Er zijn geen controles gevonden/weggeschreven."
  }
}