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
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
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
Thanks!!! 🙂
I thought foreach was required if we wanted to go through the list
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