2

I obtain the available raw disks on my VM by using

$availDisks = Get-PhysicalDisk -CanPool $true

The $availDisks contains 3 RAW disks I would like to effectively compare the size of the disks, and if they are the same size, to stripe them.

$availDisks.Size gives me the output of the 3 disks, but I need to compare each one of them.

The method I used is

$size = $availDisks[0].size
foreach($disk in $availDisks){
   if($disk.size -eq $size){
      Write-Host "The disk is equal to the desired size"
      continue with the disk striping
   }else{
      Write-Error "One of the disks is not matching the size"
      Do something else
   }
}

Is there a more effective method?

2 Answers 2

2

You can use Group-Object targeting the Size property of the object:

Get-PhysicalDisk -CanPool $true | Group-Object Size |
    Where-Object Count -GT 1 | ForEach-Object Group

Group the objects by the value of the Size property, filter the groups where the Count is greater than 1 (meaning disks with the same Size), then output each filtered object. If you don't get any output from this, means that the disks have different sizes.

Sign up to request clarification or add additional context in comments.

4 Comments

Unfortunately it doesn't work when I have 3 disks of one size and 2 disks of another size.
@silverbackbg The "desired size", where is that value coming from, how can you tell which size of the 3 disks is the "desired one" ?
It was an extreme scenario in case there are two disks with the same size and two other disks with the same size, but different from the first ones. However, it is quiet improbable to happen, hence I used your suggestion.
@silverbackbg thank you for accepting the answer, but, I may be able to fix the code so that it does what you're looking for. I'm just trying to understand your expected behavior.
1

Use Select-Object -Unique on the disk sizes to check if there is exactly one unique value. If there is any other value you either have no disks or a non-equal sizing of disks.

if( @( $availDisks.Size | Select-Object -Unique ).Count -ne 1) {
  Write-Error "One of the disks is not matching the size"
  # Do something else
} else {
  Write-Host "The disks are equal to the desired sizing"
  # Continue with the disk striping
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.