Assign Owner access on multiple resources in azure - azure

I have 300+ resource groups in Azure and i want to assign Owner access to 250 resource groups. Individually I have to assign the owner access on each resource group is time consuming task. Is there is any other way to do this?
Hi All,
I have 300+ resource groups in Azure subscription and I want to assign 'Owner' role to 250 resource groups only. Individually I have to assign the 'owner' role on each resource group is time consuming task. Is there is any other way to do this?

I tried to reproduce same in my environment I got the results successfully like below.
To assign owner access to all the resource groups you can make use of below PowerShell comment like below:
Connect-AzAccount
$resourceGroups = Get-AzResourceGroup
$resourceGroups | ForEach-Object {
New-AzRoleAssignment -ObjectId <USER OBJECTID> -RoleDefinitionName "Owner" -Scope $_.ResourceId
}
To get object ID of credentials or user Go to azure active directory -> user -> select user -> object ID like below:
According to your scenario as you have 300+ resource groups and you want to assign owner access to 250 resource groups try the below.
Go to resource group and export csv file and remove resource group which you don't want to assign access keep only resource group which you want to assign owner role in csv file like below:
When I use the PowerShell command I am able to assign owner access to list of multiple resource groups like below:
# Import CSV file with resource group names
$resourceGroups = Import-Csv -Path <PATH OF YOUR CSV FILE>
foreach ($resourceGroup in $resourceGroups) {
# Assign the role to the user or group
New-AzRoleAssignment -ObjectId ad4818XXXXXXXX -RoleDefinitionName "Owner" -ResourceGroupName $resourceGroup.ResourceGroupName
}
Output:

Related

In Azure, is it possible to control access control (IAM) for a file shares using Powershell?

I have been trying to use Powershell to assign users access control privileges for a file share in Azure. What I have been doing is:
Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
Login to Azure
az login
Get User Information and isolate the User Principle Name
$UPN = (Get-AzADUser -StartsWith $UserName).UserPrincipalName
Get the row that contains Scope
$TempScope = az storage share-rm show -g $ResourceGroup --storage-account $StorageAccount --name $FileShareName | Select-String -Pattern '/subscriptions' -CaseSensitive
Change that row to a string
$TempScope = $TempScope | foreach {$_.ToString()}
Remove the first 8 characters
$scope = $TempScope.Substring(9)
Get Name of Role
$FileShareRole = Get-AzRoleDefinition $RoleName
Assign the role to the target identity with the specified scope
New-AzRoleAssignment -SignInName $UPN -RoleDefinitionName $FileShareRole.Name -Scope $scope
It has not been giving the user access to the file share but instead to microsoft.storage/storageaccounts/fileservices/shares.
Yes, You can add Access Control to the file share. In the above-shared script, you are using the long approach to pull the resource ID of the file share. We have made some changes to the above script.
Here is the Modified script to add Access Control to the file Share:
$UserName="<UserName>"
$ResourceGroup="<RGName>"
$StorageAccount="<strgAccountName>"
$FileShareName="<FileShareName>"
$RoleName="Storage File Data SMB Share Reader"
$UPN = (Get-AzADUser -StartsWith $UserName).UserPrincipalName
$TempScope = az storage share-rm show -g $ResourceGroup --storage-account $StorageAccount --name $FileShareName --query id -o tsv
$FileShareRole = Get-AzRoleDefinition $RoleName
New-AzRoleAssignment -SignInName $UPN -RoleDefinitionName $FileShareRole.Name -Scope $scope
Here are the three Azure built-in roles for granting share-level permissions to users:
Storage File Data SMB Share Reader allows read access in Azure Storage file shares over SMB.
Storage File Data SMB Share Contributor allows read, write, and delete access in Azure Storage file shares over SMB.
Storage File Data SMB Share Elevated Contributor allows read, write, delete and modify NTFS permissions in Azure Storage file shares over SMB.

Keep RBAC Permissions When Transferring Resource

I'm transferring a resource (storage account) from one subscription to another. During the transfer the RBAC permissions will be removed from my resource. I know you can download the RBAC permissions to have a copy, but what is the best way to add them back to my resource? Do I have to do it manually one by one? Felt like there was an easier way.
You could download the role assignments as a csv file(without Inherited), then use the azure powershell to assign the roles to your storage account after transferring below. Please make sure your subscriptions are in the same AAD tenant, otherwise it will not work.
$csv = Import-Csv -Path C:\Users\joyw\Desktop\role-assignments-2021-11-08.csv
$scope = "<storage account resource id after transferring>"
foreach($rs in $csv){
New-AzRoleAssignment -ObjectId $csv.ObjectId -Scope $scope -RoleDefinitionName $csv.RoleDefinitionName
}
Thanks Joy. I should mention these resources won't be in the same ADD tenant. I'm moving the resource from one tenant to another.

Get all role assignments of an Azure AD Principal

I have an Azure environment with multiple subscriptions and resources. My requirement is to have a functionality where if I pass a user name or SPN name, it gives me all azure resources (from management group to azure resource) where that user/spn has access to and what access it is (reader/ data reader etc).
Major catch is - I want PIM role assignments too. Is there a way to get it?
Options explored
https://learn.microsoft.com/en-us/rest/api/authorization/role-assignments but this gives role assignments per scope. I want per user/spn
https://learn.microsoft.com/en-us/azure/role-based-access-control/role-assignments-list-portal it does not cover PIM assignments and gives assignments per subscription only
Azure Resource Graph Explorer - this does not cover role assignments at all
Apart from iterating through 50 subscriptions, fetching role assignments per scope and then comparing object id, is there any better way?
You can use the below Powershell Script to get the role-assignement for a Service Principal Name in multiple Subscriptions.
Connect-AzAccount
$tenantID = "yourTenantID"
$spn = "serviceprincipalname"
$user= Get-AzADUser -UserPrincipalName $spn
$subscriptions = Get-AzSubscription -TenantId $tenantID
#$subscriptions.Id
foreach ($subscription in $subscriptions) {
$set = Set-AzContext -Subscription $subscription
$set
$roleassignment= Get-AzRoleAssignment -ObjectId $user.Id
$roleassignment
}
Output:
Reference:
Install the Azure Az PowerShell module | Microsoft Docs
This might help you:
az role assignment list --all --assignee <Pricipal_ID>
https://learn.microsoft.com/en-us/cli/azure/role/assignment?view=azure-cli-latest#az-role-assignment-list
#Gjoshevski has shared a useful command, but you still need to loop through each subscription to list the role assignments on each subscription (if you have more than one in your tenant).
The following Azure CLI commands will do the job.
spID='<ObjectID here>' # ObjectID of an Applicatin or Service Principal
$tenantId='<TenantID here>'
az login --tenant 'Your Tenant'
$sub_ids=$(az account list --query id -o tsv)
foreach ($sub_id in $sub_ids) {
az account set --subscription $sub_id
"Subscription Name: $(az account show --query name)"
az role assignment list --all --assignee $spID --include-inherited --include-groups -o table
}
Note that the above is PowerShell syntax but is using Aure CLI commands

How to create Azure Role Assignment scoped to a certain resource type

