Files
management-scripts/Install-ManagementAgent.ps1
T

75 lines
3.1 KiB
PowerShell
Raw Normal View History

2020-05-22 14:35:46 -04:00
[CmdletBinding()]
param(
2020-09-04 22:09:42 -04:00
[string]$TOKEN,
2020-05-22 16:09:14 -04:00
[int]$CUSTOMER_ID,
[int]$SOFTWARE_ID=101,
2020-09-17 16:02:06 -04:00
[string]$SERVER='manage.emberkom.com',
2020-05-22 14:35:46 -04:00
[string]$AGENT_FILENAME='EmberkomAgentSetup.exe'
)
2020-06-22 12:32:01 -04:00
## Get temporary path
$TEMP = [System.IO.Path]::GetTempPath()
2020-06-22 10:57:58 -04:00
## Setup log file and function to write to it
2020-06-22 12:32:01 -04:00
$LOGFILE = "{0}EmberkomAgentSetup_{1}.log" -f $TEMP, $(Get-Date -Format "yyyyMMddHHmmss")
2020-09-04 09:14:24 -04:00
If (! (Test-Path $LOGFILE) ) { New-Item -Path $LOGFILE -ItemType File | Out-Null; Write-Output "Log file: ${LOGFILE}" }
2020-06-22 10:57:58 -04:00
Function Write-Log([string]$message)
{
$entry = "{0} : {1}" -f $(Get-Date -Format "yyyyMMddHHmmss"), $Message
Add-Content -Path $LOGFILE -Value $entry
}
Write-Log "Agent installation script has started"
2020-05-22 16:09:14 -04:00
## Immediately delete this script from disk
2020-06-22 10:57:58 -04:00
$THIS_SCRIPT = $MyInvocation.InvocationName
Write-Log "Deleting this script: ${THIS_SCRIPT}"
Try { Remove-Item $MyInvocation.InvocationName -Force }
Catch { Write-Log "This script tried to delete itself but failed!" }
Write-Log "CUSTOMER_ID: ${CUSTOMER_ID}"
Write-Log "SOFTWARE_ID: ${SOFTWARE_ID}"
2020-05-22 16:09:14 -04:00
2020-06-22 11:49:35 -04:00
## Build the path for $AGENT_INSTALLER
2020-09-04 09:14:24 -04:00
$AGENT_INSTALLER = "${TEMP}${AGENT_FILENAME}"
2020-06-22 10:57:58 -04:00
Write-Log "Agent installer will be saved to: ${AGENT_INSTALLER}"
2020-05-22 14:35:46 -04:00
2020-05-22 16:09:14 -04:00
## Make sure the CUSTOMER_ID is specified
2020-09-04 22:09:42 -04:00
If ( $CUSTOMER_ID -and $TOKEN )
2020-05-22 14:35:46 -04:00
{
## Make sure we need to install the agent
If ( !(Test-Path 'HKLM:SOFTWARE\N-able Technologies\NcentralAsset') -and !(Test-Path 'SOFTWARE\Wow6432Node\N-able Technologies\NcentralAsset'))
{
2020-05-22 16:09:14 -04:00
## Build the AGENT_URL
2020-09-04 22:09:42 -04:00
#$AGENT_URL = 'https://manage.emberkom.com/downloadAgentOrProbeSoftwareDownloadAction.action?customerId={0}&softwareId={1}' -f $CUSTOMER_ID.ToString(),$SOFTWARE_ID.ToString()
$AGENT_URL = 'https://manage.emberkom.com/download/2020.1.0.202/winnt/N-central/WindowsAgentSetup.exe'
2020-05-22 16:09:14 -04:00
## Delete $AGENT_INSTALLER if it already exists
If ( Test-Path $AGENT_INSTALLER )
{
2020-06-22 10:57:58 -04:00
Write-Log "Deleting previous agent installer: ${AGENT_INSTALLER}"
2020-05-22 16:09:14 -04:00
Try { Remove-Item $AGENT_INSTALLER -Force }
2020-06-22 10:57:58 -04:00
Catch { Write-Log $_.Exception.Message }
2020-05-22 16:09:14 -04:00
}
2020-05-24 15:28:49 -04:00
## Download the agent installer
2020-06-22 10:57:58 -04:00
Write-Log "Downloading agent installer from: ${AGENT_URL}"
Try { (New-Object System.Net.WebClient).DownloadFile($AGENT_URL, $AGENT_INSTALLER) }
2020-06-22 10:57:58 -04:00
Catch { Write-Log $_.Exception.Message ; Exit }
2020-05-22 14:35:46 -04:00
## Install the agent
2020-06-22 10:57:58 -04:00
Write-Log "Begin installing agent"
2020-10-15 13:22:55 -04:00
Try { Start-Process $AGENT_INSTALLER -ArgumentList "/s /v"" /qn CUSTOMERID=${CUSTOMER_ID} CUSTOMERSPECIFIC=1 REGISTRATION_TOKEN=${TOKEN} SERVERPROTOCOL=HTTPS SERVERADDRESS=${SERVER} SERVERPORT=443""" -Wait }
Catch { Write-Log $_.Exception.Message }
2020-05-22 16:09:14 -04:00
## Remove the agent installer
If ( Test-Path $AGENT_INSTALLER )
{
2020-06-22 10:57:58 -04:00
Write-Log "Deleting agent installer: ${AGENT_INSTALLER}"
2020-05-22 16:09:14 -04:00
Try { Remove-Item -Path $AGENT_INSTALLER -Force }
2020-06-22 10:57:58 -04:00
Catch { Write-Log $_.Exception.Message }
2020-05-22 16:09:14 -04:00
}
2020-06-22 10:57:58 -04:00
Write-Log "Agent installation script has finished"
} Else { Write-Log "The agent is already installed on this endpoint." }
2020-09-04 22:09:42 -04:00
} Else { Write-Log "No CUSTOMER_ID or TOKEN was specified!" }