Public/ps1/UblionConfiguration.ps1
### 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 |