带有PowerShell输出到csv的多个订阅的所有资源的标记

时间:2020-08-14 19:36:47

标签: azure powershell csv

我是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 
}

2 个答案:

答案 0 :(得分:2)

您没有将使用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 
}

答案 1 :(得分:0)

尝试这个。

$Output = [System.Collections.ArrayList]::new()
$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) {
            $csvObject = New-Object PSObject
            Add-Member -inputObject $csvObject -memberType NoteProperty -name "ResourceGroup" -value $ResourceGroup.ResourceGroupName
            Add-Member -inputObject $csvObject -memberType NoteProperty -name "ResourceName" -value ''
            Add-Member -inputObject $csvObject -memberType NoteProperty -name "TagKey" -value $key
            Add-Member -inputObject $csvObject -memberType NoteProperty -name "Value" -value $tags.Properties.TagsProperty.Item($($key))
            $Output.Add($csvObject)

            #$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) {
                $csvObject = New-Object PSObject
                Add-Member -inputObject $csvObject -memberType NoteProperty -name "ResourceGroup" -value $ResourceGroup.ResourceGroupName
                Add-Member -inputObject $csvObject -memberType NoteProperty -name "ResourceName" -value $res.Name
                Add-Member -inputObject $csvObject -memberType NoteProperty -name "TagKey" -value $key
                Add-Member -inputObject $csvObject -memberType NoteProperty -name "Value" -value $tags.Properties.TagsProperty.Item($($key))               
                $Output.Add($csvObject)

                #$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)))"
            }
        }
    }


$Output | Export-Csv -Path test.csv -NoClobber -NoTypeInformation -Append -Encoding UTF8 -Force