@@ -16,64 +16,159 @@ param (
1616 [string ]$PluginDir = $env: NRIA_PLUGIN_DIR ,
1717 [string ]$ConfigFile = $env: NRIA_CONFIG_FILE ,
1818 [string ]$AppDataDir = $env: NRIA_APP_DATA_DIR ,
19- [string ]$ServiceName = $env: NRIA_SERVICE_NAME
19+ [string ]$ServiceName = $env: NRIA_SERVICE_NAME ,
20+ [string ]$ServiceOverwrite = $env: NRIA_OVERWRITE
2021)
2122
23+ # Convert ServiceOverwrite string to boolean
24+ $ServiceOverwriteBool = $false
25+ if ($ServiceOverwrite -eq " true" -or $ServiceOverwrite -eq " 1" -or $ServiceOverwrite -eq $true ) {
26+ $ServiceOverwriteBool = $true
27+ }
28+
29+ # Initialize debug log file
30+ # For user accounts: use <drive>:\Users\<username>\.newrelic
31+ # For SYSTEM account (MSI): use <SystemDrive>:\ProgramData\New Relic\.logs
32+ if ($env: USERNAME -eq " SYSTEM" ) {
33+ $tempPath = " $env: SystemDrive \ProgramData\New Relic\newrelic-infra\tmp"
34+ } elseif ($env: USERPROFILE ) {
35+ $tempPath = " $env: USERPROFILE \.newrelic"
36+ } else {
37+ $tempPath = " $env: SystemDrive \Windows\Temp"
38+ }
39+ $DebugLogFile = Join-Path $tempPath " newrelic_installer_debug.log"
40+
41+ # Logging function
42+ function Write-DebugLog {
43+ param ([string ]$Message )
44+ $timestamp = Get-Date - Format " yyyy-MM-dd HH:mm:ss"
45+ $logEntry = " [$timestamp ] $Message "
46+ try {
47+ # Ensure directory exists
48+ $logDir = Split-Path $DebugLogFile - Parent
49+ if (-not (Test-Path $logDir )) {
50+ New-Item - Path $logDir - ItemType Directory - Force | Out-Null
51+ }
52+ # Write to log file
53+ Add-Content - Path $DebugLogFile - Value $logEntry - Force
54+ } catch {
55+ Write-Warning " Failed to write to debug log: $_ "
56+ }
57+ Write-Host $Message
58+ }
59+
60+ # Initialize log file with startup message
61+ try {
62+ $logDir = Split-Path $DebugLogFile - Parent
63+ if (-not (Test-Path $logDir )) {
64+ New-Item - Path $logDir - ItemType Directory - Force | Out-Null
65+ }
66+ $timestamp = Get-Date - Format " yyyy-MM-dd HH:mm:ss"
67+ " [$timestamp ] =====================================" | Out-File - FilePath $DebugLogFile - Force
68+ " [$timestamp ] Installer script started" | Add-Content - Path $DebugLogFile - Force
69+ " [$timestamp ] Running as user: $env: USERNAME " | Add-Content - Path $DebugLogFile - Force
70+ " [$timestamp ] Log file: $DebugLogFile " | Add-Content - Path $DebugLogFile - Force
71+ " [$timestamp ] =====================================" | Add-Content - Path $DebugLogFile - Force
72+
73+ # Display log location prominently
74+ Write-Host " "
75+ Write-Host " ========================================" - ForegroundColor Cyan
76+ Write-Host " Installation Debug Log: $DebugLogFile " - ForegroundColor Cyan
77+ Write-Host " ========================================" - ForegroundColor Cyan
78+ Write-Host " "
79+ } catch {
80+ Write-Warning " Failed to initialize debug log file: $_ "
81+ }
82+
2283# Check for admin rights
2384function Check-Administrator {
2485 $user = [Security.Principal.WindowsIdentity ]::GetCurrent()
2586 (New-Object Security.Principal.WindowsPrincipal $user ).IsInRole([Security.Principal.WindowsBuiltinRole ]::Administrator)
2687}
2788
89+ Write-DebugLog " Starting New Relic Infrastructure Agent installer"
90+ Write-DebugLog " Debug log file: $DebugLogFile "
91+
2892if (-Not (Check- Administrator)) {
93+ Write-DebugLog " ERROR: Admin permission check failed"
2994 Write-Error " Admin permission is required. Please, open a Windows PowerShell session with administrative rights."
3095 exit 1
3196}
97+ Write-DebugLog " Admin permission check passed"
3298
3399# The priority is:
34100# 1 Command-line parameter
35101# 2 Environment variable
36102# 3 Default value
37103# if (-Not $LicenseKey) { echo "no license key provided"; exit -1}
38- if (-Not $AgentDir ) { $AgentDir = [IO.Path ]::Combine($env: ProgramFiles , ' New Relic\newrelic-infra' ) }
104+ # Use ProgramW6432 to ensure 64-bit path even when called from 32-bit process
105+ $programFilesPath = if ($env: ProgramW6432 ) { $env: ProgramW6432 } else { $env: ProgramFiles }
106+ if (-Not $AgentDir ) { $AgentDir = [IO.Path ]::Combine($programFilesPath , ' New Relic\newrelic-infra' ) }
39107if (-Not $LogFile ) { $LogFile = [IO.Path ]::Combine($AgentDir , ' newrelic-infra.log' ) }
40108if (-Not $PluginDir ) { $PluginDir = [IO.Path ]::Combine($AgentDir , ' integrations.d' ) }
41109if (-Not $ConfigFile ) { $ConfigFile = [IO.Path ]::Combine($AgentDir , ' newrelic-infra.yml' ) }
42110if (-Not $AppDataDir ) { $AppDataDir = [IO.Path ]::Combine($env: ProgramData , ' New Relic\newrelic-infra' ) }
43111if (-Not $ServiceName ) { $ServiceName = ' newrelic-infra' }
44112
113+ Write-DebugLog " Configuration parameters:"
114+ Write-DebugLog " AgentDir: $AgentDir "
115+ Write-DebugLog " LogFile: $LogFile "
116+ Write-DebugLog " PluginDir: $PluginDir "
117+ Write-DebugLog " ConfigFile: $ConfigFile "
118+ Write-DebugLog " AppDataDir: $AppDataDir "
119+ Write-DebugLog " ServiceName: $ServiceName "
120+ Write-DebugLog " ServiceOverwrite: $ServiceOverwriteBool "
121+
45122# Check if service already exists
46123$existingService = Get-Service $ServiceName - ErrorAction SilentlyContinue
47124$isUpgrade = $false
48125$preservedAccount = $null
49126
127+ Write-DebugLog " Checking for existing service: $ServiceName "
50128if ($existingService ) {
51- Write-Host " Service $ServiceName already exists. Performing upgrade... "
129+ Write-DebugLog " Service $ServiceName found - already exists "
52130
53131 # Get the existing service account before stopping
54132 $existingServiceWMI = Get-WmiObject - Class Win32_Service - Filter " name='$ServiceName '"
55133 if ($existingServiceWMI ) {
56134 $preservedAccount = $existingServiceWMI.StartName
57- Write-Host " Existing service runs as: $preservedAccount "
135+ Write-DebugLog " Existing service runs as: $preservedAccount "
58136 }
59137
60138 # Stop the service if running
61139 if ($existingService.Status -eq ' Running' ) {
62- Write-Host " Stopping service $ServiceName ..."
140+ Write-DebugLog " Stopping service $ServiceName ..."
63141 Stop-Service $ServiceName - Force | Out-Null
64142 Start-Sleep - Seconds 2
143+ Write-DebugLog " Service stopped successfully"
65144 }
66145
67- $isUpgrade = $true
146+ if ($ServiceOverwriteBool -eq $true ) {
147+ Write-DebugLog " ServiceOverwrite flag is set - removing existing service $ServiceName ..."
148+ if ($existingServiceWMI ) {
149+ $existingServiceWMI.delete () | Out-Null
150+ Write-DebugLog " Service removed. Performing fresh installation..."
151+ # Treat as fresh install, not upgrade
152+ $isUpgrade = $false
153+ $preservedAccount = $null
154+ } else {
155+ Write-DebugLog " ERROR: Failed to find service to remove"
156+ }
157+ } else {
158+ Write-DebugLog " ServiceOverwrite flag not set - upgrading existing service $ServiceName ..."
159+ $isUpgrade = $true
160+ }
161+
162+
68163} else {
69- Write-Host " Service $ServiceName does not exist. Performing fresh installation... "
164+ Write-DebugLog " Service $ServiceName not found - performing fresh installation"
70165}
71166
72167
73168
74169if (Test-Path " $AgentDir \newrelic-infra.exe" ) {
75170 $versionOutput = & " $AgentDir \newrelic-infra.exe" - version
76- " Installing $versionOutput "
171+ Write-DebugLog " Installing $versionOutput "
77172}
78173Write-Host - NoNewline " Using the following configuration..."
79174[PSCustomObject ] @ {
@@ -83,12 +178,15 @@ Write-Host -NoNewline "Using the following configuration..."
83178 ConfigFile = $ConfigFile
84179 AppDataDir = $AppDataDir
85180 ServiceName = $ServiceName
181+ ServiceOverwrite = $ServiceOverwriteBool
86182} | Format-List
87183
88184Function Create-Directory ($dir ) {
89185 if (-Not (Test-Path - Path $dir )) {
90- " Creating $dir "
186+ Write-DebugLog " Creating directory: $dir "
91187 New-Item - ItemType directory - Path $dir | Out-Null
188+ } else {
189+ Write-DebugLog " Directory already exists: $dir "
92190 }
93191}
94192
@@ -100,7 +198,7 @@ $ScriptPath = Get-ScriptDirectory
100198
101199# Create directories only for fresh installation
102200if (-not $isUpgrade ) {
103- " Creating directories... "
201+ Write-DebugLog " Creating directories for fresh installation "
104202 Create- Directory $AgentDir
105203 Create- Directory $AgentDir \custom- integrations
106204 Create- Directory $AgentDir \newrelic- integrations
@@ -112,17 +210,18 @@ if (-not $isUpgrade) {
112210 Create- Directory $PluginDir
113211 Create- Directory $AppDataDir
114212} else {
115- Write-Host " Upgrade detected - skipping directory creation"
213+ Write-DebugLog " Upgrade detected - skipping directory creation"
116214}
117215
118- " Copying executables to $AgentDir ... "
216+ Write-DebugLog " Copying executables from $ScriptPath to $AgentDir "
119217Copy-Item - Path " $ScriptPath \*.exe" - Destination " $AgentDir " - Force
218+ Write-DebugLog " Executables copied successfully"
120219
121220# For upgrades, only update config if it doesn't exist
122221if ($isUpgrade -and (Test-Path $ConfigFile )) {
123- Write-Host " Preserving existing configuration file: $ConfigFile "
222+ Write-DebugLog " Preserving existing configuration file: $ConfigFile "
124223} else {
125- " Creating config file in $ConfigFile "
224+ Write-DebugLog " Creating new config file in $ConfigFile "
126225 Clear-Content - Path $ConfigFile - ErrorAction SilentlyContinue
127226 Add-Content - Path $ConfigFile - Value `
128227 " license_key: $LicenseKey " ,
@@ -133,36 +232,44 @@ if ($isUpgrade -and (Test-Path $ConfigFile)) {
133232
134233if ($isUpgrade ) {
135234 # Upgrade scenario: restart service with preserved account
136- Write-Host " Restarting service with preserved account: $preservedAccount "
235+ Write-DebugLog " Upgrade path: Restarting service with preserved account: $preservedAccount "
137236
138237 # Grant permissions if using a custom service account (not LocalSystem)
139238 if ($preservedAccount -and $preservedAccount -ne " LocalSystem" ) {
140- Write-Host " Granting permissions to $preservedAccount on data directories ..."
239+ Write-DebugLog " Granting permissions to $preservedAccount on $AppDataDir ..."
141240 $username = $preservedAccount -replace ' ^\.\\' # Remove .\ prefix if present
142241 icacls " $AppDataDir " / grant " ${username} :(OI)(CI)F" / T / Q | Out-Null
242+ Write-DebugLog " Permissions granted successfully"
143243 }
144244
145245 try {
246+ Write-DebugLog " Starting service: $ServiceName "
146247 Start-Service - Name $ServiceName - ErrorAction Stop
147- " Upgrade completed successfully!"
248+ Write-DebugLog " Upgrade completed successfully!"
148249 } catch {
149- Write-Warning " Failed to restart service: $_ "
250+ Write-DebugLog " ERROR: Failed to restart service: $_ "
150251 exit 1
151252 }
152253} else {
153254 # Fresh installation scenario: create service with LocalSystem
255+ Write-DebugLog " Creating new service: $ServiceName "
256+ Write-DebugLog " Binary path: $AgentDir \newrelic-infra-service.exe -config $ConfigFile "
154257 New-Service - Name $ServiceName - DisplayName ' New Relic Infrastructure Agent' - BinaryPathName " $AgentDir \newrelic-infra-service.exe -config $ConfigFile " - StartupType Automatic | Out-Null
155258
156259 $serviceCreated = Get-Service - Name $ServiceName - ErrorAction SilentlyContinue
157260 if ($serviceCreated ) {
261+ Write-DebugLog " Service created successfully"
158262 try {
263+ Write-DebugLog " Starting service: $ServiceName "
159264 Start-Service - Name $ServiceName - ErrorAction Stop
160- " Installation completed!"
265+ Write-DebugLog " Installation completed successfully !"
161266 } catch {
162- Write-Warning " Failed to start service: $_ "
267+ Write-DebugLog " ERROR: Failed to start service: $_ "
163268 }
164269 } else {
165- " error creating service $ServiceName "
270+ Write-DebugLog " ERROR: Failed to create service $ServiceName "
166271 exit 1
167272 }
168273}
274+
275+ Write-DebugLog " Installer script finished"
0 commit comments