以下代码生成一个列表:
$users = Get-AzAksCluster -ResourceGroupName $resource_group -SubscriptionId $subscription_id |
Select-Object -Property DnsPrefix, KubernetesVersion, Location, Name, ResourceGroupName, @{e = { $_.Tags.creator | ConvertTo-Json | ConvertFrom-Json }; l = 'Tags' } |
Sort-Object -Descending Creator |
ForEach-Object {
new-object psobject -Property @{
AzurePortalFQDN = $_.AzurePortalFQDN
DnsPrefix = $_.DnsPrefix
Fqdn = $_.Fqdn
Id = $_.Id
KubernetesVersion = $_.KubernetesVersion
Location = $_.Location
Name = $_.Name
NodeResourceGroup = $_.NodeResourceGroup
ProvisioningState = $_.ProvisioningState
ResourceGroupName = $_.ResourceGroupName
ServicePrincipalProfile = $_.ServicePrincipalProfile
Creator = if ($_.Tags) { $_.Tags } else { "zNo Creator Found" }
}
} |
Select-Object -Property Creator |
Where-Object {$_.Creator -ne "zNo Creator Found"} |
Format-Table -HideTableHeaders
当我尝试遍历列表时,foreach 中的 $users 变量为空,并且 $user 永远不会被填充
foreach ($user in $users) {
Write-Output $user
$Output = Get-AzAksCluster -ResourceGroupName $resource_group -SubscriptionId $subscription_id |
Select-Object -Property DnsPrefix, KubernetesVersion, Location, Name, ResourceGroupName, @{e = { $_.Tags.creator | ConvertTo-Json | ConvertFrom-Json }; l = 'Tags' } |
Where-Object -FilterScript {$_.Tags.creator -like $user} |
Sort-Object -Descending Creator |
ForEach-Object {
new-object psobject -Property @{
AzurePortalFQDN = $_.AzurePortalFQDN
DnsPrefix = $_.DnsPrefix
Fqdn = $_.Fqdn
Id = $_.Id
KubernetesVersion = $_.KubernetesVersion
Location = $_.Location
Name = $_.Name
NodeResourceGroup = $_.NodeResourceGroup
ProvisioningState = $_.ProvisioningState
ResourceGroupName = $_.ResourceGroupName
ServicePrincipalProfile = $_.ServicePrincipalProfile
Creator = if ($_.Tags) { $_.Tags } else { "zNo Creator Found" }
}
}
Write-Output $Output
有人能指出为什么我可以写入输出 $users 并看到用户,但 foreach 无法对其进行迭代吗?
答案1
答案来自杰夫米尔纳是正确的。删除后Format-Table
问题解决了。