major logging overhaul, plus other fixes

This commit is contained in:
2020-10-22 14:01:35 -04:00
parent 77cf4d6276
commit 978373ebe2
27 changed files with 430 additions and 611 deletions
+22 -67
View File
@@ -1,76 +1,31 @@
#[CmdletBinding()]
param(
[string]$TOKEN,
[int]$CUSTOMER_ID,
[int]$SOFTWARE_ID=101,
[string]$SERVER='manage.emberkom.com',
[string]$AGENT_FILENAME='EmberkomAgentSetup.exe'
param(
[Parameter(Mandatory=$true)][string]$TOKEN,
[Parameter(Mandatory=$true)][int]$CUSTOMER_ID,
[Parameter(Mandatory=$false)][int]$SOFTWARE_ID=101,
[Parameter(Mandatory=$false)][string]$SERVER='manage.emberkom.com',
[Parameter(Mandatory=$false)][string]$AGENT_FILENAME='EmberkomAgentSetup.exe'
)
## Immediately delete this script from disk
$THIS_SCRIPT = $MyInvocation.InvocationName
Try { Remove-Item $MyInvocation.InvocationName -Force }
Catch { Write-Output "This script tried to delete itself but failed!" }
## Get temporary path
$TEMP = [System.IO.Path]::GetTempPath()
## Setup log file and function to write to it
$LOGFILE = "{0}EmberkomAgentSetup_{1}.log" -f $TEMP, $(Get-Date -Format "yyyyMMddHHmmss")
If (! (Test-Path $LOGFILE) ) { New-Item -Path $LOGFILE -ItemType File | Out-Null; Write-Output "Log file: ${LOGFILE}" }
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"
Write-Log "CUSTOMER_ID: ${CUSTOMER_ID}"
Write-Log "SOFTWARE_ID: ${SOFTWARE_ID}"
## Source the Tools.ps1 script
Function SourceTools
{ . $([Scriptblock]::Create((New-Object Net.WebClient).DownloadString('https://dev.emberkom.com/emberkom/management-scripts/raw/branch/master/Tools.ps1'))) }
. SourceTools
## Build the path for $AGENT_INSTALLER
$AGENT_INSTALLER = "${TEMP}${AGENT_FILENAME}"
Write-Log "Agent installer will be saved to: ${AGENT_INSTALLER}"
$AGENT_INSTALLER = '{0}{1}' -f (GetTempPath), $AGENT_FILENAME
## Make sure the CUSTOMER_ID is specified
If ( $CUSTOMER_ID -and $TOKEN )
## Get the right path to agent.exe based on the system bit-level
If ( Is64bit ) { $AGENT_EXE = "${Env:ProgramFiles(x86)}\N-able Technologies\Windows Agent\bin\agent.exe" }
Else { $AGENT_EXE = "${Env:ProgramFiles}\N-able Technologies\Windows Agent\bin\agent.exe" }
## Make sure we need to install the agent
If ( !(Test-Path $AGENT_EXE) )
{
## Get the right path to agent.exe based on the system bit-level
If ( Test-Path "${Env:ProgramFiles(x86)}" )
{ $AGENT_EXE = "${Env:ProgramFiles(x86)}\N-able Technologies\Windows Agent\bin\agent.exe" }
Else { $AGENT_EXE = "${Env:ProgramFiles}\N-able Technologies\Windows Agent\bin\agent.exe" }
## Download agent and get path
$AGENT_INSTALLER = Download-File -URL 'https://manage.emberkom.com/download/2020.1.0.202/winnt/N-central/WindowsAgentSetup.exe' -File $AGENT_INSTALLER
## Make sure we need to install the agent
If ( !(Test-Path $AGENT_EXE) )
{
## Build the AGENT_URL
$AGENT_URL = 'https://manage.emberkom.com/download/2020.1.0.202/winnt/N-central/WindowsAgentSetup.exe'
## Install the agent
Install-Program $AGENT_INSTALLER -Arguments "/s /v"" /qn CUSTOMERID=${CUSTOMER_ID} CUSTOMERSPECIFIC=1 REGISTRATION_TOKEN=${TOKEN} SERVERPROTOCOL=HTTPS SERVERADDRESS=${SERVER} SERVERPORT=443""" -Delete
## Delete $AGENT_INSTALLER if it already exists
If ( Test-Path $AGENT_INSTALLER )
{
Write-Log "Deleting previous agent installer: ${AGENT_INSTALLER}"
Try { Remove-Item $AGENT_INSTALLER -Force }
Catch { Write-Log $_.Exception.Message }
}
} Else { LogMsg "The agent is already installed on this endpoint." }
## Download the agent installer
Write-Log "Downloading agent installer from: ${AGENT_URL}"
Try { (New-Object System.Net.WebClient).DownloadFile($AGENT_URL, $AGENT_INSTALLER) }
Catch { Write-Log $_.Exception.Message ; Exit }
## Install the agent
Write-Log "Begin installing agent"
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 }
## Remove the agent installer
If ( Test-Path $AGENT_INSTALLER )
{
Write-Log "Deleting agent installer: ${AGENT_INSTALLER}"
Try { Remove-Item -Path $AGENT_INSTALLER -Force }
Catch { Write-Log $_.Exception.Message }
}
Write-Log "Agent installation script has finished"
} Else { Write-Log "The agent is already installed on this endpoint." }
} Else { Write-Log "No CUSTOMER_ID or TOKEN was specified!" }