Functions/Set-DqContext.ps1

Function Set-DqContext
{
  <#
    .SYNOPSIS
      Deze functie creëert en bewaard verbindinggegevens naar een DQ Monitor Webservice zodat deze gebruikt kan worden door andere functies in de huidige sessie.
    
    .DESCRIPTION
      De Set-DqContext plaatst authenticatie informatie, over DQ Monitor, voor andere functies die gedraaid worden in de huidige sessie. Deze informatie bevat de URL's om verbinding te maken.
         
    .PARAMETER PortalUrl
      De volledige url naar de DQ Monitor Portal. Deze wordt gebruikt om te achterhalen hoe verbinding gelegd moet worden met de onderdelen van de DQ Monitor Webservice.
     
    .PARAMETER Credential
      Optioneel: Een credential object verkregen via Get-Credential, om authenticatie met de DQ Monitor Webservice te overrulen. Standaard wordt Windows Authenticatie gebruikt met de huidige gebruiker.
 
    .PARAMETER Force
      Optioneel: Indien de DQ Monitor Webservice een versie draait die niet wordt ondersteund kan dat met deze switch genegeerd worden.
    
    .INPUTS
      Geen.
    
    .OUTPUTS
      Een [DqConnection] object.
    
    .EXAMPLE
      PS> Set-DqContext -PortalUrl "http://dq-portal"
       
      PortalUrl ApiUrl StsUrl
      ------ ------ ------
      http://dq-api/ http://dq-api/api/ http://dq-sts/oauth/ro
    
    .EXAMPLE
      PS> Set-DqContext -PortalUrl "https://woongeluk.dq-monitor.nl" -Credential $Credential
       
      PortalUrl ApiUrl StsUrl
      ------ ------ ------
      https://woongeluk.dq-monitor.nl/ https://woongeluk-api.dq-monitor.nl/api/ https://woongeluk-sts.dq-monitor.nl/oauth/ro
  #>


  [OutputType([DqConnection])]
  [CmdletBinding()]
  Param(
    [Parameter(Mandatory=$True, position=1)]
    [ValidateNotNullOrEmpty()]
    [String] $PortalUrl,

    [Parameter(Mandatory=$False, position=2)]
    [ValidateNotNull()]
    [System.Management.Automation.PSCredential] $Credential = $Null,

    [Parameter(Mandatory=$False, position=3)]
    [Switch] $Force
  )

  $DqConnection = Get-ConnectionSettings -PortalUrl $PortalUrl
  If (-not($DqConnection))
  {
    Write-Host "Handeling afgebroken." -ForegroundColor Red 
    Return;
  }

  $Context = New-Context -DqConnection $DqConnection -Credential $Credential
  If (-not($Context))
  {
    Write-Host "Handeling afgebroken." -ForegroundColor Red
    Return;
  }

  Set-Variable -Name "DqConnectionSettings" -Value $DqConnection -Scope Script | Out-Null
  Set-Variable -Name "DqContext" -Value $Context -Scope Script | Out-Null

  # Check if this Data Quality API is on the correct version
  If(-not($Force.IsPresent) -or (-not($Force)))
  {
    [System.Version] $Version = Get-DqVersion
    $Res = Validate-Version -Version $Version
    If (-not($Res))
    {
      Remove-Variable -Name "DqConnectionSettings" -Scope Script | Out-Null
      Remove-Variable -Name "DqContext" -Scope Script | Out-Null
      Write-Host "Verbinding met DQ Monitor API kon gemaakt worden maar deze draait versie '$($Version.ToString())' en wordt niet ondersteund." -ForegroundColor Red
      Return;
    }
  }
  
  $DqConnection
}