fleet/ee/maintained-apps/inputs/winget/scripts/notion_uninstall.ps1
Allen Houchins f5510277c8
Add Notion as a Windows FMA (#37226)
This pull request adds support for the Notion app on Windows to the
maintained apps system. It introduces new metadata, installation and
uninstallation scripts, and updates the relevant app listings and icon.
The most important changes are grouped below:

**Notion for Windows support:**

* Added a new input metadata file for Notion on Windows at
`ee/maintained-apps/inputs/winget/notion.json`, specifying installer
details, categories, and script paths.
* Added a new output app listing for Notion on Windows in
`ee/maintained-apps/outputs/apps.json` and created a dedicated manifest
at `ee/maintained-apps/outputs/notion/windows.json` with version,
installer, uninstall script references, and detection query.
[[1]](diffhunk://#diff-4c1446cfc02c6bb0bda874481e333c65b84e184fcea52f656b49a6489f73c9c2R977-R983)
[[2]](diffhunk://#diff-1dfe0659e83ec343ffcf27344c2dd5b257cf0bcc11434614acdc1820badda243R1-R21)
* Implemented install and uninstall PowerShell scripts for Notion on
Windows, referenced by the new manifest.

**Uninstallation improvements:**

* Updated the Notion uninstall script to ensure the `/S` flag is
included for silent uninstalls with NSIS installers, improving
reliability of unattended uninstalls.
[[1]](diffhunk://#diff-fe66fb3f5c34301a1a22112997ca0aa38ec0ccd9dc97db4cf2a845a8a8db6df1R36-R45)
[[2]](diffhunk://#diff-1dfe0659e83ec343ffcf27344c2dd5b257cf0bcc11434614acdc1820badda243R1-R21)

**UI/Icon update:**

* Replaced the SVG icon for Notion in the frontend with a PNG-based
image for improved appearance or compatibility.

**Other:**

* Clarified the description for Notion Calendar in the app listings for
better user understanding.
2025-12-12 23:00:35 -06:00

152 lines
4.7 KiB
PowerShell

$softwareName = "notion"
$productKey = "661f0cc6-343a-59cb-a5e8-8f6324cc6998"
$taskName = "fleet-uninstall-$softwareName"
$scriptPath = "$env:PUBLIC\uninstall-$softwareName.ps1"
$logFile = "$env:PUBLIC\uninstall-output-$softwareName.txt"
$exitCodeFile = "$env:PUBLIC\uninstall-exitcode-$softwareName.txt"
# Embedded uninstall script
$userScript = @"
`$userKey = "HKCU:\Software\Microsoft\Windows\CurrentVersion\Uninstall\$productKey"
`$exitCode = 0
`$logFile = "$logFile"
`$exitCodeFile = "$exitCodeFile"
Start-Transcript -Path "$logFile" -Append
try {
`$key = Get-ItemProperty -Path `$userKey -ErrorAction Stop
`$uninstallCommand = if (`$key.QuietUninstallString) {
`$key.QuietUninstallString
} else {
`$key.UninstallString
}
`$splitArgs = `$uninstallCommand.Split('"')
if (`$splitArgs.Length -gt 1) {
if (`$splitArgs.Length -eq 3) {
`$uninstallArgs = `$splitArgs[2].Trim()
} elseif (`$splitArgs.Length -gt 3) {
Throw "Uninstall command contains multiple quoted strings. Please update the uninstall script.`nUninstall command: `$uninstallCommand"
}
`$uninstallCommand = `$splitArgs[1]
}
# NSIS installers require /S flag for silent uninstall
# Append /S if not already present in the uninstall args
if (`$uninstallArgs) {
if (`$uninstallArgs -notmatch '\b/S\b') {
`$uninstallArgs = "`$uninstallArgs /S".Trim()
}
} else {
`$uninstallArgs = "/S"
}
Write-Host "Uninstall command: `$uninstallCommand"
Write-Host "Uninstall args: `$uninstallArgs"
`$processOptions = @{
FilePath = `$uninstallCommand
PassThru = `$true
Wait = `$true
}
if (`$uninstallArgs -ne '') {
`$processOptions.ArgumentList = "`$uninstallArgs"
}
`$process = Start-Process @processOptions
`$exitCode = `$process.ExitCode
Write-Host "Uninstall exit code: `$exitCode"
}
catch {
Write-Host "Error: `$_.Exception.Message"
`$exitCode = 1
}
finally {
Set-Content -Path `$exitCodeFile -Value `$exitCode
}
Stop-Transcript
Exit `$exitCode
"@
$exitCode = 0
try {
# Wait for an interactive user to be logged on
while ($true) {
$userName = (Get-CimInstance Win32_ComputerSystem).UserName
if ($userName -and $userName -like "*\*") {
Write-Output "Interactive user detected: $userName"
break
} else {
Start-Sleep -Seconds 5
}
}
# Write the uninstall script to disk
Set-Content -Path $scriptPath -Value $userScript -Force
# Build task action: run script, redirect stdout/stderr to log file
$action = New-ScheduledTaskAction -Execute "powershell.exe" `
-Argument "-WindowStyle Hidden -ExecutionPolicy Bypass -File `"$scriptPath`" *> `"$logFile`" 2>&1"
$trigger = New-ScheduledTaskTrigger -AtLogOn
$settings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries
$principal = New-ScheduledTaskPrincipal -UserId $userName -RunLevel Highest
$task = New-ScheduledTask -Action $action -Trigger $trigger -Settings $settings -Principal $principal
Register-ScheduledTask -TaskName $taskName -InputObject $task -User $userName -Force
# Start the task
Start-ScheduledTask -TaskName $taskName
# Wait for it to start
$startDate = Get-Date
$state = (Get-ScheduledTask -TaskName $taskName).State
while ($state -ne "Running") {
Start-Sleep -Seconds 1
$elapsed = (New-Timespan -Start $startDate).TotalSeconds
if ($elapsed -gt 120) { throw "Timeout waiting for task to start." }
$state = (Get-ScheduledTask -TaskName $taskName).State
}
# Wait for it to complete
while ($state -eq "Running") {
Start-Sleep -Seconds 5
$elapsed = (New-Timespan -Start $startDate).TotalSeconds
if ($elapsed -gt 120) { throw "Timeout waiting for task to finish." }
$state = (Get-ScheduledTask -TaskName $taskName).State
}
# Show task output
if (Test-Path $logFile) {
Write-Host "`n--- Scheduled Task Output ---"
Get-Content $logFile | Write-Host
}
if (Test-Path $exitCodeFile) {
$exitCode = Get-Content $exitCodeFile
Write-Host "`nScheduled task exit code: $exitCode"
}
} catch {
Write-Host "Error: $_"
$exitCode = 1
} finally {
# Clean up
Write-Host "Cleaning up..."
Unregister-ScheduledTask -TaskName $taskName -Confirm:$false -ErrorAction SilentlyContinue
Remove-Item -Path $scriptPath -Force -ErrorAction SilentlyContinue
Remove-Item -Path $logFile -Force -ErrorAction SilentlyContinue
Remove-Item -Path $exitCodeFile -Force -ErrorAction SilentlyContinue
}
Exit $exitCode