hello everyone
I want to export the following script as a CSV file.
Original :
ForEach ($VM in ( Get-VM |Get-View)){($info = $VM.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)}})|Format-Table}
Export CSV:
ForEach ($VM in ( Get-VM |Get-View)){($info = $VM.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)}})| Export-Csv -path c:\test.csv -NoTypeInformation |Format-Table }
It will be saved only once, not all information.
What is the problem?
Thanks!
Try like this
$VM.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)}}
}
$report | Export-Csv -path c:\test.csv -NoTypeInformation -UseCulture
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Try like this
$VM.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)}}
}
$report | Export-Csv -path c:\test.csv -NoTypeInformation -UseCulture
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
It works very well. Thank you for your answer.