VMware Cloud Community
dbutch1976
Hot Shot
Hot Shot
Jump to solution

Import from csv then export all results to new csv

Hi all,

Simple question here, but I can't seem to figure it out on my own. I am able to import a csv file and the script provides all the formation I'm looking for like so;

$legclusters = Import-Csv C:\output\legclusters.csv
foreach ($legcluster in $legclusters){
get-cluster -name $legcluster.clusname
}

The output is: 

Name HAEnabled HAFailover DrsEnabled DrsAutomationLevel
Level
---- --------- ---------- ---------- ------------------
Mycluster1 False 1 True FullyAutomated
Mycluster2 False 1 True FullyAutomated
Mycluster3 False 1 True FullyAutomated

However, if I attempt to export this information to csv using this script I only get the last result rather than everything. How do I get all the output instead of just the last result?

$legclusters = Import-Csv C:\output\legclusters.csv

foreach ($legcluster in $legclusters){
get-cluster -name $legcluster.clusname | Select Name,HAEnabled,HAFailoverLevel,DrsEnabled,DrsAutomationLevel | Export-Csv -NoTypeInformation c:\output\expclusDRSHA.csv -UseCulture
}

 

 

 

 

 

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

You are overwriting the CSV file with each loop of the foreach block.

You can use the Append switch

$legclusters = Import-Csv C:\output\legclusters.csv

foreach ($legcluster in $legclusters){
  Get-Cluster -Name $legcluster.clusname | 
  Select Name,HAEnabled,HAFailoverLevel,DrsEnabled,DrsAutomationLevel | 
  Export-Csv -NoTypeInformation c:\output\expclusDRSHA.csv -UseCulture -Append
}


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

You are overwriting the CSV file with each loop of the foreach block.

You can use the Append switch

$legclusters = Import-Csv C:\output\legclusters.csv

foreach ($legcluster in $legclusters){
  Get-Cluster -Name $legcluster.clusname | 
  Select Name,HAEnabled,HAFailoverLevel,DrsEnabled,DrsAutomationLevel | 
  Export-Csv -NoTypeInformation c:\output\expclusDRSHA.csv -UseCulture -Append
}


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

Reply
0 Kudos
knikolov
VMware Employee
VMware Employee
Jump to solution

In PowerShell you can actually easily get an array of all the names like this $legclusters.clusname, and since Get-Cluster can accept an array for the name parameter you don't really need the foreach and you can do it like that

get-cluster -name $legclusters.clusname | Select Name,HAEnabled,HAFailoverLevel,DrsEnabled,DrsAutomationLevel | Export-Csv -NoTypeInformation c:\output\expclusDRSHA.csv -UseCulture

This will return all the clusters and will export them in CSV. The issue with your code is that you're retrieving the clusters one by one and you export them one by one in the csv file which causes every next cluster to overwrite the csv file. That's why you end up with only the latest cluster.

dbutch1976
Hot Shot
Hot Shot
Jump to solution

I literally thought using -append was so simple it couldn't possibly be the answer and didn't bother trying. Thanks LucD, that was fast, even for you!

Reply
0 Kudos
dbutch1976
Hot Shot
Hot Shot
Jump to solution

This really simplifies it, thanks knikolov!

Reply
0 Kudos