Public/Uninstall-AdobeFlashPlayer.ps1
|
function Uninstall-AdobeFlashPlayer { <# .SYNOPSIS Forces removal of Adobe Flash Player components from Windows. .DESCRIPTION Copies the Adobe Flash uninstaller to a local staging path, verifies its hash against the source, resets ownership and permissions on the Macromed\Flash directories so the uninstaller can run, executes the uninstaller, then removes residual Flash files from system directories and all user profiles. .INPUTS None. Parameters must be supplied directly. .OUTPUTS None. .PARAMETER UninstallerPath Path to the Adobe Flash Player uninstaller executable. May be a local path or a UNC path accessible from the target machine. .PARAMETER ComputerName The target computer. Defaults to the local machine. .EXAMPLE Uninstall-AdobeFlashPlayer -UninstallerPath '\\server\share\uninstall_flash_player.exe' Removes Flash Player from the local machine using the uninstaller at the specified UNC path. .EXAMPLE Uninstall-AdobeFlashPlayer -UninstallerPath '\\server\share\uninstall_flash_player.exe' -ComputerName 'Workstation01' Removes Flash Player from Workstation01. The UNC path must be accessible from Workstation01. .NOTES Requires Administrator privileges. For remote operations, UninstallerPath must be accessible from the target machine. Remote operations require WinRM to be configured on the target machine. #> [CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'High')] [OutputType([void])] param ( [Parameter(Mandatory = $true, Position = 0)] [string]$UninstallerPath, [Parameter(Mandatory = $false)] [string]$ComputerName = $env:COMPUTERNAME ) $isLocal = ($ComputerName -ieq $env:COMPUTERNAME) -or ($ComputerName -ieq 'localhost') -or ($ComputerName -eq '127.0.0.1') if ($PSCmdlet.ShouldProcess($ComputerName, 'Uninstall Adobe Flash Player and remove all residual files')) { $work = { param($srcPath) $localCopy = Join-Path $env:TEMP 'uninstall_flash_player.exe' if ($srcPath -like '\\*') { Copy-Item -Path $srcPath -Destination $localCopy -Force -ErrorAction Stop } else { $localCopy = $srcPath } $srcHash = (Get-FileHash -Path $srcPath -ErrorAction Stop).Hash $dstHash = (Get-FileHash -Path $localCopy -ErrorAction Stop).Hash if ($srcHash -ne $dstHash) { Write-Error "Hash mismatch for uninstaller copy. Aborting to prevent running a corrupted binary." return } foreach ($flashDir in @('C:\Windows\SysWOW64\Macromed\Flash', 'C:\Windows\System32\Macromed\Flash')) { if (Test-Path $flashDir) { $acl = Get-Acl -Path $flashDir $systemAccount = New-Object System.Security.Principal.NTAccount('NT AUTHORITY\SYSTEM') $acl.SetOwner($systemAccount) $accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule( 'NT AUTHORITY\SYSTEM', 'FullControl', 'ContainerInherit,ObjectInherit', 'None', 'Allow' ) $acl.SetAccessRule($accessRule) Set-Acl -Path $flashDir -AclObject $acl Get-ChildItem -Path $flashDir -Recurse | ForEach-Object { $itemAcl = Get-Acl -Path $_.FullName $itemAcl.SetOwner($systemAccount) $itemAcl.SetAccessRule($accessRule) Set-Acl -Path $_.FullName -AclObject $itemAcl Set-ItemProperty -Path $_.FullName -Name IsReadOnly -Value $false -ErrorAction SilentlyContinue } } } Start-Process -FilePath $localCopy -ArgumentList '/uninstall' -Wait -ErrorAction Stop Remove-Item -Path 'C:\Windows\System32\Macromed\Flash' -Recurse -Force -ErrorAction SilentlyContinue Remove-Item -Path 'C:\Windows\SysWOW64\Macromed\Flash' -Recurse -Force -ErrorAction SilentlyContinue Get-ChildItem -Path 'C:\Users' -Directory | ForEach-Object { $userName = $_.Name Remove-Item -Path "C:\Users\$userName\AppData\Roaming\Adobe\Flash Player" -Recurse -Force -ErrorAction SilentlyContinue Remove-Item -Path "C:\Users\$userName\AppData\Roaming\Macromedia\Flash Player" -Recurse -Force -ErrorAction SilentlyContinue } if ($srcPath -like '\\*') { Remove-Item -Path $localCopy -Force -ErrorAction SilentlyContinue } } if ($isLocal) { & $work $UninstallerPath } else { Invoke-Command -ComputerName $ComputerName -ScriptBlock $work -ArgumentList $UninstallerPath } Write-Verbose "Adobe Flash Player removal completed on '$ComputerName'." } } |