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
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
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
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
Thanks Luc!
For some reason when I specified the folder it also gives me the vm's for the sub folders.
Add the -NoRecursion switch on the Get-VM cmdlet
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
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.
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
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
Strange indeed. Could you include the output of both (as a screenshot) ?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
This is what I see when I click on the folder in the client:
Here's the output when I use the -NoRecursion switch:
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
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.
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