$report1 = Get-Datastore "yyyyyy" |Get-VM |
Select Name,
@{N="GuestUsageGB";E={[math]::Round(($_.Guest.Disks | %{$_.CapacityGB - $_.FreeSpaceGB} | Measure-Object -Sum | Select -ExpandProperty Sum),1)}},
@{N=“GuestCapacityGB";E={[math]::Round(($_.Guest.Disks | %{ $_.Capacity / 1GB} | Measure-Object -Sum | Select -ExpandProperty Sum),2)}},
@{N=“FreeSpaceGB";E={[math]::Round(($_.Guest.Disks | %{ $_.FreeSpace / 1GB} | Measure-Object -Sum | Select -ExpandProperty Sum),2)}},
@{N="GuestUsage(%)";E={[math]::Round(($_.Guest.Disks | %{ 100-(100*($_.FreeSpaceGB / $_.CapacityGB))} | Measure-Object -Average | Select -ExpandProperty Average),2)}},
@{N='SnapshotSizeGB';E={[math]::Round((Get-Snapshot -VM $_ | Measure-Object -Property SizeGB -sum).Sum,2)}}
$sum = $report1 | Measure-Object -sum "GuestUsageGB","GuestCapacityGB",“FreeSpaceGB"
$row = "" | Select "GuestUsageGB","GuestCapacityGB",“FreeSpaceGB"
$row."GuestUsageGB" = 'Total'
$row."GuestUsageGB" = $sum | where{$_.Property -eq "GuestUsageGB"} | select -ExpandProperty Sum
$row."GuestCapacityGB" = $sum | where{$_.Property -eq "GuestCapacityGB"} | select -ExpandProperty Sum
$row."FreeSpaceGB" = $sum | where{$_.Property -eq "FreeSpaceGB"} | select -ExpandProperty Sum
$report1 += $row
$report1 | export-csv VMHDD_REPORT_$datefile.csv -NoTypeInformation
I am using the above script to get Guest HDD usage but having issue at (Guestusage %) percentage, its not providing the correct values.
Hi
i have run the script and $diskNum = 1 result not exporting to csv
and i also want to get
#System_Group = (Get-TagAssignment -Category "System Group" -Entity $vm.Name).Tag.Name
#System_Type = (Get-TagAssignment -Category "System Type"-Entity $vm.Name).Tag.Name
You seem to be taking the average of the usage percentages per disk.
This doesn't take into account that some guest disks might be much bigger than other guest disks.
Try with this one perhaps?
Get-Datastore "xxxx" |Get-VM |
Select Name,
@{N="GuestUsageGB";E={[math]::Round(($_.Guest.Disks | %{$_.CapacityGB - $_.FreeSpaceGB} | Measure-Object -Sum | Select -ExpandProperty Sum),1)}},
@{N=“GuestCapacityGB";E={[math]::Round(($_.Guest.Disks | %{ $_.Capacity / 1GB} | Measure-Object -Sum | Select -ExpandProperty Sum),2)}},
@{N=“FreeSpaceGB";E={[math]::Round(($_.Guest.Disks | %{ $_.FreeSpace / 1GB} | Measure-Object -Sum | Select -ExpandProperty Sum),2)}},
@{N="GuestUsage(%)";E={
$capacityGB = $_.Guest.Disks | Measure-Object -Property CapacityGB -Sum | Select -ExpandProperty Sum
$freeGB = $_.Guest.Disks | Measure-Object -Property FreeSpaceGB -Sum | Select -ExpandProperty Sum
[math]::Round((($capacityGB - $freeGB)/$capacityGB*100),2)}},
@{N='SnapshotSizeGB';E={[math]::Round((Get-Snapshot -VM $_ | Measure-Object -Property SizeGB -sum).Sum,2)}}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi LucD
Sorry for dealy
Thanks for the updated script, it has given the exact results for what i am looking. I want to know can we get VMDK size of each VM in the same script.
And i also i have one more requirement, actually i am using three different scripts to get VM details and later combing all the results to one out put can we combine all the three scripts and get one out put file in CSV.
This adds the total VMDK capacity size to the report.
Get-Datastore "xxx" |Get-VM |
Select Name,
@{N="GuestUsageGB";E={[math]::Round(($_.Guest.Disks | %{$_.CapacityGB - $_.FreeSpaceGB} | Measure-Object -Sum | Select -ExpandProperty Sum),1)}},
@{N=“GuestCapacityGB";E={[math]::Round(($_.Guest.Disks | %{ $_.Capacity / 1GB} | Measure-Object -Sum | Select -ExpandProperty Sum),2)}},
@{N=“FreeSpaceGB";E={[math]::Round(($_.Guest.Disks | %{ $_.FreeSpace / 1GB} | Measure-Object -Sum | Select -ExpandProperty Sum),2)}},
@{N="GuestUsage(%)";E={
$capacityGB = $_.Guest.Disks | Measure-Object -Property CapacityGB -Sum | Select -ExpandProperty Sum
$freeGB = $_.Guest.Disks | Measure-Object -Property FreeSpaceGB -Sum | Select -ExpandProperty Sum
[math]::Round((($capacityGB - $freeGB)/$capacityGB*100),2)}},
@{N='VMDKSizeGB';E={(Get-HardDisk -VM $_ | Measure-Object -Property CapacityGB -Sum).Sum}},
@{N='SnapshotSizeGB';E={[math]::Round((Get-Snapshot -VM $_ | Measure-Object -Property SizeGB -sum).Sum,2)}}
To know if scripts can be combined, I would need to see the scripts.
A good practice is to first compile a sample line of the resulting report.
What should be shown in each line, and how much redundancy are you willing to have in a report.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
You try to combine informatoin about the VM, the total disk usage and the VMDK info in one report.
That would mean there would be a line per VMDK, which a lot of the information from the VM and the total disk usage being replicated on each line.
Is that what you want?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi
Yes i am looking the same report.
Try with this one
Get-Datastore -PipelineVariable ds | Get-VM |
ForEach-Object -Process {
Write-Host "Looking at $($_.Name)"
$obj = [ordered]@{
VM = $_.Name
VMState = $_.ExtensionData.summary.runtime.powerState
Boottime = $_.ExtensionData.Runtime.BootTime
TotalNics = $_.ExtensionData.summary.config.numEthernetCards
Portgroup = Get-View -Id $_.ExtensionData.Network -Property Name | select -ExpandProperty Name
OS = $_.ExtensionData.Config.GuestFullName
Hostname = $_.ExtensionData.guest.hostname
IPAddress = $_.ExtensionData.guest.ipAddress
VMHost = Get-View -Id $_.ExtensionData.Runtime.Host -property Name | select -ExpandProperty Name
ProvisionedSpaceGB = [math]::Round(($_.ExtensionData.Summary.Storage.Committed + $vm.Summary.Storage.UnCommitted)/1GB,0)
TotalUsedSpaceGB = [math]::Round($_.ExtensionData.Summary.Storage.Committed/1GB,0)
MemoryReservation = $_.ExtensionData.resourceconfig.memoryallocation.reservation
CPUreservation = $_.ExtensionData.resourceconfig.cpuallocation.reservation
TotalCPU = $_.ExtensionData.summary.config.numcpu
TotalMemoryGB = [math]::Round($_.ExtensionData.summary.config.MemorysizeMB/1KB,0)
Memory = $_.ExtensionData.summary.config.memorysizemb
MemoryUsage = $_.ExtensionData.summary.quickStats.guestMemoryUsage
ToolsStatus = $_.ExtensionData.guest.toolsstatus
ToolsVersion = $_.ExtensionData.config.tools.toolsversion
TimeSync = $_.ExtensionData.Config.Tools.SyncTimeWithHost
HardwareVersion = $_.ExtensionData.config.Version
Datastore = $_.ExtensionData.Config.DatastoreUrl[0].Name
CBT = $_.ExtensionData.Config.ChangeTrackingEnabled
Notes = $_.ExtensionData.Config.Annotation.ToString()
FaultTolerance = $_.ExtensionData.Runtime.FaultToleranceState
SnapshotName = &{$script:snaps = Get-Snapshot -VM $_.ExtensionData.Name; $script:snaps.Name -join ","}
SnapshotDate = $script:snaps.Created -join ","
SnapshotSizeGB = $script:snaps.SizeGB -join ","
ToolsState = $_.Guest.State
GuestUsageGB = [math]::Round(($vm.Guest.Disks | %{$_.CapacityGB - $_.FreeSpaceGB} | Measure-Object -Sum | Select -ExpandProperty Sum),1)
GuestCapacityGB = [math]::Round(($_.Guest.Disks | %{ $_.Capacity / 1GB} | Measure-Object -Sum | Select -ExpandProperty Sum),2)
FreeSpaceGB = [math]::Round(($_.Guest.Disks | %{ $_.FreeSpace / 1GB} | Measure-Object -Sum | Select -ExpandProperty Sum),2)
'GuestUsage(%)' = &{
$capacityGB = $_.Guest.Disks | Measure-Object -Property CapacityGB -Sum | Select -ExpandProperty Sum
$freeGB = $_.Guest.Disks | Measure-Object -Property FreeSpaceGB -Sum | Select -ExpandProperty Sum
if($capacityGB){
[math]::Round((($capacityGB - $freeGB)/$capacityGB*100),2)
}
}
VMDKSizeGB = (Get-HardDisk -VM $_ | Measure-Object -Property CapacityGB -Sum).Sum
}
$diskNum = 1
if($_.Guest.Disks){
$_.Guest.Disks | ForEach-Object -Process {
$obj.Add("Disk$($DiskNum)_Path",$_.Path)
$obj.Add("Disk$($DiskNum)_Capacity(GB)",([math]::Round($_.CapacityGB,1)))
$obj.Add("Disk$($DiskNum)_FreeSpace(GB)",([math]::Round(($_.FreeSpaceGB),1)))
$obj.Add("Disk$($DiskNum)_USedSpace(GB)",([math]::Round($_.CapacityGB - $_.FreeSpaceGB,1)))
$obj.Add("Disk$($DiskNum)_USedSpace%",([math]::Round((1 - ($_.FreeSpace)/$_.Capacity)*100,1)))
$DiskNum++
}
}
New-Object PSObject -Property $obj
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi
i have run the script and $diskNum = 1 result not exporting to csv
and i also want to get
#System_Group = (Get-TagAssignment -Category "System Group" -Entity $vm.Name).Tag.Name
#System_Type = (Get-TagAssignment -Category "System Type"-Entity $vm.Name).Tag.Name
Try like this
Get-Datastore -PipelineVariable ds | Get-VM |
ForEach-Object -Process {
Write-Host "Looking at $($_.Name)"
$obj = [ordered]@{
VM = $_.Name
VMState = $_.ExtensionData.summary.runtime.powerState
Boottime = $_.ExtensionData.Runtime.BootTime
TotalNics = $_.ExtensionData.summary.config.numEthernetCards
Portgroup = Get-View -Id $_.ExtensionData.Network -Property Name | select -ExpandProperty Name
OS = $_.ExtensionData.Config.GuestFullName
Hostname = $_.ExtensionData.guest.hostname
IPAddress = $_.ExtensionData.guest.ipAddress
VMHost = Get-View -Id $_.ExtensionData.Runtime.Host -property Name | select -ExpandProperty Name
ProvisionedSpaceGB = [math]::Round(($_.ExtensionData.Summary.Storage.Committed + $vm.Summary.Storage.UnCommitted)/1GB,0)
TotalUsedSpaceGB = [math]::Round($_.ExtensionData.Summary.Storage.Committed/1GB,0)
MemoryReservation = $_.ExtensionData.resourceconfig.memoryallocation.reservation
CPUreservation = $_.ExtensionData.resourceconfig.cpuallocation.reservation
TotalCPU = $_.ExtensionData.summary.config.numcpu
TotalMemoryGB = [math]::Round($_.ExtensionData.summary.config.MemorysizeMB/1KB,0)
Memory = $_.ExtensionData.summary.config.memorysizemb
MemoryUsage = $_.ExtensionData.summary.quickStats.guestMemoryUsage
ToolsStatus = $_.ExtensionData.guest.toolsstatus
ToolsVersion = $_.ExtensionData.config.tools.toolsversion
TimeSync = $_.ExtensionData.Config.Tools.SyncTimeWithHost
HardwareVersion = $_.ExtensionData.config.Version
Datastore = $_.ExtensionData.Config.DatastoreUrl[0].Name
CBT = $_.ExtensionData.Config.ChangeTrackingEnabled
Notes = $_.ExtensionData.Config.Annotation.ToString()
FaultTolerance = $_.ExtensionData.Runtime.FaultToleranceState
SnapshotName = &{$script:snaps = Get-Snapshot -VM $_.ExtensionData.Name; $script:snaps.Name -join ","}
SnapshotDate = $script:snaps.Created -join ","
SnapshotSizeGB = $script:snaps.SizeGB -join ","
ToolsState = $_.Guest.State
GuestUsageGB = [math]::Round(($vm.Guest.Disks | %{$_.CapacityGB - $_.FreeSpaceGB} | Measure-Object -Sum | Select -ExpandProperty Sum),1)
GuestCapacityGB = [math]::Round(($_.Guest.Disks | %{ $_.Capacity / 1GB} | Measure-Object -Sum | Select -ExpandProperty Sum),2)
FreeSpaceGB = [math]::Round(($_.Guest.Disks | %{ $_.FreeSpace / 1GB} | Measure-Object -Sum | Select -ExpandProperty Sum),2)
'GuestUsage(%)' = &{
$capacityGB = $_.Guest.Disks | Measure-Object -Property CapacityGB -Sum | Select -ExpandProperty Sum
$freeGB = $_.Guest.Disks | Measure-Object -Property FreeSpaceGB -Sum | Select -ExpandProperty Sum
if($capacityGB){
[math]::Round((($capacityGB - $freeGB)/$capacityGB*100),2)
}
}
VMDKSizeGB = (Get-HardDisk -VM $_ | Measure-Object -Property CapacityGB -Sum).Sum
System_Group = (Get-TagAssignment -Category "System Group" -Entity $_).Tag.Name
System_Type = (Get-TagAssignment -Category "System Type" -Entity $_).Tag.Name
}
$diskNum = 1
if($_.Guest.Disks){
$_.Guest.Disks | ForEach-Object -Process {
$obj.Add("Disk$($DiskNum)_Path",$_.Path)
$obj.Add("Disk$($DiskNum)_Capacity(GB)",([math]::Round($_.CapacityGB,1)))
$obj.Add("Disk$($DiskNum)_FreeSpace(GB)",([math]::Round(($_.FreeSpaceGB),1)))
$obj.Add("Disk$($DiskNum)_USedSpace(GB)",([math]::Round($_.CapacityGB - $_.FreeSpaceGB,1)))
$obj.Add("Disk$($DiskNum)_USedSpace%",([math]::Round((1 - ($_.FreeSpace)/$_.Capacity)*100,1)))
$DiskNum++
}
}
New-Object PSObject -Property $obj
} | Export-Csv -Path .\report.csv -NoTypeInformation -UseCulture
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
PS: why did you mark this thread as "Assumed answered", while my answer obviously doesn't answer your question?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi
the initial question was answered and it was perfect. now its enhancing the query so i have marked as assured answer
Ok, but obviously you didn't consider one of my answers as the Correct Answer.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
i have tried new script now i am able to get the Tag, but still the below result is not capturing in CSV file
$diskNum = 1
if($_.Guest.Disks){
$_.Guest.Disks | ForEach-Object -Process {
$obj.Add("Disk$($DiskNum)_Path",$_.Path)
$obj.Add("Disk$($DiskNum)_Capacity(GB)",([math]::Round($_.CapacityGB,1)))
$obj.Add("Disk$($DiskNum)_FreeSpace(GB)",([math]::Round(($_.FreeSpaceGB),1)))
$obj.Add("Disk$($DiskNum)_USedSpace(GB)",([math]::Round($_.CapacityGB - $_.FreeSpaceGB,1)))
$obj.Add("Disk$($DiskNum)_USedSpace%",([math]::Round((1 - ($_.FreeSpace)/$_.Capacity)*100,1)))
$DiskNum++
}
}
New-Object PSObject -Property $obj
My last version included an Export-Csv at the end, I don't see that in the code you included.
Or do you mean something else with "result is not capturing in CSV file"?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I have run your latest script which included the export path but in the csv file Guest Disk partition are not captured.
Please find the attached image in which it show fields which are captured in CSV file
Works for me.
Are you sure you have the VMware Tools installed on those VMs, and that at least some of the VMs are powered on?
Otherwise there will be no local disk info available.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
VMTools are installed and latest version and server are in power on mode and we have 5 partions servers alsol ike c dive,e drive, f drive p drive,
This might be an issue with the Export-Csv cmdlet.
The 1st row in an array determines how many columns will be written to the CSV file.
If the 1st entry does not have any guest disk information, these columns will not be present for the other entries.
Try changing the last line in the script to
} | Sort-Object -Property {($_ | Get-Member -MemberType NoteProperty).Count} -Descending |
Export-Csv -Path .\report.csv -NoTypeInformation -UseCulture
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference