0

I've wrote a PowerShell script to check for Java version and if it's not there it will run the installer for it, but for some reason even though it detects the specified Java version yet it still run the installer.

$java = Get-WmiObject -Class win32_product |
        where {$_.Name -like "*Java 7 Update 80*"}
If ($java -eq 'Java 7 Update 80') {
  "your java version is acceptable"
  Exit
} ElseIf ($java -ne 'Java 7 Update 80') {
  Start-Process -filepat C:\jre1.7.0_80.msi /passive
  "You don't have the right version of Java, installing Java 7 Update 80"
}
Write-Host "End"
2
  • 3
    have you printed the value of $java to check it out ? Commented Jul 17, 2015 at 15:08
  • 1
    FYI : don't use the win32_product class for this, unless you like to create new problems. Win32_product is broken : when you query this class it forces a repair on all msi's that are registered which may cause new problems with applications suddenly losing their settings. This is also the reason why this class performs so slow (and it spams the application eventlog). Looping through the uninstall registry values is a much faster and safer approach. I also noticed a typo in the -filepath switch of the start-process cmdlet Commented Jul 17, 2015 at 16:46

2 Answers 2

1

You filter with -like "*Java 7 Update 80*", which will pick up more than just Java 7 Update 80 (for instance Java 7 Update 80 (64-bit)), but then check if the returned string is exactly Java 7 Update 80 when you decide whether or not to launch the installer.

Do as @Random says and check the value of $java before the If. You most likely have something similar too, but more than just "Java 7 Update 80" in $java.

To avoid this kind of flaky behavior you need to keep your conditions consistent. Either use -like "*Java 7 Update 80*" everywhere, or use -eq "Java 7 Update 80" everywhere, but don't mix them.

You could also make use of how PowerShell evaluates other types to boolean. Your Get-WmiObject statement produces either $null (which evaluates to $false) or a non-empty string (which evaluates to $true), so you could do something like this to avoid implementing the same check multiple times:

if ($java) {
  "your java version is acceptable"
  Exit
} else {
  Start-Process -filepat C:\jre1.7.0_80.msi /passive
  "You don't have the right version of Java, installing Java 7 Update 80"
}

You don't need an elseif condition anyway, since your logic is binary (Java either is or isn't installed).

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

Comments

0

So turned out that the $java variable was wrong, also Eric was right

Start-Process powershell -Verb runAs
$java = Get-WmiObject -Class win32_product | where { $_.Name -like "*Java 7 Update 71*"}
If  ($java){"your java version is acceptable"}
Else {Start-Process "C:\jre-7u71-windows-i586.exe" -Verb runAs -ArgumentList "/s" -Wait
"You don't have the right version of Java, installing Java 7 Update 71"
} 
Write-Host "Press any key to exit..."

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.