question

JoeyD-6442 avatar image
0 Votes"
JoeyD-6442 asked ·

Attempted to divide by zero

any idea why I am getting this error

get-diskspace2.ps1

param(
[string]$VM
)

$report += Get-WmiObject Win32_Volume -ComputerName $VM| ForEach {
New-Object PSObject -Property @{
Computername = $VM
Name = $.Name
Label = $
.Label
FreeSpace_GB = ([Math]::Round($.FreeSpace /1GB,2))
TotalSize_GB = ([Math]::Round($
.Capacity /1GB,2))
UsedSpace_GB = ([Math]::Round($.Capacity /1GB,2)) - ([Math]::Round($.FreeSpace /1GB,2))
Percent_Free = ([int]($.Freespace*100/$.Capacity))
filesystem = $_.filesystem
}
}

$report

windows-server-powershell
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

MotoX80 avatar image
0 Votes"
MotoX80 answered ·

It would appear that this is the only statement that could generate that error.

 Percent_Free = ([int]($.Freespace*100/$_.Capacity))

So you have some volume where $_.Capacity is zero.

Add some debugging statements to your script to determine which volume is causing the problem. Maybe start by just looking at the raw freespace and capacity numbers


Add an if statement to test for zero and don't do the division if it is.

·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

IanXue-MSFT avatar image
0 Votes"
IanXue-MSFT answered ·

Hi,

The error message means the $_.Capacity of some volume is zero. It could be the CD-ROM. If you only want to get the hard drives you can filter with DriveType like below

 $report += Get-WmiObject Win32_Volume -ComputerName $VM | Where-Object {$_.DriveType -eq 3} | ForEach{ ... }

DriveType

Best Regards,
Ian Xue
============================================
If the Answer is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


·
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.