Azure导出所有用户权限---powershell命令

Posted 无踪无影

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Azure导出所有用户权限---powershell命令相关的知识,希望对你有一定的参考价值。

 
直接运行脚本
 
 
 
 
#requires -Version 3.0 -Modules AzureRM.Resources
param(
    [switch]
    $GroupRolesByUser
)
$ErrorActionPreference = ‘Stop‘

Login-AzureRmAccount -ErrorVariable loginerror -Environment AzureChinaCloud
If ($loginerror -ne $null)
{
Throw {"Error: An error occured during the login process, please correct the error and try again."}
}
Function Select-Subs
{
    $ErrorActionPreference = ‘SilentlyContinue‘
    $Menu = 0
    $Subs = @(Get-AzureRmSubscription | select Name,ID,TenantId)
    Write-Host "Please select the subscription you want to use" -ForegroundColor Green;
    $Subs | %{Write-Host "[$($Menu)]" -ForegroundColor Cyan -NoNewline ;Write-host ". $($_.Name)";$Menu++;}
    $selection = Read-Host "Please select the Subscription Number - Valid numbers are 0 - $($Subs.count -1)"
    If ($Subs.item($selection) -ne $null)
    { Return @{name = $subs[$selection].Name;ID = $subs[$selection].ID} }
}
$SubscriptionSelection = Select-Subs
Select-AzureRmSubscription -SubscriptionName $SubscriptionSelection.Name -ErrorAction Stop

$ADUser = Get-AzureRmADUser
function Resolve-AzureAdUsers {
param(
        [string]$Displayname
    )
    ForEach($i in $ADUser){
        if( $i.displayName -eq $Displayname){return $i.UserPrincipalName}
       
    }
}
function Resolve-AzureAdGroupMembers
{
    param(
        [guid]
        $GroupObjectId,
        $GroupList = (Get-AzureRmADGroup)
    )
   
    $VerbosePreference = ‘continue‘
    Write-Verbose -Message (‘Resolving {0}‘ -f $GroupObjectId)
    $group = $GroupList | Where-Object -Property Id -EQ -Value $GroupObjectId
    $groupMembers = Get-AzureRmADGroupMember -GroupObjectId $GroupObjectId
    Write-Verbose -Message (‘Found members {0}‘ -f ($groupMembers.DisplayName -join ‘, ‘))
    $parentGroup = @{
        Id          = $group.Id
        DisplayName = $group.DisplayName
        #UserPrincipalName = $group.UserPrincipalName
    }
    $groupMembers |
    Where-Object -Property Type -NE -Value Group |
    Select-Object -Property Id, DisplayName,UserPrincipalName, @{
        Name       = ‘ParentGroup‘
        Expression = { $parentGroup }
    }
    $groupMembers |
    Where-Object -Property type -EQ -Value Group |
    ForEach-Object -Process {
        Resolve-AzureAdGroupMembers -GroupObjectId $_.Id -GroupList $GroupList
    }
}
$roleAssignments = Get-AzureRmRoleAssignment -IncludeClassicAdministrators
$members = $roleAssignments | ForEach-Object -Process {
    Write-Verbose -Message (‘Processing Assignment {0}‘ -f $_.RoleDefinitionName)
    $roleAssignment = $_
   
    if($roleAssignment.ObjectType -eq ‘Group‘)
    {
        Resolve-AzureAdGroupMembers -GroupObjectId $roleAssignment.ObjectId `
        | Select-Object -Property Id,
            SignInName,DisplayName,UserPrincipalName,
            ParentGroup, @{
                Name       = ‘RoleDefinitionName‘
                Expression = { $roleAssignment.RoleDefinitionName }
            }, @{
                Name       = ‘Scope‘
                Expression = { $roleAssignment.Scope }
            }, @{
                Name       = ‘CanDelegate‘
                Expression = { $roleAssignment.CanDelegate }
            }
    }
    else
    {
        $roleAssignment | Select-Object -Property @{
                Name       = ‘Id‘
                Expression = { $_.ObjectId }
            },
            SignInName, DisplayName,UserPrincipalName,
            @{
                Name       = ‘RoleDefinitionName‘
                Expression = { $roleAssignment.RoleDefinitionName }
            },
            Scope,
            CanDelegate
    }
}
if($GroupRolesByUser)
{
    $members |
    Sort-Object -Property DisplayName, RoleDefinitionName `
    |
    Group-Object -Property DisplayName `
    |
    Select-Object -Property Count,
        Name,
        @{
            Name       = ‘RoleDefinitions‘
            Expression = { $_.Group.RoleDefinitionName -join ‘, ‘ }
        },
        ParentGroup
}
else
{
    $members|Select-Object -Property Scope,Displayname,
    @{
            Name = ‘LoginName‘
            Expression = {$(Resolve-AzureAdUsers -Displayname $_.DisplayName)}
    },RoleDefinitionName|Out-GridView
}
















































































































以上是关于Azure导出所有用户权限---powershell命令的主要内容,如果未能解决你的问题,请参考以下文章

Azure按订阅,资源组,资源类型导出所有资源powershell命令

我可以使用 Powershell 导出 azure devops 管道运行历史吗?

添加第一个域用户的 PowerShell 脚本 [权限问题]

如何在没有管理员权限的 Azure Web 应用上安装 PowerShell 模块?

使用 powershell 导出 azure db 时出错

powershell Powershell命令用于导出Azure AD并导入到本地AD