1

I wanted to start by saying i did read some other topics on this but at this point i still have to do it none the less (decision been made above my pay grade).

We are in the process of moving and testing out Azul java to see how well that works for us. Meantime I need to figure out how to script the uninstall of the all Oracle java (preferably I would have a script for JRE and another for JDK).

Currently how I have been removing java versions is by having a list of all jre versions doing msiexec /x "GUID of the java version" /qn but it is incredibly time consuming so I wanted to get those strings pragmatically.

Right now I run the following command:

Get-WmiObject -Class Win32_Product | Where-Object {$_.Name -like 'Java*'}

enter image description here

However, I am trying to find out how to automatically get the IdentifyNumber for every result as long as Vendor = Oracle Corporation and pipe it to the msiexec command to problematically remove them. Azul is installed on the same system as the command is being ran but is not showing up (which is good but just in case I wanted to have the vendor qualifier).

My question is: Can someone help me pipe the IdentifyNumber of the product into msiexec command assuming Vendor is Oracle Corporation

Thank you in advance.

Update 1: Following feedback from Drew and Ansgar

$Source = Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*
$Uninstall = $Source | Where-Object {($_.Publisher -like '*Oracle*')-and ($_.DisplayName -like 'Java*')} | select UninstallString
$UninstallString = $Uninstall.UninstallString
$UninstallSting | ForEach-Object{Invoke-Expression -ScriptBlock ($UninstallString)}

However, I cant seem to pipe the strings correctly as I keep getting:

Invoke-Command : Cannot convert 'System.Object[]' to the type 'System.Management.Automation.ScriptBlock' required by parameter 'ScriptBlock'. Specified method 
is not supported.
At C:\Konstantin\Java\UninstallAllJava.ps1:8 char:62
+ ... ting | ForEach-Object{Invoke-Command -ScriptBlock ($UninstallString)}
+                                                       ~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Invoke-Command], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgument,Microsoft.PowerShell.Commands.InvokeCommandCommand
1

2 Answers 2

1

I would recommend the Registry method.

Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* | 
Where-Object {$_.Publisher -like '*oracle*'} | select UninstallString

This will give you your MsiExec.exe link. E.G. MsiExec.exe /X{26A24AE4-039D-4CA4-87B4-2F32180181F0}

Sign up to request clarification or add additional context in comments.

1 Comment

Following your example I have the following: <blink>$Source = Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall* $Uninstall = $Source | Where-Object {($_.Publisher -like 'Oracle')-and ($_.DisplayName -like 'Java*')} | select UninstallString $UninstallString = $Uninstall.UninstallString $UninstallSting | ForEach-Object{Invoke-Expression -ScriptBlock ($UninstallString)} <blink> but the last part where i try to run each string keeps failing and i cant figure out why
0

So i was able to figure it out so here is what I came up with which seem to work

Set "Source" variable to registry location

$Source = Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*

Set "Uninstall" variable to inherit from source and reduce the scope to Oracle and Java

$Uninstall = $Source | Where-Object {($_.Publisher -like '*Oracle*')-and ($_.DisplayName -like 'Java*')} | select UninstallString

Set "UninstallString" variable to get only the uninstall string from "Uninstall" variable

$UninstallStrings = $Uninstall.UninstallString -replace "MsiExec.exe ","MsiExec.exe /qn " -replace "/I","/X"

Run all the uninstall strings

ForEach($UninstallString in $UninstallStrings){& cmd /c "$UninstallString"}

I am sure it can be a lot cleaner but this is the best i can do for now. For some versions of java the uninstall strings would give /i variable instead of /x (JDK 6) which is why i had to do the second replace statement.

Example:

UninstallString                                     
---------------                                     
MsiExec.exe /X{26A24AE4-039D-4CA4-87B4-2F03217079FF}
MsiExec.exe /X{26A24AE4-039D-4CA4-87B4-2F83216045FF}
MsiExec.exe /I{32A3A4F4-B792-11D6-A78A-00B0D0160450}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.