VMware Cloud Community
simcloud
Contributor
Contributor
Jump to solution

Script to capture sizes all files associated to a VM

Hi there.

I am looking for a script that will export the size of all files associated with a virtual machine to a CSV. Goal is to find bytes on a disk per VM.

VMs could have more than one disk.

Headers

VM nameDatastoreDisk typeDisk capacitySpace used
VM1Name of datastore the VM sits onThin or thickSpace allocated to VMSum total across all files (vmdk, log, swap, etc..)

I have found samples close.. but not exact.

Thanks!

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Which one? The 2nd script?

That's just a pipe to Export-Csv.

Get-VM -PipelineVariable vm | ForEach-Object -Process {

   $vm.ExtensionData.LayoutEx.File |

   where { $_.Type -notmatch "^disk" } |

   ForEach-Object -Process {

   $obj = [ordered]@{

   VM = $vm.Name

   File = $_.Name

   FileType = $_.Type

   SizeGB = [math]::Round($_.Size / 1GB, 0)

   }

   New-Object -TypeName PSObject -Property $obj

   }

} | Export-Csv -Path .\report.csv -NoTypeInformation -UseCulture


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
8 Replies
LucD
Leadership
Leadership
Jump to solution

Have you already tried my script in Yadr – A Vdisk Reporter?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
simcloud
Contributor
Contributor
Jump to solution

Hi Luc,

I hadn't, but just did, works well - just need more information. I am after a total of all files against a VM not just VMDK or snap.. Here I have .hlog, .vswp, .vmx, ect. need a total of all files in the size column for each VM.

vmware.jpg

Thanks.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

The other files can be easily obtained with this

Get-VM -PipelineVariable vm | ForEach-Object -Process {

   $vm.ExtensionData.LayoutEx.File |

   where { $_.Type -notmatch "^disk" } |

   ForEach-Object -Process {

   $obj = [ordered]@{

   VM = $vm.Name

   File = $_.Name

   FileType = $_.Type

   SizeGB = [math]::Round($_.Size / 1GB, 0)

   }

   New-Object -TypeName PSObject -Property $obj

   }

}

Then it would a matter of combining both reports.
The reason for that is that vDisks require special calculations due to the Thick/Thin and snapshot possibilities.
All the other files are rather straightforward.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
simcloud
Contributor
Contributor
Jump to solution

Works great, small ask - need to get this into CSV, can you help with the Export-Csv command? Thanks.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Which one? The 2nd script?

That's just a pipe to Export-Csv.

Get-VM -PipelineVariable vm | ForEach-Object -Process {

   $vm.ExtensionData.LayoutEx.File |

   where { $_.Type -notmatch "^disk" } |

   ForEach-Object -Process {

   $obj = [ordered]@{

   VM = $vm.Name

   File = $_.Name

   FileType = $_.Type

   SizeGB = [math]::Round($_.Size / 1GB, 0)

   }

   New-Object -TypeName PSObject -Property $obj

   }

} | Export-Csv -Path .\report.csv -NoTypeInformation -UseCulture


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
simcloud
Contributor
Contributor
Jump to solution

Many thanks!

If there is a way to get these two scripts combined that would be even better.. but for now I will do some excel work Smiley Happy

0 Kudos
LucD
Leadership
Leadership
Jump to solution

The problem might be that you can't have different lines in the same CSV.
So we would have to merge the lines.

I threw together the following 'quick and dirty' style, so if it helps

$yadrData = Import-Csv -Path .\Yadr-report.csv -UseCulture

$fileData = Import-Csv -Path .\report.csv -UseCulture

$yadr = @{ }

$yadrNames = $yadrData | Get-Member -MemberType NoteProperty | where { $_.Name -ne 'VMName', '' } | % { $_.Name }

$fileNames = $fileData | Get-Member -MemberType NoteProperty | % { $_.Name }

$yadrData | Group-Object -Property VMName |

ForEach-Object -Process {

   $yadr.Add($_.Name, $_.Group)

}


$fileData | Group-Object -Property VM |

ForEach-Object -Process {

   foreach ($file in $_.Group)

   {

   foreach ($name in $yadrNames)

   {

   $file | Add-Member -MemberType NoteProperty -Name $name -Value $null

   }

   $file

   }

   foreach ($hd in $yadr.Item($_.Name))

   {

   foreach ($name in $fileNames)

   {

   $hd | Add-Member -MemberType NoteProperty -Name $name -Value $null

   }

   $hd.VM = $hd.VMname

   $hd.File = $hd.Path

   $hd.FileType = 'VMDK'

   $hd.PSObject.Properties.Remove('VMname')

   $hd.PSObject.Properties.Remove('Path')

   $hd

   }

} | Export-Csv -Path .\combined.csv -NoTypeInformation -UseCulture


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
simcloud
Contributor
Contributor
Jump to solution

Worked perfectly - thank you!

0 Kudos