Install-SBRAgent.ps1

 function Install-SBRAgent(){
     [CmdletBinding()]
     param
     (
        [Parameter(Mandatory=$true)] 
        [string] $RegistrationKey
     )

    #*** Settings ***
    Write-Host "*** Deploy SBR-Agent" -ForegroundColor Green
    $ErrorActionPreference = "Stop"    
    $InstallFolder = Join-Path -Path $env:programfiles -ChildPath "SBR"
    $ServiceName = "SBRAgent"
    
    #*** Parse Parameters ***
    Write-Host "*** Parse SBR-Agent Configuration" -ForegroundColor Green
    try 
    {
        $ParameterBytes  = [System.Convert]::FromBase64String($RegistrationKey)
    } 
    catch
    {
        Write-Error -Message "Invalid [RegistrationKey]. Aborting installation."
        return
    }
    
    $ParameterJson   = [System.Text.Encoding]::UTF8.GetString($ParameterBytes)
    $ParameterObject = ConvertFrom-Json -InputObject $ParameterJson 

    $ApiBaseUrl = $ParameterObject.SBRApiUrl
    $ApiKey     = $ParameterObject.SBRApiKey
        
    #*** Check Prerequisites ***
    Write-Host "*** Check Prerequisites" -ForegroundColor Green
    Add-Type -AssemblyName System.IO.Compression.FileSystem
    $InstallUtilPath = Get-SBRInstallUtil
        
    #*** Register Agent ***
    Write-Host "*** Register Agent" -ForegroundColor Green
    $SBRAgent = Register-SBRAgent -ApiBaseUrl $ApiBaseUrl `
                                  -ApiKey $ApiKey `
                                  -AgentADDomain ([System.Net.NetworkInformation.IPGlobalProperties]::GetIPGlobalProperties()).DomainName.ToLower() `
                                  -AgentMachineName ([System.Net.NetworkInformation.IPGlobalProperties]::GetIPGlobalProperties()).HostName
                 
    #*** Activate Agent ***
    Write-Host "*** Activate Agent" -ForegroundColor Green
    Enable-SBRAgent -ApiBaseUrl $ApiBaseUrl `
                    -ApiKey $ApiKey `
                    -AgentUID $SBRAgent.AgentUID | out-null

    #*** Download Agent ***
    Write-Host "*** Download Agent" -ForegroundColor Green
    $TempZipFile = Invoke-SBRDownload -ApiBaseUrl $ApiBaseUrl `
                                      -ApiKey $ApiKey `
                                      -AgentUID $SBRAgent.AgentUID
    
    #*** Stop Existing Agent ***
    $sbrService = get-service -Name $ServiceName -ErrorAction SilentlyContinue
    $tryCount = 0
    if ($null -ne $sbrService){
        Write-Host "*** Stopping existing Agent" -ForegroundColor Green
        Stop-Service -Name $ServiceName
        while ($sbrService.Status -ne "Stopped" ){
            $tryCount += 1;            
            Start-Sleep -Seconds 1
            $sbrService = 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 Agent" -ForegroundColor Green
        $oldServicePath = (get-wmiobject win32_service | Where-Object { $_.name -eq $ServiceName} | select Name, DisplayName, State, PathName).PathName
        & $InstallUtilPath /u $oldServicePath
    }
    
    #*** Extract Agent ***
    Write-Host "*** Extract setup" -ForegroundColor Green
    $AgentPath = Join-Path -Path $InstallFolder -ChildPath "Agent"
    Remove-Item -Path $AgentPath -Recurse -Force -Confirm:$false -ErrorAction Ignore        
    [System.IO.Compression.ZipFile]::ExtractToDirectory($TempZipFile, $InstallFolder)
    
    #*** Install Agent ***
    Write-Host "*** Install Agent" -ForegroundColor Green
    $sbrService = get-service -Name $ServiceName -ErrorAction SilentlyContinue
    if ($null -eq $sbrService) {
        $ServicePath = Join-Path -Path $InstallFolder -ChildPath "Agent\SBRAgentService.exe"
    
        & $InstallUtilPath $ServicePath
    }
    
    #*** Start Agent ***
    Write-Host "*** Start Agent" -ForegroundColor Green
    Start-Service -Name $ServiceName    
 }