Share via


Multiple values for an IF statement with -contains

Question

Wednesday, June 6, 2012 9:23 AM

Hey guys !

I am facing a little problem here.

I would like to process some functions to computers that have some types of chassis. In order to do this I have the following IF statement:

 $computer = $env:computername  
        $chassis = Get-WmiObject win32_systemenclosure -computer $computer 
        $result = $chassis.chassistypes
        write-host "Here is the chassis type: $result"
        #if ($result -contains "8","9","10","11","12","14","18","21")
        if ($result -contains '10')
        {
        $computertype = "Laptop"
        MoveComputer($LaptopsOU)
        }  

        elseif ($result -contains '3','4','5','6','7','15','16')
        {
        $computertype = "Desktop"
        MoveComputer($DesktopsOU)
        }  

        else 
        {
        $computertype = "Virtual Desktop"
        MoveComputer($VDSOU)
        }    
        

        Write-Log -Message "The workstation $Computername has been identified as a $computertype and moved to appropriate OU" -Status "INFO"
        exit

The problem is that if I process the IF statement with multiple values, I directly go to the else that process the default behavior. But if I use the IF statement that is not with # here it works like a charm !

So I am guessing it is a matter on how to provide multiple values in a IF statement with the -contains parameter but I didn't find out how to solve this.

If someone could help, I would really appreciate :) !!

Thanks in advance

All replies (2)

Wednesday, June 6, 2012 9:44 AM âś…Answered | 2 votes

Hi,

If you need multiple IF, then better is use switch like this:

switch($result)
{
 {10 -contains $_} {#do something}
 {'3','4','5','6','7','15','16' -contains $_} {#do something else}
 default {#do something defoult}
}

For more: get-help about_Switch


Wednesday, June 6, 2012 10:10 AM

Indeed, my bad.

It is much better like this.

Thank you MichalGajda !