Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have a batch file that takes a computer name as its argument and will install Java JRE on it remotely. Now, Im trying to run this Powershell script to repeatedly call the batch file and install Java on any system that it finds without the latest version. It seems to run error-free, but the statements inside the if code block never seem to run - even when the if conditional test evaluates to true. Can anyone look at this script and point out what I'm possibly missing? I'm using the Quest AD cmdlets, and BSOnPosh module. Thank you.

get-qadcomputer -sizelimit 0 -name mypc* -searchroot 'OU=MyComputers,DC=MyDomain,DC=lcl'| test-host -property name |ForEach-Object -process {
$targnm = $_.name
$tststr=reg query "\\$targnm\HKLM\SOFTWARE\JavaSoft\Java Runtime Environment" /v Java6FamilyVersion
 if(-not ($tststr | select-string -SimpleMatch '1.6.0_20'))
 {
  $mssg="Updating to JRE 6u20 on $targnm"
  Out-Host $mssg
  Out-File -filepath c:\install_jre_log.txt -inputobject $mssg -Append
  cmd /c \\server\apps\java\installjreremote.cmd $targnm
 }
 else
 {
  $mssg ="JRE 6u20 found on $targnm"
  Out-Host $mssg
  Out-File -filepath c:\install_jre_log.txt -inputobject $mssg -Append
 }
}
share|improve this question
add comment

2 Answers

up vote 2 down vote accepted

OK, so I've been tinkering with this one for a bit and got it working. At first, it looked like the script was running as expected, except that it would show an error message in the console whenever the reg query command didn't find the reg key that it was looking for. That in itself was not an issue, but after that I would have expected it to continue and run the if code block - instead it seemed to be skipping right on to processing the next object in the foreach-object queue from the pipeline. I am not sure what was going on there, but eventually I got the code to the point where it was evaluating everything correctly and trying to run the if code block. This time, I was getting the following error whenever the script hit an Out-Host statement: Out-Host : A positional parameter cannot be found that accepts argument mystringvar. Apparently there is something about using Out-Host that causes ForEach-Object to think that the processing code block is done. Anyway, I overcame this by switching to using Write-Output instead. Here's the final working code:

import-module bsonposh
add-pssnapin Quest.ActiveRoles.ADManagement

get-qadcomputer -sizelimit 0 -searchroot 'OU=MyComputers,DC=MyDomain,DC=lcl' | test-host -property name |ForEach-Object -process {
    $targnm = $_.name
    $tststr=(reg query "\\$targnm\HKLM\SOFTWARE\JavaSoft\Java Runtime Environment" /v Java6FamilyVersion)
    if(-not($tststr |select-string -simplematch '1.6.0_20'))
    {
        $mssg="Updating to JRE 6u20 on $targnm"
        Write-Output $mssg
        Out-File -filepath c:\install_jre_log.txt -inputobject $mssg -Append
        cmd /c \\myfileserver\apps\java\installjreremote.cmd $targnm
    }
    else
    {
        $mssg ="JRE 6u20 found on $targnm"
        Write-Output $mssg
        Out-File -filepath c:\install_jre_log.txt -inputobject $mssg -Append
    }
}
share|improve this answer
add comment

You could try simplifying the if to:

if($tststr -notmatch '1.6.0_20')

to see if that helps, but I don't see anything obviously wrong unless it's never really evaluating to true. So the else clause is always run?

share|improve this answer
    
That's not the same as in the question since you're now doing a regular expression match and need to escape the .-like might be an appropriate alternative. –  Јοеу May 2 '10 at 9:06
    
-notlike would work if I were just looking at a simple String, but the reg.exe command actually returns an array of strings. So, I stuck to my original Select-String usage. –  Dmart May 3 '10 at 19:06
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.