Author Posts

March 30, 2015 at 7:15 am

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 }

March 30, 2015 at 7:23 am

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?

March 30, 2015 at 7:29 am

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

March 30, 2015 at 7:41 am

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.

March 30, 2015 at 8:58 am

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 !

March 30, 2015 at 9:21 am

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.

March 30, 2015 at 11:12 am

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 !! 🙂

March 30, 2015 at 11:33 am

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 ?

March 30, 2015 at 9:52 pm

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'

March 30, 2015 at 10:58 pm

Cool, thanks Tim !