VMware Cloud Community
AGFlora
Enthusiast
Enthusiast
Jump to solution

How to modify code to send results to seperate cells for csv export

Hi,

How can I modify the following code so that the results are on separate lines and cells for each virtual machine?

Get-Datastore |

Where-Object {$_.Name -notMatch ("ESX|Datastore1")} | %{

New-Object PSObject -Property @{ClusterName = (Get-DatastoreCluster -Datastore $_ | Select -ExpandProperty Name)

Name = $_.name 

Folder = (Get-VM -Datastore $_ | Select -ExpandProperty folder)

VMs = (Get-VM -Datastore $_ | Select -ExpandProperty Name)

  }

}

I'm guessing that a foreach loop can be fit in somewhere?

Thanks

Reply
0 Kudos
1 Solution

Accepted Solutions
kunaludapi
Expert
Expert
Jump to solution

Below is the code you can use data to export to excel.

Get-Datastore |

Where-Object {$_.Name -Notmatch ("ESX|Datastore1")} | %{

New-Object PSObject -Property @{ClusterName = (Get-DatastoreCluster -Datastore $_ | Select -ExpandProperty Name)

Name = $_.name

Folder = ((Get-VM -Datastore $_ | Select -ExpandProperty folder) -join "`n")

VMs = ((Get-VM -Datastore $_ | Select -ExpandProperty Name) -join "`n")

}

}

http://vcloud-lab.com

--------------------------------------------------------------- Kunal Udapi Sr. System Architect (Virtualization, Networking And Storage) http://vcloud-lab.com http://kunaludapi.blogspot.com VMWare vExpert 2014, 2015, 2016 If you found this or other information useful, please consider awarding points for "Correct" or "Helpful".

View solution in original post

Reply
0 Kudos
4 Replies
kunaludapi
Expert
Expert
Jump to solution

Below is the code you can use data to export to excel.

Get-Datastore |

Where-Object {$_.Name -Notmatch ("ESX|Datastore1")} | %{

New-Object PSObject -Property @{ClusterName = (Get-DatastoreCluster -Datastore $_ | Select -ExpandProperty Name)

Name = $_.name

Folder = ((Get-VM -Datastore $_ | Select -ExpandProperty folder) -join "`n")

VMs = ((Get-VM -Datastore $_ | Select -ExpandProperty Name) -join "`n")

}

}

http://vcloud-lab.com

--------------------------------------------------------------- Kunal Udapi Sr. System Architect (Virtualization, Networking And Storage) http://vcloud-lab.com http://kunaludapi.blogspot.com VMWare vExpert 2014, 2015, 2016 If you found this or other information useful, please consider awarding points for "Correct" or "Helpful".
Reply
0 Kudos
AGFlora
Enthusiast
Enthusiast
Jump to solution

Hi Kunai,

Thanks for your response but when I tired to export the data to a csv file I get the following message

"An empty pipe element is not allowed" Do I tried adding the Select-Object cmdlet:

Select-Object -Property ClusterName, DatastoreName, Folder, VM

but when I pipe to export-csv the data isn't getting sent to the csv file.

What I'm trying to accomplish is export the data in a csv file like this:

CusterName  DataStoreName  Folder  VMs

Reply
0 Kudos
kunaludapi
Expert
Expert
Jump to solution

test.png

I am not getting any error while running. (Running against local datastore, don't have shared)

--------------------------------------------------------------- Kunal Udapi Sr. System Architect (Virtualization, Networking And Storage) http://vcloud-lab.com http://kunaludapi.blogspot.com VMWare vExpert 2014, 2015, 2016 If you found this or other information useful, please consider awarding points for "Correct" or "Helpful".
Reply
0 Kudos
AGFlora
Enthusiast
Enthusiast
Jump to solution

I was able to get the code to work but I need a separate line for both the folder and VMs column instead of joining the output.

Thanks

Reply
0 Kudos