-1

So here's the fun times: I have an array with virtual server values in columns, etc. There's a ForEach loop that each server goes through, and I'm trying to weed out servers with a 0 count for Disks. Here's my syntax:

$VMInfo.Disks = Get-HardDisk -VM $VM | Measure-Object | Select-Object Count

In the exported CSV file, if there's not a hard disk the column has @{Count=0} as the value. So I figured this if statement would be a good way to weed out those servers from the report (this if statement is part of the ForEach overall command):

if ($VMInfo.Disks -ne "@{Count=0}") {
      $Report += $VMInfo
      }

However, when I include the if statement, I still get servers without a hard drive included in the exported CSV file. Any ideas? Do I need to change the $VMInfo.Disks -ne "@{Count=0}" command to something else? Any help would be greatly appreciated!

edit: Tried to use -gt 0 instead of -ne "@{Count=0}" but got the following error:

Cannot compare "@{Count=2}" to "0" because the objects are not the same type or the object "@{Count=2}" does not implement "IComparable".

edit: Tried to use @{Count=0} instead of "@{Count=0}" but it didn't make a difference - CSV still had columns with @{Count=0} in them.

nobody
  • 19,814
  • 17
  • 56
  • 77
Nate
  • 3
  • 2

1 Answers1

0

Despite being downvoted with no explanation or help (such a nice introduction to stackoverflow), a colleague helped me figure this out:

The $VMInfo.Disks line above essentially created a hash table in the results - that's why the spreadsheet would show @{Count=0}. What I needed to do after the $VMInfo.Disks line above was use the following line:

$VMInfo.Disks = ($VMInfo.Disks).Count

That would return an integer. Since it would return an integer, I could then use this if statement:

if ($VMInfo.Disks -ne 0) {
  $Report += $VMInfo
  }

Now I'm good!

Nate
  • 3
  • 2