I'm trying to create an Azure Role Assignment which assigns the User Access Administrator role to a service principal but only for Azure Data Factory resources.
I see plenty of documentation on setting scopes by subscription, resource group, or even resource, but can't figure out how to set it for all resources of a certain type.
I've tried this PowerShell command which runs successfully but doesn't have the intended effect. The service principal still can't perform the actions of that role on ADF resources.
New-AzRoleAssignment -ObjectId ddddddd-dddd-dddd-dddd-dddddddddddd -RoleDefinitionId 18d7d88d-d35e-4fb5-a5c3-7773c20a72d9 -Scope "/providers/Microsoft.DataFactory"
I've also tried experimenting with wildcards in the scope, but this seems unsupported: /subscriptions/dddddddd-dddd-dddd-dddd-dddddddddddd/resourceGroups/*/providers/Microsoft.DataFactory/dataFactories/*
Here's the documentation I've already read:
https://learn.microsoft.com/en-us/azure/role-based-access-control/role-assignments-portal
https://learn.microsoft.com/en-us/azure/role-based-access-control/role-definitions#assignablescopes
https://learn.microsoft.com/en-us/azure/role-based-access-control/role-assignments-template
The Role Assignment is used to assign the user a predefined or custom role "role definition".
The role definition is the one that defines the scope of the role. The scope of the role needs to be subscription(s), resource group(s) and resource(s). You can't define a type of resource. Its more like one or multiple locations.
Structure of RBAC Scope:
https://learn.microsoft.com/en-us/azure/role-based-access-control/overview#scope
How to create a Custom Role:
https://learn.microsoft.com/en-us/azure/role-based-access-control/custom-roles
In your case, if you want to grant a user to be able to handle IAM on all DataFactory you will need to manually define each datafactory scope. Then use the actions in the Microsoft.Authorization provider: https://learn.microsoft.com/en-us/azure/role-based-access-control/resource-provider-operations#microsoftauthorization
When defining the Scope for the assignment you can either define it with the -Scope <String> parameter or a combination of the following parameters.
-ResourceGroupName <String>
-ResourceName <String>
-ResourceType <String>
The resource type "-ResourceType ". For e.g. Microsoft.Network/virtualNetworks. Should only be used in conjunction with ResourceGroupName, ResourceName and (optionally)ParentResource parameters to construct a hierarchical scope in the form of a relative URI that identifies a resource.
For more details see: https://learn.microsoft.com/en-us/powershell/module/az.resources/new-azroleassignment?view=azps-3.3.0
the existing answer is pretty much on point, however there are a few things you can do to make it work:
the most obvious limit all data factories to a single resource group (or a couple of them) and grant SP owner permissions to those resource groups (or create a custom RBAC role).
use a simple script to assign those permissions dynamically
If you cant put all of the data factories in the same resource group\set of resource groups. Script would look like this ("pseudo" code, not tested, but will give you the idea):
Get-AzSbuscription | Foreach-Object {
$_ | Select-AzSubscription
Get-AzDataFactory | Foreach-Object {
New-AzRoleAssignment -Scope $_.ResourceId`
-ObjectId ddddddd-dddd-dddd-dddd-dddddddddddd `
-RoleDefinitionId 18d7d88d-d35e-4fb5-a5c3-7773c20a72d9
}
}
and run it on a schedule

How to export LocalNetworkGateway info from multiple ResourceGroups but also across multiple subscriptions

I'm new to powershell and azure and need to export all the LocalNetworkGateway information from multiple Resource Groups but also from across multiple Subscriptions.
A contributor has kindly provided me with a script that can output the data from multiple Resource Groups within a single subscription but I need to find a way of doing this across all subscriptions without having to set the subscription context manually for each one and then running the script for each subscription.
I have used the
$azureSubs = Get-AzSubscription
as a way of extracting information across multiple subscriptions that does not require Resource Group Names but I am now stuck. The code below has successfully provided info across RG's but within a single subscription.
$resourceGroups = Get-AzResourceGroup
$resourceGroups.foreach{
Get-AzLocalNetworkGateway -ResourceGroupName $_.ResourceGroupName |
Export-Csv -Path "c:\Azure\LocalNetworkGateway.csv" -Append
}
you need to create another loop around subscription (similar to resource groups), the only trick is that you need to switch active subscription before doing requests to the subscription:
$azureSubs = Get-AzSubscription
$azureSubs.foreach{
Select-AzSubscription $_ # << change active subscription
$resourceGroups = Get-AzResourceGroup # << same resource group loop starts here
$resourceGroups.foreach{
Get-AzLocalNetworkGateway -ResourceGroupName $_.ResourceGroupName |
Export-Csv -Path "c:\Azure\LocalNetworkGateway.csv" -Append
}
}

Resources