Improved check-apps.ps1

This commit is contained in:
Markus Fleschutz 2024-09-10 12:58:30 +02:00
parent 8f7237196c
commit 4d3369babf

View File

@ -5,50 +5,47 @@
This PowerShell script queries the installed applications and prints it. This PowerShell script queries the installed applications and prints it.
.EXAMPLE .EXAMPLE
PS> ./check-apps.ps1 PS> ./check-apps.ps1
119 Windows apps installed, 11 upgrades available 150 Win apps installed, 72 upgrades available, 5 crash dump(s) found
.LINK .LINK
https://github.com/fleschutz/PowerShell https://github.com/fleschutz/PowerShell
.NOTES .NOTES
Author: Markus Fleschutz | License: CC0 Author: Markus Fleschutz | License: CC0
#> #>
function GetCrashDumps { function CountCrashDumps {
[string]$path = Resolve-Path -Path "~\AppData\Local\CrashDumps" [string]$path = Resolve-Path -Path "~\AppData\Local\CrashDumps"
$count = 0
$files = (Get-ChildItem -path "$path\*.dmp" -attributes !Directory) $files = (Get-ChildItem -path "$path\*.dmp" -attributes !Directory)
foreach($file in $files) { $count++ } return $files.Count
return $count
} }
try { try {
$statusIcon = "" $status = ""
$statusMsg = ""
if ($IsLinux) { if ($IsLinux) {
Write-Progress "Querying installed applications..." Write-Progress "Querying installed applications..."
$numPkgs = (apt list --installed 2>/dev/null).Count $numPkgs = (apt list --installed 2>/dev/null).Count
$numSnaps = (snap list).Count - 1 $numSnaps = (snap list).Count - 1
Write-Progress -completed "Done." Write-Progress -completed "Done."
$statusMsg += "$numPkgs Debian packages, $numSnaps snaps installed" $reply = "$numPkgs Debian packages, $numSnaps snaps installed"
} else { } else {
Write-Progress "Querying installed apps..." Write-Progress "Querying installed apps..."
$Apps = Get-AppxPackage $apps = Get-AppxPackage
Write-Progress -completed "Done." Write-Progress -completed "Done."
$statusMsg = "$($Apps.Count) Win apps installed" $reply = "$($apps.Count) Win apps installed"
[int]$numNonOk = 0 [int]$numNonOk = 0
foreach($App in $Apps) { if ($App.Status -ne "Ok") { $numNonOk++ } } foreach($app in $apps) { if ($app.Status -ne "Ok") { $numNonOk++ } }
if ($numNonOk -gt 0) { $statusIcon = "⚠️"; $statusMsg += ", $numNonOk non-ok" } if ($numNonOk -gt 0) { $status = "⚠️"; $reply += ", $numNonOk non-ok" }
[int]$numErrors = (Get-AppxLastError) [int]$numErrors = (Get-AppxLastError)
if ($numErrors -gt 0) { $statusIcon = "⚠️"; $statusMsg += ", $numErrors errors" } if ($numErrors -gt 0) { $status = "⚠️"; $reply += ", $numErrors errors" }
$numUpdates = (winget upgrade --include-unknown).Count - 5 $numUpdates = (winget upgrade --include-unknown).Count - 5
$statusMsg += ", $numUpdates upgrades available" $reply += ", $numUpdates upgrades available"
$crashDumps = GetCrashDumps $numCrashDumps = CountCrashDumps
if ($crashDumps -ne 0) { $statusIcon = "⚠️"; $statusMsg += " - found $crashDumps crash dump(s)" } if ($numCrashDumps -ne 0) { $status = "⚠️"; $reply += ", $numCrashDumps crash dump(s) found" }
} }
Write-Host "$statusIcon $statusMsg" Write-Host "$status $reply"
exit 0 # success exit 0 # success
} catch { } catch {
"⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])" "⚠️ Error in line $($_.InvocationInfo.ScriptLineNumber): $($Error[0])"