Install-SBRAgent.ps1
|
function Install-SBRAgent(){ [CmdletBinding()] param ( [Parameter(Mandatory=$true)] [string] $RegistrationKey ) #*** Settings *** $ErrorActionPreference = "Stop" $InstallFolder = Join-Path -Path $env:programfiles -ChildPath "SBR" $ServiceName = "SBRAgent" #*** Parse Parameters *** 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 *** Add-Type -AssemblyName System.IO.Compression.FileSystem $InstallUtilPath = Get-SBRInstallUtil #*** Register Agent *** $SBRAgent = Register-SBRAgent -ApiBaseUrl $ApiBaseUrl ` -ApiKey $ApiKey ` -AgentADDomain ([System.Net.NetworkInformation.IPGlobalProperties]::GetIPGlobalProperties()).DomainName.ToLower() ` -AgentMachineName ([System.Net.NetworkInformation.IPGlobalProperties]::GetIPGlobalProperties()).HostName #*** Activate Agent *** Enable-SBRAgent -ApiBaseUrl $ApiBaseUrl ` -ApiKey $ApiKey ` -AgentUID $SBRAgent.AgentUID #*** Download Agent *** $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){ 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 } #*** Extract Agent *** $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 *** $sbrService = get-service -Name $ServiceName -ErrorAction SilentlyContinue if ($null -eq $sbrService) { $ServicePath = Join-Path -Path $InstallFolder -ChildPath "Agent\SBRAgentService.exe" & $InstallUtilPath $ServicePath } #*** Start Agent *** Start-Service -Name $ServiceName } |