|
| 1 | +<# |
| 2 | +.Synopsis |
| 3 | + Software Install and Removal Detection - Reports new installs and removals without considering version numbers. |
| 4 | +.DESCRIPTION |
| 5 | + This script compares the current installed software list from the registry with a previous state. |
| 6 | +.VERSION |
| 7 | + v1.0 11/23/2024 |
| 8 | +#> |
| 9 | + |
| 10 | +Function Foldercreate { |
| 11 | + param ( |
| 12 | + [Parameter(Mandatory = $false)] |
| 13 | + [String[]]$Paths |
| 14 | + ) |
| 15 | + |
| 16 | + foreach ($Path in $Paths) { |
| 17 | + if (!(Test-Path $Path)) { |
| 18 | + New-Item -ItemType Directory -Force -Path $Path |
| 19 | + } |
| 20 | + } |
| 21 | +} |
| 22 | +Foldercreate -Paths "$env:ProgramData\TacticalRMM\temp", "$env:ProgramData\TacticalRMM\logs" |
| 23 | + |
| 24 | +# Define file paths |
| 25 | +$previousStateFile = "$env:ProgramData\TacticalRMM\installed_software.json" |
| 26 | +$logFile = "$env:ProgramData\TacticalRMM\logs\software_changes.log" |
| 27 | + |
| 28 | +# Function to get installed software from the registry |
| 29 | +function Get-InstalledSoftware { |
| 30 | + $installedSoftware = @() |
| 31 | + |
| 32 | + # Get software from 64-bit and 32-bit registry paths |
| 33 | + $installedSoftware += Get-ItemProperty 'HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*' | |
| 34 | + Select-Object DisplayName, DisplayVersion |
| 35 | + $installedSoftware += Get-ItemProperty 'HKLM:\Software\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*' | |
| 36 | + Select-Object DisplayName, DisplayVersion |
| 37 | + |
| 38 | + # Filter out entries without a valid DisplayName |
| 39 | + $installedSoftware = $installedSoftware | Where-Object { $_.DisplayName -ne $null -and $_.DisplayName -ne '' } |
| 40 | + |
| 41 | + # Strip version number patterns from DisplayName and remove duplicates |
| 42 | + $installedSoftware = $installedSoftware | ForEach-Object { |
| 43 | + if ($_.DisplayVersion -and $_.DisplayName -like "*$($_.DisplayVersion)*") { |
| 44 | + $_.DisplayName = $_.DisplayName -replace [regex]::Escape($_.DisplayVersion), '' # Strip DisplayVersion |
| 45 | + $_.DisplayName = $_.DisplayName.Trim() # Remove trailing spaces |
| 46 | + } |
| 47 | + $_ |
| 48 | + } | Sort-Object DisplayName -Unique |
| 49 | + |
| 50 | + return $installedSoftware |
| 51 | +} |
| 52 | + |
| 53 | +# Function to log changes to a file, ensuring proper logging |
| 54 | +function LogChange { |
| 55 | + param([string]$message) |
| 56 | + $timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss" |
| 57 | + $logEntry = "$timestamp - $message" |
| 58 | + |
| 59 | + # Write the log entry to the file |
| 60 | + Add-Content -Path $logFile -Value $logEntry |
| 61 | +} |
| 62 | + |
| 63 | +# Get current installed software |
| 64 | +$currentSoftware = Get-InstalledSoftware |
| 65 | + |
| 66 | +# Check if the previous state file exists |
| 67 | +if (Test-Path $previousStateFile) { |
| 68 | + # Load the previous state |
| 69 | + $previousSoftware = Get-Content $previousStateFile | ConvertFrom-Json |
| 70 | + |
| 71 | + # Compare current and previous software lists |
| 72 | + $newSoftware = Compare-Object -ReferenceObject $previousSoftware -DifferenceObject $currentSoftware -Property DisplayName -PassThru | |
| 73 | + Where-Object { $_.SideIndicator -eq '=>' } |
| 74 | + |
| 75 | + $removedSoftware = Compare-Object -ReferenceObject $previousSoftware -DifferenceObject $currentSoftware -Property DisplayName -PassThru | |
| 76 | + Where-Object { $_.SideIndicator -eq '<=' } |
| 77 | + |
| 78 | + # Report new installs |
| 79 | + if ($newSoftware) { |
| 80 | + Write-Output "New software installed:" |
| 81 | + $newSoftware | ForEach-Object { |
| 82 | + Write-Output " - $($_.DisplayName)" |
| 83 | + LogChange "Installed: $($_.DisplayName)" |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + # Report removals |
| 88 | + if ($removedSoftware) { |
| 89 | + Write-Output "The following software(s) were removed:" |
| 90 | + $removedSoftware | ForEach-Object { |
| 91 | + Write-Output " - $($_.DisplayName)" |
| 92 | + LogChange "Removed: $($_.DisplayName)" |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + # Save the current state (overwrite the existing file) |
| 97 | + $currentSoftware | ConvertTo-Json | Out-File -FilePath $previousStateFile -Encoding UTF8 |
| 98 | + |
| 99 | + # Exit with status code based on changes |
| 100 | + if ($newSoftware -or $removedSoftware) { |
| 101 | + exit 1 |
| 102 | + } |
| 103 | + else { |
| 104 | + Write-Output "No new software installations or removals detected." |
| 105 | + exit 0 |
| 106 | + } |
| 107 | +} |
| 108 | +else { |
| 109 | + # Save the current state if no previous state exists (overwrite if needed) |
| 110 | + $currentSoftware | ConvertTo-Json | Out-File -FilePath $previousStateFile -Encoding UTF8 |
| 111 | + LogChange "Initial software inventory saved." |
| 112 | + Write-Output "Initial software inventory saved." |
| 113 | + exit 0 |
| 114 | +} |
0 commit comments