VMware Cloud Community
BobFriend
Contributor
Contributor
Jump to solution

Need help outputting VM and its datastore location to text

I need to export of a list of specific VMs with their respective datastore names. I have gotten as far as importing the list, but I cannot get the results to display both the vm name and datastore name. Once I accomplish that I would like to export the results to CSV.

Can someone give provide me some pointers or point me in the right direction?

This is what I have so far...

Import-Csv C:\temp\inputFile.csv | %{Get-VM $_.vm | Get-Datastore | Select-Object -ExpandProperty name -First 1 |FT -AutoSize}

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

The ForEach loop doesn't place anything on the pipeline, so we collect all output (to the console) in a variable, and then pipe that to the Export-Csv cmdlet.

$report = Import-Csv C:\temp\inputFile.csv | %{

    Get-VM $_.vm |

    Select Name,

        @{N='Datastore';E={(Get-Datastore -RelatedObject $_).Name -join '|'}}

}

$report | Export-Csv -Path .\report.csv -NoTypeInformation -UseCulture


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

View solution in original post

Reply
0 Kudos
4 Replies
LucD
Leadership
Leadership
Jump to solution

Try like this

Import-Csv C:\temp\inputFile.csv | %{

    Get-VM $_.vm |

    Select Name,

        @{N='Datastore';E={(Get-Datastore -RelatedObject $_).Name -join '|'}}

}


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

Reply
0 Kudos
BobFriend
Contributor
Contributor
Jump to solution

That's perfect. Now how do I export output to CSV? Smiley Happy

If I run the following I only get output for one line of the input file.

Import-Csv C:\temp\inputFile.csv | %{

    Get-VM $_.vm |

    Select Name,

        @{N='Datastore';E={(Get-Datastore -RelatedObject $_).Name -join '|'}}| Export-Csv c:\temp\output.csv -NoTypeInformation

}

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

The ForEach loop doesn't place anything on the pipeline, so we collect all output (to the console) in a variable, and then pipe that to the Export-Csv cmdlet.

$report = Import-Csv C:\temp\inputFile.csv | %{

    Get-VM $_.vm |

    Select Name,

        @{N='Datastore';E={(Get-Datastore -RelatedObject $_).Name -join '|'}}

}

$report | Export-Csv -Path .\report.csv -NoTypeInformation -UseCulture


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

Reply
0 Kudos
BobFriend
Contributor
Contributor
Jump to solution

Perfect. Thank you so much!

Reply
0 Kudos