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



我正在对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文件,需要收集对象,而不是一系列字符串,并使用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

最新更新