0

I want to uninstall a package once it finds the right version. irrespective of any version, its uninstallaing it . Its not considering the version here. Can someone kindly help me

$uninstallDsktp = Get-ItemProperty 'HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*'| Select-Object DisplayVersion | Where-Object { $_.Displayname -match "MicroStrategy Desktop"}
#$tru = 11.1.1.6968

if ($uninstallDsktp -eq 11.1.6968) {
 $Parms= " /Uninstall /norestart --ResponseFile=""$InstDir\response_uninstall.ini"" /s /f1""$InstDir\uninstall.iss"" -f2""C:\Windows\FNMA\Logs\MSTR_Desktop-11.1.1_UnInstall.log"""
 $CMDS = "Setup.exe"
 $Process = Start-Process $CMDS $Parms -PassThru -Wait -WorkingDirectory "C:\Program Files (x86)\InstallShield Installation Information\{CE4E5307-2A7F-4DE2-A66D-9B198829A688}"
 $Extval=$Process.ExitCode
 LogWrite "Uninstalling MicroStrategy Desktop 11.1.0 exit code:$Extval"
}
vonPryz
  • 22,996
  • 7
  • 54
  • 65
Hema
  • 13
  • 3

1 Answers1

1

You have a mistake in the pipeline. Try this

$uninstallDsktp = Get-ItemProperty 'HKLM:\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*' | Where-Object { $_.Displayname -match "MicroStrategy Desktop"} | Select-Object DisplayVersion

The other error I noticed is that the value you're looking is stored in $uninstallDsktp.DisplayVersion, so the if statement should look like this

if ([version] $uninstallDsktp.DisplayVersion -eq [version] '11.1.6968') {
knurmia
  • 141
  • 2
  • 10
  • 1
    you may want to look into using the `[version]` type for doing comparisons. it's fairly neat ... [*grin*] – Lee_Dailey Jun 01 '20 at 21:55
  • If the pipeline filters out the parameter that is used in the latter where clause, the one liner will never give any results i.e. if you select only DisplayVersion, you cannot filter later using DisplayName. – knurmia Jun 02 '20 at 02:39
  • @knurmia My apologies: I didn't notice that two similar-sounding, but distinct properties were involved `DisplayName` vs. `DisplayVersion`, so you're right about how the pipeline must be rewritten. However, the point about the `if` statement stands: the original `if` statement is flawed, and so is yours, which additionally introduces a superfluous `$(...)`. – mklement0 Jun 02 '20 at 03:42
  • 1
    The response from knurmia worked. This solution worked for me and thank you so much. – Hema Jun 02 '20 at 13:06
  • @mklement0 You're correct that I have superfluous parentheses in my example, but I've just learned to use those since many times those are needed to correctly address the attribute. For instance $user = Get-ADUser | Select Name Write-Host "The name is: $user.Name" # This doesn't show the 'Name' attribute Write-Host "The name is: $($user.Name)" # This shows the 'Name' attribute – knurmia Jun 02 '20 at 14:20
  • @knurmia Thanks for updating. Note that you only ever need `$(...)` around property-access or method-call expressions _inside `"..."`_ - see [this answer](https://stackoverflow.com/a/40445998/45375). Similarly, to embed a single _command_ in an expression, you should use `(...)`, not `$(...)` - and assigning a single command's output to a variable requires neither (e.g., `$year = Get-Date | Select-Object -ExpandProperty Year`) – mklement0 Jun 02 '20 at 14:22
  • 1
    @mklement0 Thanks for the writeup. That's the problem in learning by doing. You learn how to get things working, not the best and most efficient way to achieve the result you want. – knurmia Jun 02 '20 at 14:31