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
}
}
You seem to be doing the same thing multiple times.
Why not simplify it to
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
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
You seem to be doing the same thing multiple times.
Why not simplify it to
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
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
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.
It's one fo the Common Parameters, see PipelineVariable
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
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.
You can use a Where-clause
Get-VM | Get-VMGuest -PipelineVariable guest |
where{$_.Disks -ne $null} |
ForEach-Object -Process {
$_.Disks |
where{($_.FreeSpaceGB/$_.CapacityGB) -lt 0.1} |
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
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference