无法使Powershell列表仅在最终结果完成时显示

Unable to make Powershell list display only when the final result is done

我正在遍历 vmware cluster/datastores 基础架构,试图只发布相关的 clusters/datastores。 一切都很好,但是当我填充数据存储时,我为每个发现得到另一行。

# 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"

我知道在检查数据存储是否存在 key/value 之前我正在输出信息,但我期待它被填充...

我自己无法对此进行测试,但它表明您收集的输出不是 csv。 要创建包含 headers 和数据行的正确 CSV 文件,您需要收集 objects,而不是一系列字符串并使用 Export-Csv 保存。

像这样:

# 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