gpt4 book ai didi

azure - 多个订阅的所有资源的标签,PowerShell 输出为 csv

转载 作者:行者123 更新时间:2023-12-03 01:25:36 24 4
gpt4 key购买 nike

我是 PowerShell 新手,尝试获取多个 Azure 订阅中所有资源的标签并在 csv 文件中发送输出。下面是脚本,它在屏幕上写入输出,但不在 csv 文件中。谁能纠正我,将所有数据导出到 csv 文件。感谢您的帮助。

SubscriptionIds = Get-Content -Path "path for SubscriptionID.txt"  
Foreach($SubscriptionId in $SubscriptionIds)
{
Try{$null = Set-AzContext -SubscriptionId $SubscriptionId}
catch [Exception] {write-host ("Error occured: " + $($_.Exception.Message)) -ForegroundColor Red;Exit}
Write-Host "Azure Login Session successful" -ForegroundColor Green -BackgroundColor Black

# Initialise output array
$Output = @()

# Collect all the groups from the current subscription
$ResourceGroups = Get-AzResourceGroup
foreach ($ResourceGroup in $ResourceGroups) {
Write-Host "Resource Group =$($ResourceGroup.ResourceGroupName)"
$resourceNames= Get-AzResource -ResourceGroupName $ResourceGroup.ResourceGroupName
$tags=Get-AzTag -ResourceId $ResourceGroup.ResourceId
foreach($key in $tags.Properties.TagsProperty.Keys)
{
Write-Host "`t ResourceGroup = $($ResourceGroup.ResourceGroupName) `t TagKey= $($key) `t Value = $($tags.Properties.TagsProperty.Item($($key)))"
}
foreach($res in $resourceNames)
{
Write-Host "ResourceName = $($res.Name)"
$tags=Get-AzTag -ResourceId $res.ResourceId
foreach($key in $tags.Properties.TagsProperty.Keys)
{
Write-Host "`t `t ResourceGroup = $($ResourceGroup.ResourceGroupName) `t ResourceName = $($res.Name) `t TagKey= $($key) `t Value = $($tags.Properties.TagsProperty.Item($($key)))"
}
}
}
# Sent the final output to CSV
$Output | Export-Csv -Path test.csv -NoClobber -NoTypeInformation -Append -Encoding UTF8 -Force
}

最佳答案

您没有将使用 Write-Host cmdlet 打印的输出添加到 $output 数组中:

SubscriptionIds = Get-Content -Path "path for SubscriptionID.txt"  
Foreach ($SubscriptionId in $SubscriptionIds) {
Try { $null = Set-AzContext -SubscriptionId $SubscriptionId }
catch [Exception] { write-host ("Error occured: " + $($_.Exception.Message)) -ForegroundColor Red; Exit }
Write-Host "Azure Login Session successful" -ForegroundColor Green -BackgroundColor Black

# Initialise output array
$Output = @()

# Collect all the groups from the current subscription
$ResourceGroups = Get-AzResourceGroup
foreach ($ResourceGroup in $ResourceGroups) {
Write-Host "Resource Group =$($ResourceGroup.ResourceGroupName)"
$resourceNames = Get-AzResource -ResourceGroupName $ResourceGroup.ResourceGroupName
$tags = Get-AzTag -ResourceId $ResourceGroup.ResourceId
foreach ($key in $tags.Properties.TagsProperty.Keys) {
$Output += "`t ResourceGroup = $($ResourceGroup.ResourceGroupName) `t TagKey= $($key) `t Value = $($tags.Properties.TagsProperty.Item($($key)))"
Write-Host "`t ResourceGroup = $($ResourceGroup.ResourceGroupName) `t TagKey= $($key) `t Value = $($tags.Properties.TagsProperty.Item($($key)))"
}
foreach ($res in $resourceNames) {
Write-Host "ResourceName = $($res.Name)"
$tags = Get-AzTag -ResourceId $res.ResourceId
foreach ($key in $tags.Properties.TagsProperty.Keys) {
$Output += "`t ResourceGroup = $($ResourceGroup.ResourceGroupName) `t TagKey= $($key) `t Value = $($tags.Properties.TagsProperty.Item($($key)))"
Write-Host "`t `t ResourceGroup = $($ResourceGroup.ResourceGroupName) `t ResourceName = $($res.Name) `t TagKey= $($key) `t Value = $($tags.Properties.TagsProperty.Item($($key)))"
}
}
}
# Sent the final output to CSV
$Output | Export-Csv -Path test.csv -NoClobber -NoTypeInformation -Append -Encoding UTF8 -Force
}

关于azure - 多个订阅的所有资源的标签,PowerShell 输出为 csv,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63419087/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com