Powershell Foreach-Object with if statement not working - help!
Posted
by Dmart
on Server Fault
See other posts from Server Fault
or by Dmart
Published on 2010-05-01T07:05:31Z
Indexed on
2010/05/03
19:28 UTC
Read the original article
Hit count: 245
powershell
|scripting
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
}
}
© Server Fault or respective owner