As you said, updating a user with a new permission scope with the Set-AzStorageLocalUser
function overwrites the existing permission scope every time.
So after a workaround on this, I found a way by generating a SAS token for the storage account using New-AzStorageAccountSASToken
cmdlet. Then the token helps to grant access to all containers in the storage account.
Below is the script which I tried to generate a SAS token and then used that SAS token to grant access to the other storage accounts.
$accountName = "<storageaccount>"
$accountKey = "xxxxxxzxrQ=="
$storageContext = New-AzStorageContext -StorageAccountName $accountName -StorageAccountKey $accountKey
$permission = "rxxxxlup"
$expiryTime = (Get-Date).AddDays(1)
$sasToken = New-AzStorageAccountSASToken -Context $storageContext -Service Blob, File, Queue, Table -ResourceType Container -Permission $permission -StartTime (Get-Date).AddMinutes(-5) -ExpiryTime $expiryTime
$storageContext = New-AzStorageContext -StorageAccountName "<otherstorageaccount" -SasToken $sasToken
$storageContext
Output:



Alternatively:-
I tried using the NewAzStorageLocalUserPermissionScope
and
Set-AzStorageLocalUser
commands and it worked as follows:
Steps followed:
- Container's storage account was retrieved with the context.
- With the respective user, I created permission scopes for various containers.
- Those permission scopes were appended and saved in a single variable.
- The local user was then assigned the stored scopes.
Script:
$context = (Get-AzStorageAccount -Name <Storageaccount> -ResourceGroupName <resourcegroup>).Context
$Userinfo = Get-AzStorageLocalUser -Context $context -Username <User>
#permission scope for one container
$permissionScope = New-AzStorageLocalUserPermissionScope -Permission rw -Service blob -ResourceName <containername>
#New permission scope for the other container
$newScope = New-AzStorageLocalUserPermissionScope -Container <othercontainername> -Permission "rwdl"
$permissionscope.permissions += $newscope.permissions
$p1 = $permissionscope.permissions
$p2 = $newscope.permissions
$p1 += $p2
Set-AzStorageLocalUser -ResourceGroupName <Resourcegroup> -AccountName <Storageaccount> -Username <User> -PermissionScopes $p1
Output:


