Install-ALOpsBCOpenAPI.ps1

function Install-ALOpsBCOpenAPI(){
    [CmdletBinding()]
    param
    (
        [string] $ServerInstance = "BC",
        [string] $BCApiEndpoint,
        [string] $BCApiKey,
        [string] $ListenHost = "*",
        [int] $ListenPort = 42069,
        [string] $CustomScriptPath
    )
    
    $ErrorActionPreference = "Stop"        

    #*** Settings ***
    $DisplayName = "ALOps BC OpenAPI - $($ServerInstance)"
    $ServiceName = "ALOpsBCOpenAPI`$$($ServerInstance)"

    Write-Host "*** Deploy [$($DisplayName)]" -ForegroundColor Green
    
    #*** Remove Previous Installed Service
    Remove-ALOpsBCOpenAPI -ServiceName $ServiceName

    #*** Install Service ***
    Write-Host "*** Install ALOps-BC-OpenAPI Agent" -ForegroundColor Green
    $alopsService = get-service -Name $ServiceName -ErrorAction SilentlyContinue
    if ($null -eq $alopsService) {
        $ServicePath = Join-Path -Path ((get-Module ALOps.BCOpenAPI).ModuleBase) -ChildPath "bin\*"
    
        $ProgramFilesRoot = Join-Path -Path $env:ProgramFiles -ChildPath "ALOps\BCOpenAPI\$($ServerInstance)"
        if (-not (Test-Path -Path $ProgramFilesRoot)){
            New-Item -Path $ProgramFilesRoot -ItemType Directory -Confirm:$false -Force:$true | Out-Null
        }

        Copy-Item -Path  $ServicePath -Destination $ProgramFilesRoot -Recurse -Confirm:$false -Force:$true

        ### Configure Service
        $BCOpenAPIConfig = Join-Path -Path $ProgramFilesRoot -ChildPath "ALOpsBCOpenAPI.exe.config"

        $ConfigXml = [xml](Get-Content -Path $BCOpenAPIConfig -Encoding UTF8 -Raw)
        $ALOpsBCOpenAPISettings = $ConfigXml.configuration.applicationSettings.'ALOpsBCOpenAPI.Properties.Settings'.setting

        $P_BCAPIEndpoint = ($ALOpsBCOpenAPISettings | where-object { $_.name -eq "bc_api_endpoint"})
        $P_BCAPIEndpoint.value = "$($BCApiEndpoint)"

        $P_BCAPIKey = ($ALOpsBCOpenAPISettings | where-object { $_.name -eq "bc_api_key"})
        $P_BCAPIKey.value = "$($BCApiKey)"

        $P_ListenHost = ($ALOpsBCOpenAPISettings | where-object { $_.name -eq "listen_host"})
        $P_ListenHost.value = "$($ListenHost)"

        $P_ListenPort = ($ALOpsBCOpenAPISettings | where-object { $_.name -eq "listen_port"})
        $P_ListenPort.value = "$($ListenPort)"

        $P_ServiceName = ($ALOpsBCOpenAPISettings | where-object { $_.name -eq "ServiceName"})
        $P_ServiceName.value = "$($ServiceName)"

        $ConfigXml.Save($BCOpenAPIConfig)

        ### Register Service
        $ALOpsBCOpenAPIEXE = Join-Path -Path $ProgramFilesRoot -ChildPath "ALOpsBCOpenAPI.exe"
        & "sc.exe" create "$($ServiceName)" displayname= "$($DisplayName)" binpath= "$($ALOpsBCOpenAPIEXE)" start= auto | Out-Null
    }

    #*** Start Service ***
    Write-Host "*** Start Service [$($ServiceName)]" -ForegroundColor Green
    Start-Service -Name $ServiceName
    
    #*** Setup ServiceTier
    $ALOpsBCOpenAPIEndpoint = "http://$($ListenHost):$($ListenPort)/"
    Write-Host "ALOps BC OpenAPI Endpoint: $($ALOpsBCOpenAPIEndpoint)"
}