VMware Cloud Community
andrew_uk
Enthusiast
Enthusiast
Jump to solution

Export VM with Datastore and Datastore Cluster name

Hello

This prints perfectly, but cannot export if I do export-csv (it doesn't input any information) 

$vmfile = Import-Csv "myfile"
foreach ($vm in $vmfile) {
Get-VM $vm.vmname |
Select Name,
@{N="Datastore";E={[string]::Join(',',(Get-Datastore -Id $_.DatastoreIDList | Select -ExpandProperty Name))}},@{N="Cluster";E={Get-DatastoreCluster -VM $_}}
}

How can I export? 

 

Thanks

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

The foreach statement doesn't place anything in the pipeline.
You can rewrite that code to use the pipeline.

$vmfile = Import-Csv "myfile"

Get-VM $vmfile.vmname |
Select Name,
    @{N="Datastore";E={[string]::Join(',',(Get-Datastore -Id $_.DatastoreIDList | 
    Select -ExpandProperty Name))}},
        @{N="Cluster";E={Get-DatastoreCluster -VM $_}} |
Export-Csv -Path .\report.csv -NoTypeInformation -UseCulture


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

View solution in original post

3 Replies
LucD
Leadership
Leadership
Jump to solution

The foreach statement doesn't place anything in the pipeline.
You can rewrite that code to use the pipeline.

$vmfile = Import-Csv "myfile"

Get-VM $vmfile.vmname |
Select Name,
    @{N="Datastore";E={[string]::Join(',',(Get-Datastore -Id $_.DatastoreIDList | 
    Select -ExpandProperty Name))}},
        @{N="Cluster";E={Get-DatastoreCluster -VM $_}} |
Export-Csv -Path .\report.csv -NoTypeInformation -UseCulture


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

andrew_uk
Enthusiast
Enthusiast
Jump to solution

Thanks!!! 🙂 

I thought foreach was required if we wanted to go through the list 

Tags (1)
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Not really, if you place an array in the pipeline, the ext cmdlets will take those objects one-by-one.
Kind of an implicit foreach.

You can also use Foreach-Object, that does place objects in the pipeline


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