Automating Java installation through Powershell -


i've wrote powershell script check java version , if it's not there run installer it, reason though detects specified java version yet still run installer.

$java = get-wmiobject -class win32_product |         {$_.name -like "*java 7 update 80*"} if ($java -eq 'java 7 update 80') {   "your java version acceptable"   exit } elseif ($java -ne 'java 7 update 80') {   start-process -filepat c:\jre1.7.0_80.msi /passive   "you don't have right version of java, installing java 7 update 80" } write-host "end" 

you filter -like "*java 7 update 80*", pick more java 7 update 80 (for instance java 7 update 80 (64-bit)), check if returned string exactly java 7 update 80 when decide whether or not launch installer.

do @random says , check value of $java before if. have similar too, more "java 7 update 80" in $java.

to avoid kind of flaky behavior need keep conditions consistent. either use -like "*java 7 update 80*" everywhere, or use -eq "java 7 update 80" everywhere, don't mix them.

you make use of how powershell evaluates other types boolean. get-wmiobject statement produces either $null (which evaluates $false) or non-empty string (which evaluates $true), avoid implementing same check multiple times:

if ($java) {   "your java version acceptable"   exit } else {   start-process -filepat c:\jre1.7.0_80.msi /passive   "you don't have right version of java, installing java 7 update 80" } 

you don't need elseif condition anyway, since logic binary (java either or isn't installed).


Comments