21 January 2013

A couple of days after I made my last post I realized that, for that specific purpose, I could have just dynamically created hashtables as the values of each array.  It would look like this pseudo-code:

$array = @()
$array += @{
   DriveLetter = $object.DriveLetter
   TotalSpace = $object.Size
   FreeSpace = $object.FreeSpace
}

Just remember to first declare the array variable as an array or you’ll get errors.  Then as I enumerate through each returned object it’s as simple as:

ForEach ($disk in $array) {
  write-host "Drive: $disk.DriveLetter has $disk.FreeSpace free space out of a total of $disk.TotalSpace"
}

Or whatever I wanted to do with it.  Now that I think about it, creating a specific name for each variable isn’t necessary and I can’t think of any scenarios where I really need to do that.  I guess it could happen, though, so I’ll leave my previous post up.


There are no comments.



You must be logged in to post a comment.