-
-
Save bcrpntr/32f1038bd7b90c7e495e8884d418ef1e to your computer and use it in GitHub Desktop.
Remove-Sophos.ps1
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Logging variables | |
| $LogDirectory = 'C:\Users\Public\Logs' | |
| $LogPath = 'C:\Users\Public\Logs\TFS_scripted_updates.log' | |
| # Set download variables | |
| $DownloadURL = '' | |
| $ZapFile = 'C:\temp\SophosZap.exe' | |
| # Create log folder if it doesn't exist | |
| If (-Not (Test-Path -Path $LogDirectory)) { | |
| New-Item -Path $LogDirectory -ItemType Directory | Out-Null | |
| } | |
| # Create log file if it doesn't exist | |
| If (-Not (Test-Path -Path $LogPath)) { | |
| New-Item -Name 'TFS_scripted_updates.log' -Path $LogDirectory | Out-Null | |
| } | |
| If (-Not (Test-Path -Path 'C:\temp')) { | |
| New-Item -Path 'C:\temp' -ItemType Directory | Out-Null | |
| } | |
| # Set local variables | |
| $InstalledSoftware = Get-ItemProperty 'HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*' | Select-Object DisplayName, DisplayVersion, Publisher, InstallDate | |
| $InstalledSoftware += Get-ItemProperty 'HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*' | Select-Object DisplayName, DisplayVersion, Publisher, InstallDate | |
| $LogData = Get-Content -Path $LogPath | |
| $LogCount = ($LogData -match "SophosZap2").count | |
| # Download uninstaller | |
| Function Get-SophosZap { | |
| If (($InstalledSoftware -match "Sophos") -and ($LogCount -le 1) -and -Not (Test-Path $ZapFile)) { | |
| Try { | |
| Invoke-WebRequest -Uri $DownloadURL -OutFile $ZapFile -UseBasicParsing | |
| } | |
| Catch { | |
| Add-Content -Path $LogPath -Value "[-][$(Get-Date)]: Error occured when downloading Sophos uninstaller." | |
| Return $False | |
| } | |
| Add-Content -Path $LogPath -Value "[+][$(Get-Date)]: Downloaded Sophos uninstaller successfully." | |
| Return $True | |
| } | |
| Else { | |
| Return $True | |
| } | |
| } | |
| # Execute Zap | |
| Function Start-SophosZap { | |
| Try { | |
| Start-Process -FilePath $ZapFile -ArgumentList "--confirm" -NoNewWindow -Wait | |
| } | |
| Catch { | |
| Add-Content -Path $LogPath -Value "[-][$(Get-Date)]: Error occured when running Sophos uninstaller." | |
| Return $False | |
| } | |
| Return $True | |
| } | |
| # Delete SophosZap if uninstallation is complete | |
| If (($InstalledSoftware -notmatch "Sophos") -and ($LogCount -ge 2) -and (Test-Path $ZapFile)) { | |
| Try { | |
| Remove-Item -Path $ZapFile | |
| Add-Content -Path $LogPath -Value "[+][$(Get-Date)]: Deleted Sophos uninstaller successfully." | |
| } | |
| Catch { | |
| Add-Content -Path $LogPath -Value "[-][$(Get-Date)]: Error occured when deleting Sophos uninstaller." | |
| } | |
| Exit 0 | |
| } | |
| # Conditional logic to run removal function | |
| If (Get-SophosZap) { | |
| If (($InstalledSoftware -match "Sophos") -and ($LogCount -eq 0)) { | |
| If (Start-SophosZap) { | |
| Add-Content -Path $LogPath -Value "[+][$(Get-Date)]: Initiated SophosZap2 successfully." | |
| } | |
| Else { | |
| Add-Content -Path $LogPath -Value "[-][$(Get-Date)]: Failed to uninstall Sophos." | |
| } | |
| } | |
| If ($LogCount -eq 1) { | |
| $Uptime = Get-Date (Get-CimInstance -ClassName Win32_OperatingSystem).LastBootUpTime | |
| $StringMatch = Get-Content $LogPath | select-string "SophosZap2" -simplematch | |
| $LastLine = $StringMatch[($StringMatch.count - 1)] | Out-String | |
| $LastDate = (Get-Date $LastLine.Substring(6, 19)) | |
| If ($Uptime -gt $LastDate) { | |
| If (Start-SophosZap) { | |
| Add-Content -Path $LogPath -Value "[+][$(Get-Date)]: SophosZap2 second run successful." | |
| } | |
| Else { | |
| Add-Content -Path $LogPath -Value "[-][$(Get-Date)]: Failed to uninstall Sophos." | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment