dbatools icon indicating copy to clipboard operation
dbatools copied to clipboard

Get-DbaDiskSpace - Attempted to divide by zero.

Open ClaudioESSilva opened this issue 10 years ago • 13 comments

Found this odd error. On my computer I can see an "transparent" drive "E:" image

It is eligeble to the command output but the capacity and other properties are 0 (zero) or empty. image

My suggestion and I have done it on my branch for Move-SqlDatabaseFile that relays on Get-DbaDiskSpace is doing the following.

if ([string]::IsNullOrEmpty($($disk.Capacity)))
{
    $diskCapacity = 1
}
else
{
    $diskCapacity = $disk.Capacity
}
$percentfree = "{0:n2}" -f (($disk.Freespace / $diskCapacity) * 100)

Thoughts?

ClaudioESSilva avatar Sep 19 '16 22:09 ClaudioESSilva

First thought is, it's showing in File Explorer without any size information, so I'm not surprised the cmdlet shows no information either?

dansqldba avatar Sep 20 '16 08:09 dansqldba

Shouldn't wmi be replaced with cim? https://blogs.technet.microsoft.com/heyscriptingguy/2016/02/08/should-i-use-cim-or-wmi-with-windows-powershell/

I wrote this a while ago:

function Get-DriveInfo { [CmdletBinding()] param( [Parameter( Mandatory=$True, ValueFromPipeline=$True, ValueFromPipelineByPropertyName=$True )] [string[]]$computername ) BEGIN {} PROCESS { foreach ($computer in $computername) { $disks = Get-CimInstance -Query "SELECT * FROM Win32_LogicalDisk WHERE DriveType=3 AND VolumeName <> 'System Reserved'" -computername $computer | select $computer, DeviceID, Freespace, size, @{l= 'free';e={"{0:N0}" -f ($.FreeSpace / $.Size * 100)}} foreach ($disk in $disks) { [PSCustomObject]@{ 'ComputerName' = $computer; 'DriveLetter'=$disk.DeviceID; 'FreeSpaceGB'=$disk.FreeSpace / 1GB; 'SizeGB'=$disk.Size / 1GB; 'FreePercent'=$disk.free } } } } END {} }

There is no divide by zero error if the CIM (or WMI) objects are piped to Select first!

When using Win32_Volume the additional clause should be 'AND SystemVolume = $false' If including type 2, all available USB slots will be shown, is that usefull? And if so, why not type 4, 5 and 6?

If empty drives should be left out, add another where clause: 'AND size > 0'.

PowerDBAKlaas avatar Oct 14 '16 12:10 PowerDBAKlaas

Just looked and Get-CimInstance is supported in PS v3. Sounds awesome! Feel free to proceed.

potatoqualitee avatar Oct 14 '16 12:10 potatoqualitee

There's an open PR #190 for this cmdlet with a number of changes, waiting on @Jagoop to fix something.

dansqldba avatar Oct 14 '16 12:10 dansqldba

There's a way for PowerShell V2 (WSMan 2) too:

$opt = New-CimSessionOption -Protocol DCOM $sd = New-CimSession -ComputerName server02 -SessionOption $opt Get-CimInstance -CimSession $sd -ClassName Win32_LogicalDisk

https://richardspowershellblog.wordpress.com/2012/01/16/cim-sessions/

PowerDBAKlaas avatar Oct 14 '16 14:10 PowerDBAKlaas

I would love to change all WMI to CIM, but my general impression was that this project had decided to stick to WMI?

Jagoop avatar Oct 15 '16 12:10 Jagoop

The baseline is v3 so whatever is most efficient for v3 works. In this instance, an explicit decision was not made to use a specific technology; I used WMI because I had a script that used it. Please proceed with CIM across the board as desired. We can also list it on the Guidelines when I'm done (trying to find time this weekend)

potatoqualitee avatar Oct 15 '16 12:10 potatoqualitee

I have addressed Claudio's issue with size 0/null/empty.

    if ($disk.capacity -eq 0 -or [string]::IsNullOrEmpty($disk.capacity))
    {
        $total = 0
        $free = 0
        $percentfree = 0
    }
    else {
        $total = [math]::round($disk.Capacity / $measure, 2)
        $free = [math]::round($disk.Freespace/$measure, 2)
        $percentfree = [math]::round(($disk.Freespace / $disk.Capacity) * 100, 2)
    }

Jagoop avatar Oct 15 '16 19:10 Jagoop

What about this:

            try
            {
                $disks = Get-Ciminstance -ComputerName $ComputerName -Query $query | Sort-Object -Property Name
            }
            catch
            {
                try
                {
                    $SessionParams = @{}
                    $SessionParams.ComputerName = $ComputerName
                    $SessionParams.SessionOption = (New-CimSessionOption -Protocol Dcom)
                    $Session = New-CimSession @SessionParams
                    $disks = Get-CimInstance -CimSession $Session -Query $query | Sort-Object -Property Name
                }
                catch
                {
                Write-Warning "Can't connect to WMI on $server"
                return
                }
            }

Get-Ciminstance is always preferred. If WinRM is not enabled, or WSMan < 3, fall back is New-CIMSession with DCom as protocol. This is like Get-WMIObject but should also work on non Windows OS.

This worked in my environment for servers with WSMAN 2.0, where Get-Ciminstance failed.

PowerDBAKlaas avatar Oct 17 '16 14:10 PowerDBAKlaas

Good idea - I'll try it in the next revision.

Jagoop avatar Oct 17 '16 19:10 Jagoop

try { $ipaddr = (Test-Connection ....} never fails. When I execute this to a non-existing server the catch { "can't connect to $server..."} is never reached. the catch {"cannot connect to WMI..."} is what terminates the function.

When I add 'Try-Connection $ipaddr | out-Null' to the first 'try' the catch block is executed.

PowerDBAKlaas avatar Oct 18 '16 12:10 PowerDBAKlaas

I have test it for "divide by zero" error and it is OK! Yes, I continue to see my "transparent" drive :smile: PS: have used Commit https://github.com/sqlcollaborative/dbatools/commit/c83f2da67222ecff49aba28cc1ea0617563e0265

ClaudioESSilva avatar Oct 25 '16 13:10 ClaudioESSilva

hella fixed

potatoqualitee avatar Jan 28 '17 19:01 potatoqualitee