Private/RESTUtil.ps1

$RESTAdapterParameters = @{
    "Server" = "Server";
    "RESTPort" = "Port";
    "AuthenticationMode" = "AuthenticationMode";
    "SSL" = "SSL";
    "Instance" = "InstanceName";
    "ServiceAccountDomain" = "ServiceDomain";
    "ServiceAccountLogin" = "ServiceLogin";
    "ServiceAccountPassword" = "UnencryptedServicePassword";
    "SynchronizationTick" = "SynchronizationTimerStep";
    "MapOperationOnIgnoreResources" = "MapOperationOnIgnoreResourcesPS";
    "Tenant" = "TenantName";
    "WebServer" = "WebServerName";
    "WebPort" = "WebSitePort";
    "DefaultCulture" = "DefaultCultureCode";
    "UntrustedCARoot" = "AcceptUntrustedRoot";
    "ClientCertificatePath" = "ClientCertificatePath";
    "ClientCertificatePassword" = "UnencryptedClientCertificateKeyPassword";
    "ServerCAPath" = "ServerCertificateAuthorityPath";
}

$RESTAdapterParametersType = @{
    "Server" = "string";
    "RESTPort" = "int";
    "AuthenticationMode" = "string";
    "SSL" = "bool";
    "Instance" = "string";
    "ServiceAccountDomain" = "string";
    "ServiceAccountLogin" = "string";
    "ServiceAccountPassword" = "string";
    "SynchronizationTick" = "int";
    "MapOperationOnIgnoreResources" = "bool";
    "Tenant" = "string";
    "WebServer" = "string";
    "WebPort" = "string";
    "DefaultCulture" = "string";
    "UntrustedCARoot" = "bool";
    "ClientCertificatePath" = "string";
    "ClientCertificatePassword" = "string";
    "ServerCAPath" = "string";
}

function Set-RESTParameters(
    [string]$Tenant,
    [string]$Server,
    [int]$SOAPPort,
    [string]$AuthenticationMode,
    [bool]$SSL,
    [string]$InstanceName,
    [string]$Domain,
    [string]$Login,
    [string]$Password,
    [string]$TenantName) {
    
    if ($Server -eq "") {
        $Server = $Env:COMPUTERNAME
    }
    
    if ($SOAPPort -eq 0) {
        $SOAPPort = 7047
    }
    
    if ($AuthenticationMode -eq "") {
        $AuthenticationMode = "Negotiate"
        
        if ($Domain -eq "") {
            $Domain = $Env:COMPUTERNAME
        }
    }
    
    if ($ClientPort -eq 0) {
        $ClientPort = -1;
    }
    
    $configuration = @{
        UId = [System.Guid]::NewGuid();
        Version = 0;        
        Server = $Server;
        Port = $SOAPPort;
        AuthenticationMode = $AuthenticationMode;
        SSL = $SSL;
        InstanceName = $InstanceName;        
        ServiceDomain = $Domain;
        ServiceLogin = $Login;
        UnencryptedServicePassword = $Password;
        SynchronizerTimerStep = 3000;
        MapOperationsOnIgnoredResourcesPS = $true;
        KeepAlive = $true;
        RetryInitRatio = 5;
        TenantName = $TenantName;
        WebServerName = "";
        WebSitePort = 8080;
        LastModificationDate = [System.DateTime]::UtcNow;
    }
    
    Write-RESTParameters $Tenant $configuration
}

function Write-RESTParameters($Tenant,$configuration) {
    $data = $configuration | ConvertTo-Json

    $storePath = Get-RESTConfigurationPath $Tenant
    [System.IO.File]::WriteAllText($storePath, $data)
    Write-Verbose "REST configuration stored in $storePath"
}

function Get-RESTConfigurationPath($Tenant) {
    return Get-ConfigurationPath $Tenant "REST.json"
}

function Set-RESTAdapterParameter([string] $Tenant, [string] $Name, [string] $Value) {
    $storePath = Get-RESTConfigurationPath $Tenant
    $parameters = Get-AdapterParameters $Tenant $storePath
    if ($parameters -eq $null) {
        $parameters = New-RESTAdapterParameters $Tenant $storePath
    }

    if ($parameters -eq $null) {
        Write-Warning "Cannot create or get REST parameters for tenant $Tenant"
        return
    }

    Set-AdapterParameter $Name $Value $parameters $RESTAdapterParameters $RESTAdapterParametersType "REST"
    Write-RESTParameters $Tenant $parameters
    Write-Verbose "Parameter set"
}

function New-RESTAdapterParameters([string] $Tenant, $StorePath) {
    if (!(Test-Path $StorePath)) {
        Set-RESTParameters
    }

    return Get-AdapterParameters $Tenant $StorePath
}

function Write-RESTValidParameters {
    Write-ValidParameters "REST" $RESTAdapterParameters 
}