If statement with numbers

This topic contains 9 replies, has 3 voices, and was last updated by Profile photo of Graham Beer Graham Beer 1 year, 8 months ago.

  • Author
    Posts
  • #23758
    Profile photo of Graham Beer
    Graham Beer
    Participant

    Hi,

    trying to up lift the IsDesktop from the MDT ZTIGather.wsf to powershell.

    But the below is not working and i can work out why. What am i doing wrong ?

    $Chassis = Get-CimInstance -Class Win32_SystemEnclosure
    if ($Chassis.ChassisTypes -eq '8','9','10','11','12','14','18','21','23' -and '3','4','5','6','7','15','16' ) {
    $true
    }
    else {
    $false }

  • #23760
    Profile photo of Don Jones
    Don Jones
    Keymaster

    I think you're probably using the wrong operator. First of all, does ChassisTypes contain one value, or an array of values? The docs suggest it's an array of numbers. PowerShell doesn't really contain an operator that will tell you "this array contains one or more values from this other array." You could possibly hack something together with Compare-Object, but it'd be awkward.

    You're also using -and entirely wrong. The Boolean operator -and requires a complete comparison on either side of it; it does not mean "-andAlsoEqualTo," which is how you're trying to use it.

    So to clarify: You want to see if ChassisTypes contains ALL of the values in EITHER list you've provided?

  • #23763
    Profile photo of Graham Beer
    Graham Beer
    Participant

    Hi Don,

    Basically i want to say if the Chassis number is (for example) 1,2,4,7,8 then True, else False.

    Thank you

  • #23768
    Profile photo of Don Jones
    Don Jones
    Keymaster

    Yeah, that's what I was afraid of.

    PowerShell's -eq operator doesn't do array comparisons. For example:

    PS C:\> $list1 = 1,2,3
    PS C:\> $list2 = 3,4,5
    PS C:\> $test_true = 1,2,3
    PS C:\> $test_false = 2,3,4
    PS C:\> if ($test_true -eq $list1 -or $test_true -eq $list2) { $true } else { $false }
    False
    

    See? That should have been True, because $test_true DOES equal $list1, but PowerShell doesn't see it that way. Also notice that this is the correct use of -or (as well as -and), with a comparison on either side.

    You could maybe use Compare-Object. If there are NO differences between a reference list and your test list, then they're the same.

    PS C:\> (Compare-Object $test_true $list1).Count
    0
    PS C:\> (Compare-Object $test_false $list1).Count
    2
    

    So, done as logic:

    PS C:\> $list1 = 1,2,3
    $list2 = 4,5,6
    
    $test = 1,2,3
    
    if ( (Compare-Object -Ref $list1 -Dif $test).Count -eq 0 -or
         (Compare-Object -Ref $list2 -Dif $test).Count -eq 0
        ) 
    { "One list matches" }
    else
    { "Neither list matches" }
    
    
    One list matches
    

    That will tell you if array $test exactly matches EITHER array $list1 or array $list2.

  • #23775
    Profile photo of Graham Beer
    Graham Beer
    Participant

    Ok thank Don. In the VB script the script let the result check to say if a number which matches 1 – 10 then true.

    Looks like I might need another way to establish it the machine is physical.

    Thanks for clearing up the And as well !

  • #23776
    Profile photo of Tim Pringle
    Tim Pringle
    Participant

    Hey Graham,

    If you're happy to just know if it's physical or virtual, then maybe take a look at win32_computersystem. I'm not certain what it is with VMware virtuals, but you'll find the manufacturer property has Microsoft for Hyper V systems, and Xen for XenServer systems.

  • #23787
    Profile photo of Graham Beer
    Graham Beer
    Participant

    Hi,

    Think i might have sorted the best way to find physical or virtual :

    (Get-CimInstance -ClassName Win32_BaseBoard).HostingBoard

    Seems to work a treat. Quite proud of myself !! 🙂

  • #23790
    Profile photo of Graham Beer
    Graham Beer
    Participant

    Actually got another way as well,

    $IsVM = (Get-CimInstance -Class Win32_ComputerSystem).Model -match 'virtual'
    Write-Output $IsVM

    Am i getting this in the right format ?

  • #23803
    Profile photo of Tim Pringle
    Tim Pringle
    Participant

    That looks good Graham. If you're only using Hyper V virtuals, that's you pretty covered

    If you're using any Xen virtuals at all, you may also need to look for a model of 'HVM domU'

  • #23808
    Profile photo of Graham Beer
    Graham Beer
    Participant

    Cool, thanks Tim !

You must be logged in to reply to this topic.