Remove-ALOpsBCOpenAPI.ps1

function Remove-ALOpsBCOpenAPI()
{
    param
    (
        [string] $ServiceName = "ALOpsBCOpenAPI"
    )
   
    $alopsService = get-service -Name $ServiceName -ErrorAction SilentlyContinue

    if ($null -eq $alopsService){
        return
    }

    $tryCount = 0
    
    Write-Host "*** Stopping existing service" -ForegroundColor Green
    Stop-Service -Name $ServiceName
    while ($alopsService.Status.ToString().ToUpper() -ne "Stopped".ToUpper() ){
        $tryCount += 1;            
        Start-Sleep -Seconds 1
        $alopsService = get-service -Name $ServiceName

        if ($tryCount -gt 15){
            $ServicePID = (get-wmiobject win32_service | Where-Object { $_.name -eq $ServiceName }).processID
            Stop-Process $ServicePID -Force
        }
    }
    Start-Sleep -Seconds 1

    Write-Host "*** Uninstall existing service" -ForegroundColor Green
    $oldService = get-wmiobject win32_service | Where-Object { $_.name -eq $ServiceName} | Select-Object -Property  Name, DisplayName, State, PathName
    Write-Host "$($oldService | format-list -Property * | out-string)"

    & "sc.exe" delete "$($ServiceName)"

    $ServiceLocation = Get-Item -Path $oldService.PathName
    Write-Host "*** Cleanup folder: [$($ServiceLocation.Directory)]"

    Remove-Item -Path $ServiceLocation.Directory -Recurse -Confirm:$false -Force:$true                            
}