Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

Simple ForEach Combined with If Statment brings wrong results

$
0
0

Im fairly new to PS, im trying to perform a simple task, get list of computers by using the get-content, than though a foreach loop perform a wmi query to each device in the list and get the OS type, than with a IF statment check perform a different task depends on the OS, everntually win vista 7 8 10 needed to be seperated from xp. I wrote the following PS script :

$computers=Get-Content C:\ComputerList\Computers.txt 
$OSType=Get-WmiObject -Class Win32_operatingsystem -namespace "root\CIMV2" -ComputerName $computers

ForEach ( $compdevice in $computers ) {

if ( $OSType.buildnumber -eq "2600*" ) {

Write-Host $compdevice"'s OS type is XP" }

Else {

Write-Host $compdevice"'s Os type is Newer than xp"

}

in this case i get the same result for all computers ( im running the secret againt 2 win xp 1 win 7 and 1 win 8 in a domain envierment.

i've tried a different variation also :

 

$computers=Get-Content C:\ComputerList\Computers.txt $OSType=Get-WmiObject -Class Win32_operatingsystem -namespace "root\CIMV2" -ComputerName $computers

ForEach ( $compdevice in $computers ) {

if ( $OSType.buildnumber -eq "2600*" ) {

Write-Host $compdevice"'s OS type is XP" }

Else {

Write-Host $compdevice"'s Os type is Newer than xp"

} }

in both cases i get the ecxact same results ( all goes to one option of the IF statment )

I wonder, what am I doing wrong ? Note - I was trying to filter by caption, buildnumber and version. and even wild card in the IF statment, it doesnt work well :). thx in advance :)


Viewing all articles
Browse latest Browse all 6937

Trending Articles