Hello everyone,
I’m having difficulties creating a PowerShell script. The goal of this script is to remove a program.
Basically, I’ve created several scripts for removing other programs, and these scripts work perfectly. All test runs through BigFix were successful. Below is one of the successful scripts:
function UninstallProgram {
param(
[string]$programName
)
function Stop-Process {
$process = Get-Process | Where-Object { $_.ProcessName -eq "ultravnc" }
if ($process) {
Write-Host "Process found. Process ID: $($process.Id)"
Stop-Process -Id $process.Id -Force
Write-Host "Process stopped successfully."
} else {
Write-Host "Process not found."
}
}
Stop-Process
$registryKeys = @(
"HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall",
"HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall"
)
foreach ($registryKey in $registryKeys) {
if ($registryKey.StartsWith("HKU:")) {
$sid = $registryKey -replace "HKU:\\"
$userReg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('Users', '')
$regKey = $userReg.OpenSubKey("$sid\Software\Microsoft\Windows\CurrentVersion\Uninstall")
if ($regKey -eq $null) {
continue
}
$uninstallKeys = $regKey.GetSubKeyNames() | ForEach-Object { $regKey.OpenSubKey($_) }
} else {
$uninstallKeys = Get-ChildItem $registryKey | Get-ItemProperty
}
foreach ($key in $uninstallKeys) {
if ($key.DisplayName -match $programName) {
$uninstallString = $key.UninstallString
$is64BitApp = $uninstallString -match "SysWOW64"
if ($uninstallString -notlike "* /VERYSILENT") {
$uninstallString += " /VERYSILENT"
}
Write-Host "UninstallString value: $uninstallString"
Write-Host "Uninstalling $($key.DisplayName) silently..."
Start-Process -FilePath "cmd.exe" -ArgumentList "/c $uninstallString" -WindowStyle Hidden -PassThru -Wait
Write-Host "$($key.DisplayName) uninstalled successfully."
return
}
}
}
Write-Host "$programName not found. No action taken."
}
UninstallProgram "UltraVNC"
In this script above, the UninstallString is looked up during execution. I also tried to do it this way for the other script I’m creating, but it hasn’t been working.
The difference between the UltraVNC program and dotPeek is that the uninstallstring for UltraVNC is located in HKEY_LOCAL_MACHINE, while for dotPeek it is located in HKEY_USERS. The application itself is installed only for the user.
Whenever I try to make any changes related to users through BigFix, some kind of problem occurs. Is it possible that the problem is with permissions? Doesn’t BigFix already run as Administrator?
Also, I would like to share below the newly created PowerShell script for dotPeek removal. The script works in the PowerShell ISE tool, but not when executed by BigFix. It is also worth noting that I wanted to make this script as simple as possible, already with the uninstallstring provided so that there are no conflicts with environment variables when it comes to users (if that is a possibility).
function UninstallDotPeek {
Start-Process -FilePath "cmd.exe" -ArgumentList '/c "C:\Users\user_123\AppData\Local\JetBrains\Installations\dotPeek233\JetBrains.Platform.Installer.exe" /HostsToRemove=dotPeek233 /sile' -WindowStyle Hidden -PassThru -Wait
return $true
}
UninstallDotPeek
Does anyone have any idea what it could be?