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 name | Datastore | Disk type | Disk capacity | Space used |
---|---|---|---|---|
VM1 | Name of datastore the VM sits on | Thin or thick | Space allocated to VM | Sum total across all files (vmdk, log, swap, etc..) |
I have found samples close.. but not exact.
Thanks!
Which one? The 2nd script?
That's just a pipe to Export-Csv.
$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
Have you already tried my script in Yadr – A Vdisk Reporter?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
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.
Thanks.
The other files can be easily obtained with this
$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
Works great, small ask - need to get this into CSV, can you help with the Export-Csv command? Thanks.
Which one? The 2nd script?
That's just a pipe to Export-Csv.
$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
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
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
$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
Worked perfectly - thank you!