Public/ps1/UblionConfiguration.ps1

function Validate-Configuration {
    $testSql = "Server=10.21.0.199\\LEFTPRD14;Database=AxillaApprx;Integrated Security=False;Persist Security Info=True;User ID=axillaApprx;Password=dUj0RyWL2D71;MultipleActiveResultSets=True"

}

function Install-LeftConnectRemoteControllerSQL
{    
    [CmdletBinding()]
    param(
        [Parameter(Mandatory)]
        [String]$Tenant,
        
        [System.Management.Automation.PSCredential]
        $Credential = $(Get-Credential),

        [Parameter(Mandatory)]
        [String]$SqlConnectionString,

        [String]$channelId,
        [String] $LogFolder

    )
        
    
    Set-ConfigurationValue -name SqlConnectionString -value $SqlConnectionString -secure

    Install-LeftConnectionBase -Tenant $Tenant -Credential $Credential -LogFolder $LogFolder

    Log("LeftConnect + SQL installed")
}


### Storing the remote connection credentials on a save way
function Install-LeftConnectionBase{
    param(
        [Parameter(Mandatory)]
        [String]$Tenant,
                
        $Credential,
        [String] $LogFolder,
        [int] $LogHours,
        [int] $LogRetention
    )
    
    
    Set-ConfigurationValue -name UserName -value $Credential.UserName
    Set-ConfigurationValue -name Tenant -value $Tenant
    Set-ConfigurationValue -name Password -value ($Credential.Password | ConvertFrom-SecureString)

    Set-LogConfiguration -LogFilder $LogFolder -LogHours $LogHours -LogRetention $LogRetention


}



function Set-ConfigurationValue{
    param($name, $value, [switch]$secure)

    
    $folder = Get-LeftConnectConfigurationFolder


    if ($secure) {
        $value = ConvertTo-SecureString -Force -AsPlainText $value | ConvertFrom-SecureString 
    }
    
    if (Test-Path "$folder\config.json") {
    } else {
        "{}"|set-content "$folder\config.json"
    }

    $config = Get-Content "$folder\config.json" | ConvertFrom-Json
    # the prpoerty does not exist
    if (($config.$name -ne $null)) {
        $config.$name = $value
    } else {
        $config | Add-Member -MemberType NoteProperty -Name $name -Value $value
    }


    $config | ConvertTo-Json | Set-Content "$folder\config.json" 

}

function Remove-ConfigurationValue{
    param($name)

    
    $folder = Get-LeftConnectConfigurationFolder


    
    if (Test-Path "$folder\config.json") {
    } else {
        "{}"|set-content "$folder\config.json"
    }

    $config = Get-Content "$folder\config.json" | ConvertFrom-Json
    # the prpoerty does not exist

    $config.PSObject.Properties.Remove($name)


    $config | ConvertTo-Json | Set-Content "$folder\config.json" 

}


function Get-ConfigurationValue{
    param($name,[switch]$secure, $defaultValue)

    
    $folder = Get-LeftConnectConfigurationFolder

       
    if (Test-Path "$folder\config.json") {
    } else {
        "{}"|set-content "$folder\config.json"
    }

    $config = Get-Content "$folder\config.json" | ConvertFrom-Json
    # the prpoerty does not exist
    if (($config.$name -ne $null)) {
        if ($secure) {
            
            $secureString = $config.$name | ConvertTo-SecureString
            $BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($secureString)
            [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)
        } else {
            $config.$name
        }
    } else {
        $defaultValue
    }
}

function Get-Configuration {
       
    $folder = Get-LeftConnectConfigurationFolder
    
    
    if (Test-Path "$folder\config.json") {
    } else {
        "{}"|set-content "$folder\config.json"
    }

    Get-Content "$folder\config.json" | ConvertFrom-Json
}


function Get-LeftConnectSqlConfiguration {
    
    @{
        connectionString = Get-ConfigurationValue -name SqlConnectionString -secure
    }
}

function Get-LeftConnectBaseConfiguration {
    @{
        user =     Get-ConfigurationValue -name UserName 
        pass =     Get-ConfigurationValue -name Password -secure 
        tenant =   Get-ConfigurationValue -name Tenant
    }
}

<#
.SYNOPSIS
Get the tenant url
 
.DESCRIPTION
Returns the url host
 
.EXAMPLE
An example
 
.NOTES
General notes
#>
#
function Get-LeftConnectUrlHost {
    $tenant = (Get-LeftConnectBaseConfiguration).tenant

    if ($tenant -eq $null){
        throw "Tenant not specified."
    }
    if (-not $tenant.Contains("-connectapi")) {
        $tenant = $tenant -replace ".apprx.eu", "-connectapi.apprx.eu"
    }
    "https://$tenant/"
}



<#
.SYNOPSIS
Create an folder
 
.DESCRIPTION
Long description
 
.PARAMETER DirectoryToCreate
Parameter description
 
.EXAMPLE
An example
 
.NOTES
General notes
#>
#
function Install-AppData{
    [CmdletBinding()]
    Param(
    [Parameter(Mandatory = $True)]
    [String] $DirectoryToCreate)

    if (-not (Test-Path -LiteralPath $DirectoryToCreate)) {
        
        try {
            New-Item -Path $DirectoryToCreate -ItemType Directory -ErrorAction Stop | Out-Null #-Force
        }
        catch {
            Write-Error -Message "Unable to create directory '$DirectoryToCreate'. Error was: $_" -ErrorAction Stop
        }
        Write-host "Successfully saved credentials"
        $DirectoryToCreate

    }
    else {
        Write-host "Directory already existed"
        $DirectoryToCreate
    }
}

function Get-LeftConnectConfigurationFolder{
    $folder = $env:LOCALAPPDATA + "\LeftConnect"
    
    if( -not (Test-path $folder)){
      Install-AppData -DirectoryToCreate $folder -ErrorAction Ignore
    }
    $folder
}







Export-ModuleMember -function  Install-LeftConnectRemoteControllerSQL


Export-ModuleMember -function  Install-LeftConnectionBase


Export-ModuleMember -function  Set-ConfigurationValue


Export-ModuleMember -function  Remove-ConfigurationValue


Export-ModuleMember -function  Get-ConfigurationValue


Export-ModuleMember -function  Get-Configuration


Export-ModuleMember -function  Get-LeftConnectSqlConfiguration


Export-ModuleMember -function  Get-LeftConnectBaseConfiguration


Export-ModuleMember -function  Get-LeftConnectUrlHost


Export-ModuleMember -function  Install-AppData


Export-ModuleMember -function  Get-LeftConnectConfigurationFolder