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
}
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
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
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.
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!
This really simplifies it, thanks knikolov!