Connect-Qlik.ps1

function Connect-Qlik {
    <#
    .SYNOPSIS
    This function ...
 
    .DESCRIPTION
    A bit more description
 
    .PARAMETER FromPipeline
    Shows how to process input from the pipeline, remaining parameters or by named parameter.
 
    .EXAMPLE
    Connect-Qlik 'abc'
 
    Description of the example.
 
    #>


    <# Enable -Confirm and -WhatIf. #>
    [CmdletBinding(SupportsShouldProcess = $true)]
    param(
    # Name of the Sense server to connect to
    [parameter(Position=0)]
    [string]$Computername,
    # Disable checking of certificate trust
    [switch]$TrustAllCerts = $true,
    # UserId to use with certificate authentication in the format domain\username
    [Parameter(ParameterSetName = "Certificate")]
    [string]$Username = "$($env:userdomain)\$($env:username)"
    # Client certificate to use for authentication
    #[parameter(ParameterSetName = "Certificate", ValueFromPipeline=$true)]
    #[System.Security.Cryptography.X509Certificates.X509Certificate]$Certificate
    # Credentials to use when connecting via proxy
    #[parameter(ParameterSetName = "Credential")]
    #[PSCredential]$Credential,
    # Use credentials of logged on user for authentication, prevents automatically locating a certificate
    #[parameter(ParameterSetName = "Default")]
    #[switch]$UseDefaultCredentials
    )

    begin {
    }

    process {
    # Since we are connecting we need to clear any variables relating to previous connections
    $script:api_params = $null
    $script:prefix = $null
    $script:webSession = $null

    If( $TrustAllCerts ) {
      add-type @"
        using System.Net;
        using System.Security.Cryptography.X509Certificates;
        public class TrustAllCertsPolicy : ICertificatePolicy {
          public bool CheckValidationResult(
            ServicePoint srvPoint, X509Certificate certificate,
            WebRequest request, int certificateProblem) {
            return true;
          }
        }
"@

      [System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
    }
   <# If( !$Certificate -And !$Credential -And !$UseDefaultCredentials ) {
      $certs = @(FetchCertificate "My" "CurrentUser")
      Write-Verbose "Found $($certs.Count) certificates in CurrentUser store"
      If( $certs.Count -eq 0 ) {
        $certs = @(FetchCertificate "My" "LocalMachine")
        Write-Verbose "Found $($certs.Count) certificates in LocalMachine store"
      }
      If( $certs.Count -gt 0 ) {
        $Certificate = $certs[0]
      }
    }#>

     $certs = @(FetchCertificate -storeName "My" -storeLocation "LocalMachine" -certFindValue "QlikClient")
        Write-Verbose "Found $($certs.Count) certificates in LocalMachine store"
        $Certificate = $certs[0]
          Write-Verbose "Using certificate $($Certificate.FriendlyName)"
    $Script:api_params = @{
        Certificate=$Certificate
        Header=@{"X-Qlik-User" = $("UserDirectory=INTERNAL;UserId=sa_repository" -f $($username -split "\\"))}
      }
      $port = ":4242"
      <#
    If( $Certificate ) {
      Write-Verbose "Using certificate $($Certificate.FriendlyName)"
 
      $Script:api_params = @{
        Certificate=$Certificate
        Header=@{"X-Qlik-User" = $("UserDirectory=INTERNAL;UserId=sa_repository" -f $($username -split "\\"))}
      }
      $port = ":4242"
    } ElseIf( $Credential ) {
      Write-Verbose $("Using credentials for {0}" -f $Credential.Username)
      $Script:api_params = @{
        Credential=$Credential
      }
    } Else {
      Write-Verbose "No valid certificate found, using Windows credentials"
      $Script:api_params = @{
        UseDefaultCredentials=$true
      }
    }#>


    If ( $Computername ) {
      If( $Computername.ToLower().StartsWith( "http" ) ) {
        $Script:prefix = $Computername
      } else {
        $Script:prefix = "https://" + $Computername + $port
      }
    } else {
      $Script:prefix = "https://" + $env:computername + $port
    }

    $result = Get-QlikAbout
    return $result
  }

    end {
    }
}

if ($loadingModule) {
    Export-ModuleMember -Function 'Connect-Qlik'
}