VMware Cloud Community
AGFlora
Enthusiast
Enthusiast
Jump to solution

Trouble exporting output to csv file

Hi

I can't seem to get the following to export to a csv file:

get-folder $folder | get-vm | %{ $_.Name; ($_ | get-datastore | select Name).Name } |

Export-Csv test.csv -NoTypeInfo

For some reason unknown to me it will only export to a text file.

Thanks

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

If you have VMs that are stored on multiple datastores, you can do it like this

Get-Folder $folder | Get-VM | 
Select Name, 
@
{N="DS Name";E={[string]::Join(',',(Get-Datastore -RelatedObject $_ | Select -ExpandProperty Name))}} |
Export-Csv test.csv -NoTypeInfo


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

View solution in original post

0 Kudos
12 Replies
LucD
Leadership
Leadership
Jump to solution

That's because of the ForEach statement, that doesn't place any objects in the pipeline.

Try it like this

Get-Folder $folder | Get-VM | 
Select Name,@{N="DS Name";E={Get-Datastore -RelatedObject $_ | Select -ExpandProperty Name}} | 
Export-Csv
test.csv -NoTypeInfo


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

0 Kudos
LucD
Leadership
Leadership
Jump to solution

If you have VMs that are stored on multiple datastores, you can do it like this

Get-Folder $folder | Get-VM | 
Select Name, 
@
{N="DS Name";E={[string]::Join(',',(Get-Datastore -RelatedObject $_ | Select -ExpandProperty Name))}} |
Export-Csv test.csv -NoTypeInfo


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

0 Kudos
AGFlora
Enthusiast
Enthusiast
Jump to solution

Thanks Luc!

For some reason when I specified the folder it also gives me the vm's for the sub folders.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Add the -NoRecursion switch on the Get-VM cmdlet


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

0 Kudos
AGFlora
Enthusiast
Enthusiast
Jump to solution

This is strange...when I use the -NoRecursion switch I get only a few vm's and not what I see when I click on the folder. When I don't use the -NoRecursion

switch I get all of the vm's including the sub folders.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

What do you mean with "click on the folder" ? In the vSphere client under the Virtual Machines tab ?

That also gives you a recursive view.


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

0 Kudos
AGFlora
Enthusiast
Enthusiast
Jump to solution

Yes when I click on the virtual machine tab in the client....but the vm's in the output file are totally different than when I click on the vm tab...like I said...strange

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Strange indeed. Could you include the output of both (as a screenshot) ?


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

0 Kudos
AGFlora
Enthusiast
Enthusiast
Jump to solution

This is what I see when I click on the folder in the client:

1-30-2013 12-18-14 PM.jpg

Here's the output when I use the -NoRecursion switch:

ss2.jpg

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Are you sure that $folder points to the same folder as the one you use in the vSphere client ?

Or perhaps there are multiple entries in $folder ?


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

0 Kudos
AGFlora
Enthusiast
Enthusiast
Jump to solution

Yes $folder points to the folder in vSphere and there are no multiple entries. When I use the -NoRecursion switch the output is only some of the vm's that are in the root folder.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Strange, the resultins in the vSphere client and from the script seem to be completely different.

I'm afraid I don't have any other possible explanation for this phenomena


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

0 Kudos