Hi all.
I am new to PowerShell and am having some difficulties executing a script that I adapted from one I found online on PCs that are running PowerShell 2.0. The script executes just fine on those running PS 3.0
The aim of the script is to uninstall all previous versions of Java, and it is as follows:
#Set msiexec location as variable
$msiexec = "C:\Windows\System32\msiexec.exe"
#Stop any running Java processes
IF (Get-Process -Name Java -ea SilentlyContinue)
{
Stop-Process -Name Java -Force
}
IF (Get-Process -Name JP2Launcher -ea SilentlyContinue)
{
Stop-Process -Name JP2Launcher -Force
}
IF (Get-Process -Name javaw -ea SilentlyContinue)
{
Stop-Process -Name javaw -Force
}
#Find instances of Java and uninstall them
$java = Get-WmiObject -Class Win32_Product | Where-Object { $_.Name -match "Java" }
foreach ($app in $java)
{
Start-Process -FilePath $msiexec -ArgumentList /x, $app.IdentifyingNumber, /quiet, /norestart -Wait
}
Are there any basic reasons as to why the script may not execute correctly on PowerShell 2.0? I am using LanDesk software to push out the script - which further complicates things.
Basically the script runs first, removes previous Java versions, and then deploys the latest Java MSI.
On PS 2.0 machines, the uninstall script does not work correctly, and the MSI installs over the top, which ends up breaking Java. On PS 3.0 machines, the uninstall script works correctly, and the Java MSI installs Java and all is good.
First I thought it could be because I needed to import modules in PS 2.0, but I didn't get very far with that.
I hope the above makes sense.
Any help is much appreciated.