windows - Simple ForEach Combined with If Statment brings wrong results -
im new ps, im trying perform simple task, list of computers using get-content, though foreach loop perform wmi query each device in list , os type, if statment check perform different task depends on os, everntually win vista 7 8 10 needed seperated xp. wrote 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 xp" } else { write-host $compdevice"'s os type newer xp" } } in case same result computers ( im running secret againt 2 win xp 1 win 7 , 1 win 8 in domain envierment.
i've tried different variation :
$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 xp" } else { write-host $compdevice"'s os type newer xp" } } in both cases exact same results ( goes 1 option of if statement )
i wonder, doing wrong ? note - trying filter caption, buildnumber , version. , wild card in if statement, doesn't work
a couple of things wrong. first, have captured wmi content array not have relation of data computername. if include wmi lookup inside foreach loop, have relationship established. in other words:
$computers=get-content c:\computerlist\computers.txt foreach ( $compdevice in $computers ) { $ostype=get-wmiobject -class win32_operatingsystem -namespace "root\cimv2" -computername $compdevice ### using '-eq' should provide actual number, use * -like operator if ( $ostype.buildnumber -eq "2600" ) { ...
Comments
Post a Comment