bernz
Enthusiast
Enthusiast

Export CSV - Multiple VMs hard disks information

All

I started a powercli below to extract harddisk information and generate a csv file.

The problem here is that information being exported is only for one vm, and not all vms included in  GetVMList.txt are being exported.

Anyone can give me solution, I appreciate any comments.

$VMs = Get-content E:\vmw\GetVMList.txt

Write-Host "Collecting Vm hard disks information..." -ForegroundColor Yellow

Foreach ($vm in $VMs){

  Get-vm $vm | get-harddisk | select Parent,StorageFormat,DiskType,Filename,CapacityGB,Persistence,Name | Export-Csv e:\vmw\output\VM_disks.csv –NoTypeInformation

}

Write-Host "Done." -foreground Green

0 Kudos
2 Replies
LucD
Leadership
Leadership

You are overwriting the CSV file inside the ForEach loop for each VM.

You can transform this into 1 pipeline construct, something like this

Write-Host "Collecting Vm hard disks information..." -ForegroundColor Yellow

Get-VM -Name (Get-content -Path E:\vmw\GetVMList.txt) |

Get-HardDisk |

select Parent,StorageFormat,DiskType,Filename,CapacityGB,Persistence,Name |

Export-Csv e:\vmw\output\VM_disks.csv –NoTypeInformation


Write-Host "Done." -foreground Green


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

0 Kudos
Robert637
Contributor
Contributor

I can manage to get 2 scripts to get VM with Thin or Thick details and another for VM disk space.

For VM thin for Thick Details

Get-VM | ForEach {Get-harddisk -VM $_ | Select-Object -Property Parent, Name, StorageFormat, CapacityGB, @{N='OS Total Free Space GB of all partitions'; E={ [math]::round((Get-VMGuest $vm | ForEach-Object { ($_.Disks) } | Measure-Object -property FreeSpaceGB -sum).Sum)}}} | convertTo-html | out-file C:\Users\USERA\Desktop\VMThin&Thick.htm

For VM Disk Space DQFanFeedback

ForEach ($VM in Get-VM ){($VM.Extensiondata.Guest.Disk | Select @{N="Name";E={$VM.Name}},DiskPath, @{N="Capacity(GB)";E={[math]::Round($_.Capacity/ 1GB)}}, @{N="Free Space(GB)";E={[math]::Round($_.FreeSpace / 1GB)}}, @{N="Free Space %";E={[math]::Round(((100* ($_.FreeSpace))/ ($_.Capacity)),0)}})}

NOTE - To get the VM Disk space output in html format use below given lines. (copy above given lines in a txt file and save it as FreeSpace.ps1 (what ever name you can give) and run the below lines in powercli.

0 Kudos