Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cherry-pick: Updated PS1 install/uninstall scripts to fail on error. (#22164) #22203

Merged
merged 1 commit into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading