Powershell script question

I am working on putting together a script that will generate an html report on disk space usage on our Windows servers.  I am having troubles with the script giving me an error on a section of the script stating "Assignment expression is not valid.  The left hand side of an assignment operator needs to be something that can be assigned..."  
here is the section of the script:
foreach($disk in $disks)
 {        
  $deviceID = $disk.DeviceID,
  $volName = $disk.VolumeName,
  $size = $disk.Size,
  $freespace = $disk.FreeSpace,  
  $percentFree = [Math]::Round(($freespace / $size) * 100, 2),
  $sizeGB = [Math]::Round($size / 1073741824, 2),
  $freeSpaceGB = [Math]::Round($freespace / 1073741824, 2),
  $usedSpaceGB = ($sizeGB - $freespaceGB), <-- This is the problem
  $color = $whiteColor;

If I replace the commas with semi-colons at the end of each line, the script will run but it will only capture one server and only one of its drives.  I am new to powershell scripting so I am a little stuck.
justin_grswldAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
Remove the commas. Don't use semi-colons either. PowerShell does not have a command termination character; the semi-colon is used only if you want to supply several commands on the same line. The comma always builds an array, so you are in fact creating a nested array above.

The above code is missing some important stuff to collect the final results. I reckon you want to something like this:
$result = @()    # an empty array
foreach ($disk in $disks) 
 {         
  $sizeGB = [Math]::Round($disk.size / 1073741824, 2)
  $freeSpaceGB = [Math]::Round($disk.freespace / 1073741824, 2)
  $result += New-Object PsObject -Property @{
    deviceID =  $disk.DeviceID
    volName = $disk.VolumeName
    size = $disk.Size
    freespace = $disk.FreeSpace
    percentFree = [Math]::Round(($disk.freespace / $disk.size) * 100, 2)
    freeSpaceGB = $freeSpaceGB
    sizeGB = $sizeGB
    usedSpaceGB = ($sizeGB - $freespaceGB)
    color = $whiteColor
  }
}

Open in new window

$result will then contain several uniform objects with the properties as given. E.g. $results[0].deviceID contains the first device ID.
Note that you cannot use the result of a property in an expresssion in the same block. That is, we have to set $sizeGB outside of New-Object to be able to refer twice to it.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
justin_grswldAuthor Commented:
Thank you for the fast response on this.  You pointed me in the right direction.  I had to modify this script a touch, but was able to make it work.  Thank again.
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
Of course you had to modify it, since you did not post a complete script :p. But this is even better, as it shows you were able to digest what I was presenting.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.