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

无法使Powershell列表仅在最终结果完成时显示,powershell,vmware,powercli,Powershell,Vmware,Powercli,我正在遍历vmware群集/数据存储基础架构,试图只推出相关的群集/数据存储。 如果一切正常的话,但是当我填充数据存储时,我会为每个发现获得另一行 # 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

我正在遍历vmware群集/数据存储基础架构,试图只推出相关的群集/数据存储。 如果一切正常的话,但是当我填充数据存储时,我会为每个发现获得另一行

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

我知道在检查数据存储是否存在键/值之前,我正在输出信息,但我希望填充它…

我自己无法测试,但它表明您收集的输出不是csv。 要创建包含标题和数据行的正确CSV文件,您需要收集对象,而不是一系列字符串,并使用
导出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

作为文件输出,您的目标不是CSV。为什么不创建一个适当的CSV文件,文件头和下面的数据行?我试图导出CSV,但输出不符合预期-我只看到类型和长度。此外,我根本不需要标题构建数据存储排除列表,然后添加到$content,而不是作为ForEach循环的一部分。谢谢!我这样做了,它起作用了。