Automation

 View Only
  • 1.  Get Guest OS Disk Space

    Posted Jul 22, 2020 06:48 PM

    Hi,

    I'm trying to get the stats on disks within the VM Guest OS. The script I have below kind of works, but some of the results are outputting to the CSV file as System.Object[].

    What am I missing here? I've tried both Select and Select-Object and get the same result. Is there a more efficient way of doing this rather than get all the disks and then a ForEach loop for each disk? Note we are running vSphere 6.7 so we can't take advantage of the new Get-VMGuestDisk cmdlet.

    $dsk = (Get-VM)

    ForEach ($vm in $dsk){

        $vName = $vm.Name

        $query = (Get-VM $vName | Get-VMGuest).Disks | Select-Object Path, @{N="CapacityGB";E={[math]::round($_.CapacityGB)}}, @{N="FreeSpaceGB";E={[math]::round($_.FreeSpaceGB)}}, @{N="Percent";E={[int]($_.FreeSpaceGB/$_.CapacityGB * 100)}}

       

        ForEach ($disk in $query) {

            $path = $query.path

            $cap = $query.CapacityGB

            $free = $query.FreeSpaceGB

            $per = $query.percent

         

            $obj = New-Object PSObject

                $obj | add-member Noteproperty "Server" $vName

                $obj | add-member Noteproperty "Path" $path

                $obj | add-member Noteproperty "CapacityGB By" $cap

                $obj | add-member Noteproperty "FreeSpaceGB" $free

                $obj | add-member Noteproperty "Percent" $per

               

                $obj | Export-CSV "$PSScriptRoot\disks.csv" -NoTypeInformation -Append

        }

    }



  • 2.  RE: Get Guest OS Disk Space
    Best Answer

    Posted Jul 22, 2020 07:07 PM

    You seem to be doing the same thing multiple times.

    Why not simplify it to

    Get-VM | Get-VMGuest -PipelineVariable guest |

    where{$_.Disks -ne $null} |

    ForEach-Object -Process {

        $_.Disks | ForEach-Object -Process {

            New-Object PSObject -Property ([ordered]@{

                Server = $guest.VM.Name

                Path = $_.Path

                CapacityGB = [math]::round($_.CapacityGB)

                FreeSpaceGB = [math]::round($_.FreeSpaceGB)

                Percent = [int]($_.FreeSpaceGB/$_.CapacityGB * 100)  

            })

        }

    } | Export-CSV "$PSScriptRoot\disks.csv" -NoTypeInformation -UseCulture



  • 3.  RE: Get Guest OS Disk Space

    Posted Jul 22, 2020 07:26 PM

    That worked perfectly, thanks! Can you point me to any documentation/info on the switch -PipeLineVariable? I've not seen this before, so would like to understand how it works and how to use it.



  • 4.  RE: Get Guest OS Disk Space

    Posted Jul 22, 2020 07:38 PM

    It's one fo the Common Parameters, see PipelineVariable



  • 5.  RE: Get Guest OS Disk Space

    Posted May 04, 2022 02:24 PM

    Can we use this Free Percentage to filter output, like select and display disk space with less that 10% free space rather then all VM guest disk information.



  • 6.  RE: Get Guest OS Disk Space

    Posted May 04, 2022 04:25 PM

    You can use a Where-clause