Automation

 View Only
  • 1.  Retrieving disk usage

    Posted Aug 27, 2018 01:24 PM

    Hi All

    I am using the script provided here VMs Disk Usage Report  to retrieve the disk usage information in my environments. I have altered it slightly but am getting this error :

    Attempted to divide by zero. PS1:28 char :13

    Attempted to divide by zero  PS1:56 char : 9

    Any idea whats wrong ?

    #import-module vmware.vimautomation.core

    $viserver = read-host "please enter your vCenter hostname / IP"

    $cred = get-credential

    connect-viserver $viserver -Credential $cred

    $rp = read-host -Prompt "What resource pool do you want to gather information for"

    $vms = get-resourcepool -Name $rp | get-vm | where {$_.PowerState -eq 'Poweredon'}

    $FinalResult = foreach($vm in $vms){

           $totalCapacity = $totalFree = 0

           $vm.Guest.Disk | %{

                $object = New-Object -TypeName PSObject

                $Capacity = "{0:N0}" -f [math]::Round($_.Capacity / 1MB)

                $totalCapacity += $_.Capacity

                $totalFree += $_.FreeSpace

                $Freespace = "{0:N0}" -f [math]::Round($_.FreeSpace / 1MB)

                $Percent = [math]::Round(($FreeSpace)/ ($Capacity) * 100)

                $PercentFree = "{0:P0}" -f ($Percent/100)

                $object | Add-Member -MemberType NoteProperty -Name "Server Name" -Value $vm.Name

                $object | Add-Member -MemberType NoteProperty -Name Disk -Value $_.DiskPath

                $object | Add-Member -MemberType NoteProperty -Name "Capacity MB" -Value $Capacity

                $object | Add-Member -MemberType NoteProperty -Name "Free MB" -Value $FreeSpace

                $object | Add-Member -MemberType NoteProperty -Name "Free %" -Value $PercentFree

                $finalResult += $object

            }

            $object = New-Object -TypeName PSObject

            $object | Add-Member -MemberType NoteProperty -Name "Server Name" -Value $vm.Name

            $object | Add-Member -MemberType NoteProperty -Name Disk -Value 'SubTotal'

            $object | Add-Member -MemberType NoteProperty -Name "Capacity MB" -Value ("{0:N0}" -f ($totalCapacity/1MB))

            $object | Add-Member -MemberType NoteProperty -Name "Free MB" -Value ("{0:N0}" -f ($totalFree/1MB))

            $object | Add-Member -MemberType NoteProperty -Name "Free %" -Value ("{0:P0}" -f ($totalFree/$totalCapacity))

            $finalResult += $object

        }

    $finalResult | Export-Csv "C:\DiskUsageReport.csv" -NoTypeInformation  -UseCulture    # dump the report to .csv



  • 2.  RE: Retrieving disk usage

    Posted Aug 29, 2018 08:44 PM

    I am fairly new to scripting, but I found that when I modifed Luc's script from using the get-view method to using get-resourcepool (or get-cluster in my case) I had to change $vm.Guest.Disk to $vm.Guest.Disks.   It will still return (cannot divide by 0) errors if VMTools is not running on the guest, but it should provide more data.  

    You may also want to make sure you fully define the array for $finalResult before starting the loop.    

     

    $FinalResult = foreach($vm in $vms){ 

    \/\/\/\/\/

    $finalResult = @()

    foreach....



  • 3.  RE: Retrieving disk usage
    Best Answer

    Posted Sep 02, 2018 06:10 AM

    You are hitting the difference between .Net objects, as returned by PowerCLI cmdlets like Get-VM, and vSphere objects, as returned by Get-View and mapped by Extensiondata on .Net objects.

    The names of some properties are not exactly the same for both type of objects.

    And the Guest.DIsk(s) property is on of those.

    You can keep using the original script, which assumes you are passing it vSphere objects, by using the Get-View cmdlet as the last cmdlet in the line that fills that $vms variable.

    $vms = Get-ResourcePool -Name $rp | Get-VM | where {$_.PowerState -eq 'Poweredon'} | Get-View



  • 4.  RE: Retrieving disk usage

    Posted Sep 04, 2018 08:48 AM

    Thanks guys

    I added the | get-view but not getting this error

    does not contain a method named 'op_addition'

    At c:\users\mleahy\desktop\script.ps1:42 char :13

    $finalresult +=4object



  • 5.  RE: Retrieving disk usage

    Posted Sep 04, 2018 09:01 AM

    Since you are already capturing the result in $finalResult at the foreach line, try replacing the line

    $finalResult += $object

    with

    $object



  • 6.  RE: Retrieving disk usage

    Posted Sep 10, 2018 02:27 PM

    awesome, thanks