0

我有十几个订阅,很难同时输出来自所有订阅的所有资源的所有角色分配。我目前正在使用下面的那个,但是每个 csv 文件的每个订阅都会循环输出。我如何最好将输出输出到一张纸上?

Get-AzSubscription | foreach-object {

    Write-Verbose -Message "Changing to Subscription $($_.Name)" -Verbose

    Set-AzContext -TenantId $_.TenantId -SubscriptionId $_.Id -Force
    $Name     = $_.Name
    $TenantId = $_.TenantId
    $SubId    = $_.SubscriptionId  

    Get-AzRoleAssignment -IncludeClassicAdministrators | Select-Object RoleDefinitionName,DisplayName,SignInName,ObjectType,Scope,
    @{name="TenantId";expression = {$TenantId}},@{name="SubscriptionName";expression = {$Name}},@{name="SubscriptionId";expression = {$SubId}} -OutVariable ra

    # Also export the individual subscriptions to excel documents on your Desktop.
    # One file per subscription
    $ra | Export-Csv -Path C:\scripts\Export-AzRoleAssignment\$Name.csv -NoTypeInformation

}
4

1 回答 1

0

您可以使用该命令的-Append参数。Export-Csv

所以,你的最后一行看起来像这样:

$ra | Export-Csv -Path C:\scripts\Export-AzRoleAssignment\csv_with_everything.csv -NoTypeInformation -Append

或者您可以在运行后将所有 csv 文件合并为大文件,例如https://stackoverflow.com/a/43972739/1713589

于 2020-05-15T08:06:10.887 回答