I am iterating through vmware cluster/datastores infrastructure trying to put out only relevant clusters/datastores. Everything if fine but when I'm populating the datastores I get another line for each finding.
# Read date with custom format
$date = (Get-Date -Format 'yyyy-MM-dd HH:mm:ss.ss').ToString()
# Read all clusters
$clusters = @(Get-Cluster)
# Create an empty list required to populate with exclusion datastores per cluster
$ds_list = @()
# Create an empty list required to populate the content of the .csv that will be exported
$content = @()
# Iterate through each cluster
foreach ($cluster in $clusters){
# Check if the cluster is being monitored and continue with the check
if ($cluster.CustomFields['Attribute' -eq "Yes"]){
# Gather all datastores per cluster
$datastores = @($cluster | Get-Datastore)
# Read the cluster volumeType
$volumeType = $cluster.CustomFields['VolumeType']
# Populate the content list with clusters that don't have exclusions
$content += "$date, cluster_name='$cluster', volume_type='$volumeType', exclusion_list=''"
# Iterate through each datastore from the current cluster and check if it's excluded
foreach ($datastore in $datastores){
# If the datastore is excluded, populate the exclusion_list part of the content
if (($datastore.ExtensionData.Value.Key -eq "123") -and ($datastore.ExtensionData.Value.Value -eq "Yes")){
# Populate the empty list with the datastore name that's excluded
$ds_list += $datastore.Name
# Format the list and join it (the format should be 'item1,item2,item3')
$ds_list = $(($ds_list | Select -Unique) -join ",")
# Populate the content with the exclusion_list
$content += "$date, cluster_name='$cluster', volume_type='$volumeType', exclusion_list='$ds_list'"
}
}
}
$content | Out-File -FilePath ".\file.csv" -Encoding ascii
}
# CSV Expected data format:
# YYYY-MM-DD HH:mm:ss.sss, cluster_name="Cluster1 name here", volume_type="volume1 Type", exclusion_list=""
# YYYY-MM-DD HH:mm:ss.sss, cluster_name="Cluster2 name here", volume_type="volume2 Type", exclusion_list="item1,item2,item3"
# CSV retrieved format:
# YYYY-MM-DD HH:mm:ss.sss, cluster_name="Cluster2 name here", volume_type="volume2 Type", exclusion_list=""
# YYYY-MM-DD HH:mm:ss.sss, cluster_name="Cluster2 name here", volume_type="volume2 Type", exclusion_list="item1"
# YYYY-MM-DD HH:mm:ss.sss, cluster_name="Cluster2 name here", volume_type="volume2 Type", exclusion_list="item1,item2"
# YYYY-MM-DD HH:mm:ss.sss, cluster_name="Cluster2 name here", volume_type="volume2 Type", exclusion_list="item1,item2,item3"
I know that I'm outputing the information before checking if the datastore has the key/value present but I was expecting for it to be populated...
I cannot test this myself, but it sems the output you are gathering is not csv.
To create a proper CSV file with headers and rows of data, you need to collect objects, not a series of strings and use Export-Csv
to save.
Something like this:
# Read date with custom format
$date = (Get-Date -Format 'yyyy-MM-dd HH:mm:ss.ss').ToString()
# Read all clusters
$clusters = @(Get-Cluster)
# Iterate through each cluster
$content = foreach ($cluster in $clusters){
# Check if the cluster is being monitored and continue with the check
if ($cluster.CustomFields['Attribute' -eq "Yes"]){
# Gather all datastores per cluster
$datastores = @($cluster | Get-Datastore)
# Read the cluster volumeType
$volumeType = $cluster.CustomFields['VolumeType']
# output an object with clusters that don't have exclusions
[PsCustomObject]@{
'Date' = $date
'cluster_name' = $cluster.Name
'volume_type' = $volumeType
'exclusion_list' = ''
}
# Iterate through each datastore from the current cluster and check if it's excluded
$ds_list = foreach ($datastore in $datastores){
# If the datastore is excluded, populate the exclusion_list part of the content
if (($datastore.ExtensionData.Value.Key -eq "123") -and ($datastore.ExtensionData.Value.Value -eq "Yes")){
# output the datastore name to be captured in variable $ds_list
$datastore.Name
}
}
if ($ds_list) {
# output an object with the exclusion_list
[PsCustomObject]@{
'Date' = $date
'cluster_name' = $cluster.Name
'volume_type' = $volumeType
'exclusion_list' = ($ds_list | Sort-Object -Unique) -join ","
}
}
}
}
# output on console
$content
# output to CSV
$content | Export-Csv -Path ".\file.csv" -NoTypeInformation