VMware Cloud Community
TangKing2moya
Contributor
Contributor
Jump to solution

HOW Make "VM GUEST DISK SIZES Script " export CSV file

 

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!

 

1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Try like this

$report = ForEach ($VM in (Get-View -ViewType VirtualMachine)){

   $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

View solution in original post

2 Replies
LucD
Leadership
Leadership
Jump to solution

Try like this

$report = ForEach ($VM in (Get-View -ViewType VirtualMachine)){

   $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

TangKing2moya
Contributor
Contributor
Jump to solution

It works very well. Thank you for your answer.

Reply
0 Kudos