Skip to content

Commit

Permalink
Updated PS1 install/uninstall scripts to fail on error. (#22164)
Browse files Browse the repository at this point in the history
  • Loading branch information
getvictor authored Sep 17, 2024
1 parent b11edd9 commit ddbdce4
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 2 deletions.
7 changes: 7 additions & 0 deletions pkg/file/scripts/install_exe.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

$exeFilePath = "${env:INSTALLER_PATH}"

try {

# Add argument to install silently
# Argument to make install silent depends on installer,
# each installer might use different argument (usually it's "/S" or "/s")
Expand All @@ -20,3 +22,8 @@ $exitCode = $process.ExitCode
# Prints the exit code
Write-Host "Install exit code: $exitCode"
Exit $exitCode

} catch {
Write-Host "Error: $_"
Exit 1
}
9 changes: 8 additions & 1 deletion pkg/file/scripts/install_msi.ps1
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
$logFile = "${env:TEMP}/fleet-install-software.log"

try {

$installProcess = Start-Process msiexec.exe `
-ArgumentList "/quiet /norestart /lv ${logFile} /i `"${env:INSTALLER_PATH}`"" `
-PassThru -Verb RunAs -Wait

Get-Content $logFile -Tail 500

exit $installProcess.ExitCode
Exit $installProcess.ExitCode

} catch {
Write-Host "Error: $_"
Exit 1
}
31 changes: 31 additions & 0 deletions pkg/file/scripts/uninstall_exe.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ $machineKey = `
'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*'
$machineKey32on64 = `
'HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*'

$exitCode = 0

try {

[array]$uninstallKeys = Get-ChildItem `
-Path @($machineKey, $machineKey32on64) `
-ErrorAction SilentlyContinue |
Expand All @@ -33,6 +38,24 @@ foreach ($key in $uninstallKeys) {
$key.UninstallString
}

# The uninstall command may contain command and args, like:
# "C:\Program Files\Software\uninstall.exe" --uninstall --silent
# Split the command and args
$splitArgs = $uninstallCommand.Split('"')
if ($splitArgs.Length -gt 1) {
if ($splitArgs.Length -eq 3) {
$uninstallArgs = "$( $splitArgs[2] ) $uninstallArgs".Trim()
} elseif ($splitArgs.Length -gt 3) {
Throw `
"Uninstall command contains multiple quoted strings. " +
"Please update the uninstall script.`n" +
"Uninstall command: $uninstallCommand"
}
$uninstallCommand = $splitArgs[1]
}
Write-Host "Uninstall command: $uninstallCommand"
Write-Host "Uninstall args: $uninstallArgs"

$processOptions = @{
FilePath = $uninstallCommand
PassThru = $true
Expand All @@ -55,6 +78,14 @@ foreach ($key in $uninstallKeys) {

if (-not $foundUninstaller) {
Write-Host "Uninstaller for '$softwareName' not found."
# Change exit code to 0 if you don't want to fail if uninstaller is not
# found. This could happen if program was already uninstalled.
$exitCode = 1
}

} catch {
Write-Host "Error: $_"
$exitCode = 1
}

Exit $exitCode
7 changes: 7 additions & 0 deletions pkg/file/testdata/scripts/install_exe.ps1.golden
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

$exeFilePath = "${env:INSTALLER_PATH}"

try {

# Add argument to install silently
# Argument to make install silent depends on installer,
# each installer might use different argument (usually it's "/S" or "/s")
Expand All @@ -20,3 +22,8 @@ $exitCode = $process.ExitCode
# Prints the exit code
Write-Host "Install exit code: $exitCode"
Exit $exitCode

} catch {
Write-Host "Error: $_"
Exit 1
}
9 changes: 8 additions & 1 deletion pkg/file/testdata/scripts/install_msi.ps1.golden
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
$logFile = "${env:TEMP}/fleet-install-software.log"

try {

$installProcess = Start-Process msiexec.exe `
-ArgumentList "/quiet /norestart /lv ${logFile} /i `"${env:INSTALLER_PATH}`"" `
-PassThru -Verb RunAs -Wait

Get-Content $logFile -Tail 500

exit $installProcess.ExitCode
Exit $installProcess.ExitCode

} catch {
Write-Host "Error: $_"
Exit 1
}
31 changes: 31 additions & 0 deletions pkg/file/testdata/scripts/uninstall_exe.ps1.golden
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ $machineKey = `
'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*'
$machineKey32on64 = `
'HKLM:\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*'

$exitCode = 0

try {

[array]$uninstallKeys = Get-ChildItem `
-Path @($machineKey, $machineKey32on64) `
-ErrorAction SilentlyContinue |
Expand All @@ -33,6 +38,24 @@ foreach ($key in $uninstallKeys) {
$key.UninstallString
}

# The uninstall command may contain command and args, like:
# "C:\Program Files\Software\uninstall.exe" --uninstall --silent
# Split the command and args
$splitArgs = $uninstallCommand.Split('"')
if ($splitArgs.Length -gt 1) {
if ($splitArgs.Length -eq 3) {
$uninstallArgs = "$( $splitArgs[2] ) $uninstallArgs".Trim()
} elseif ($splitArgs.Length -gt 3) {
Throw `
"Uninstall command contains multiple quoted strings. " +
"Please update the uninstall script.`n" +
"Uninstall command: $uninstallCommand"
}
$uninstallCommand = $splitArgs[1]
}
Write-Host "Uninstall command: $uninstallCommand"
Write-Host "Uninstall args: $uninstallArgs"

$processOptions = @{
FilePath = $uninstallCommand
PassThru = $true
Expand All @@ -55,6 +78,14 @@ foreach ($key in $uninstallKeys) {

if (-not $foundUninstaller) {
Write-Host "Uninstaller for '$softwareName' not found."
# Change exit code to 0 if you don't want to fail if uninstaller is not
# found. This could happen if program was already uninstalled.
$exitCode = 1
}

} catch {
Write-Host "Error: $_"
$exitCode = 1
}

Exit $exitCode

0 comments on commit ddbdce4

Please sign in to comment.