Skip to content

Commit 70a5aa0

Browse files
authored
Fixed bugs in examples
1 parent 7b502ee commit 70a5aa0

File tree

1 file changed

+25
-27
lines changed

1 file changed

+25
-27
lines changed

articles/active-directory/users-groups-roles/roles-assign-powershell.md

Lines changed: 25 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -52,16 +52,17 @@ Now you can start using the cmdlets in the module. For a full description of the
5252
1. Create a new role using the following PowerShell script.
5353

5454
``` PowerShell
55+
## Assign a role to a user or service principal with resource scope
5556
# Get the user and role definition you want to link
56-
$user = Get-AzureADMSUser -Filter "userPrincipalName eq '[email protected]'"
57+
$user = Get-AzureADUser -Filter "userPrincipalName eq '[email protected]'"
5758
$roleDefinition = Get-AzureADMSRoleDefinition -Filter "displayName eq 'Application Support Administrator'"
5859
5960
# Get app registration and construct resource scope for assignment.
60-
"displayName eq 'f/128 Filter Photos'"
61-
$resourceScopes = '/' + $appRegistration.objectId
61+
$appRegistration = Get-AzureADApplication -Filter "displayName eq 'f/128 Filter Photos'"
62+
$resourceScope = '/' + $appRegistration.objectId
6263
6364
# Create a scoped role assignment
64-
$roleAssignment = New-AzureADMSRoleAssignment -ResourceScopes $resourceScopes -RoleDefinitionId $roleDefinition.objectId -PrincipalId $user.objectId
65+
$roleAssignment = New-AzureADMSRoleAssignment -ResourceScope $resourceScope -RoleDefinitionId $roleDefinition.Id -PrincipalId $user.objectId
6566
```
6667

6768
To assign the role to a service principal instead of a user, use the [Get-AzureADMSServicePrincipal cmdlet](https://docs.microsoft.com/powershell/module/azuread/get-azureadserviceprincipal?view=azureadps-2.0).
@@ -74,24 +75,20 @@ Role definition objects contain the definition of the built-in or custom role, a
7475

7576
``` PowerShell
7677
# Basic information
77-
78-
$description = "Application Registration Credential Administrator"
79-
$displayName = "Custom Demo Admin"
80-
$resourceScopes = @('/')
81-
$templateId = "355aed8a-864b-4e2b-b225-ea95482e7570"
78+
$description = "Can manage credentials of application registrations"
79+
$displayName = "Application Registration Credential Administrator"
80+
$templateId = (New-Guid).Guid
8281
8382
# Set of actions to grant
8483
$allowedResourceAction =
8584
@(
86-
"microsoft.directory/applications/default/read",
85+
"microsoft.directory/applications/standard/read",
8786
"microsoft.directory/applications/credentials/update"
8887
)
89-
$resourceActions = @{'allowedResourceActions'= $allowedResourceAction}
90-
$rolePermission = @{'resourceActions' = $resourceActions}
91-
$rolePermissions = $rolePermission
88+
$rolePermissions = @{'allowedResourceActions'= $allowedResourceAction}
9289
9390
# Create new custom admin role
94-
$customAdmin = New-AzureADMSRoleDefinitions -RolePermissions $rolePermissions -ResourceScopes $resourceScopes -DisplayName $displayName -Description $description -TemplateId $templateId -IsEnabled $true
91+
$customAdmin = New-AzureADMSRoleDefinition -RolePermissions $rolePermissions -DisplayName $displayName -Description $description -TemplateId $templateId -IsEnabled $true
9592
```
9693

9794
### Read Operations on RoleDefinition
@@ -101,10 +98,10 @@ $customAdmin = New-AzureADMSRoleDefinitions -RolePermissions $rolePermissions -R
10198
Get-AzureADMSRoleDefinitions
10299
103100
# Get single role definition by objectId
104-
$customAdmin = Get-AzureADMSRoleDefinitions -ObjectId '86593cfc-114b-4a15-9954-97c3494ef49b'
101+
Get-AzureADMSRoleDefinition -Id 86593cfc-114b-4a15-9954-97c3494ef49b
105102
106103
# Get single role definition by templateId
107-
$customAdmin = Get-AzureADMSRoleDefinitions -Filter "templateId eq '355aed8a-864b-4e2b-b225-ea95482e757not
104+
Get-AzureADMSRoleDefinition -Filter "templateId eq 'c4e39bd9-1100-46d3-8c65-fb160da0071f'"
108105
```
109106

110107
### Update Operations on RoleDefinition
@@ -113,14 +110,14 @@ $customAdmin = Get-AzureADMSRoleDefinitions -Filter "templateId eq '355aed8a-864
113110
# Update role definition
114111
# This works for any writable property on role definition. You can replace display name with other
115112
# valid properties.
116-
Set-AzureADMSRoleDefinitions -ObjectId $customAdmin.ObjectId -DisplayName "Updated DisplayName"
113+
Set-AzureADMSRoleDefinition -Id c4e39bd9-1100-46d3-8c65-fb160da0071f -DisplayName "Updated DisplayName"
117114
```
118115

119116
### Delete operations on RoleDefinition
120117

121118
``` PowerShell
122119
# Delete role definition
123-
Remove-AzureADMSRoleDefinitions -ObjectId $customAdmin.ObjectId
120+
Remove-AzureADMSRoleDefinitions -Id c4e39bd9-1100-46d3-8c65-fb160da0071f
124121
```
125122

126123
## Operations on RoleAssignment
@@ -130,32 +127,33 @@ Role assignments contain information linking a given security principal (a user
130127
### Create Operations on RoleAssignment
131128

132129
``` PowerShell
133-
# Scopes to scope granted permissions to
134-
$resourceScopes = @('/')
130+
# Get the user and role definition you want to link
131+
$user = Get-AzureADUser -Filter "userPrincipalName eq '[email protected]'"
132+
$roleDefinition = Get-AzureADMSRoleDefinition -Filter "displayName eq 'Application Support Administrator'"
135133
136-
# IDs of principal and role definition you want to link
137-
$principalId = "27c8ca78-ab1c-40ae-bd1b-eaeebd6f68ac"
138-
$roleDefinitionId = $customKeyCredAdmin.ObjectId
134+
# Get app registration and construct resource scope for assignment.
135+
$appRegistration = Get-AzureADApplication -Filter "displayName eq 'f/128 Filter Photos'"
136+
$resourceScope = '/' + $appRegistration.objectId
139137
140138
# Create a scoped role assignment
141-
$roleAssignment = New-AzureADMSRoleAssignments -ResourceScopes $resourceScopes -RoleDefinitionId -PrincipalId $principalId
139+
$roleAssignment = New-AzureADMSRoleAssignment -ResourceScope $resourceScope -RoleDefinitionId $roleDefinition.Id -PrincipalId $user.objectId
142140
```
143141

144142
### Read Operations on RoleAssignment
145143

146144
``` PowerShell
147145
# Get role assignments for a given principal
148-
Get-AzureADMSRoleAssignments -Filter "principalId eq '27c8ca78-ab1c-40ae-bd1b-eaeebd6f68ac'"
146+
Get-AzureADMSRoleAssignment -Filter "principalId eq '27c8ca78-ab1c-40ae-bd1b-eaeebd6f68ac'"
149147
150148
# Get role assignments for a given role definition
151-
Get-AzureADMSRoleAssignments -Filter "principalId eq '355aed8a-864b-4e2b-b225-ea95482e7570'"
149+
Get-AzureADMSRoleAssignment -Filter "roleDefinitionId eq '355aed8a-864b-4e2b-b225-ea95482e7570'"
152150
```
153151

154152
### Delete Operations on RoleAssignment
155153

156154
``` PowerShell
157155
# Delete role assignment
158-
Remove-AzureADMSRoleAssignments -ObjectId $roleAssignment.ObjectId
156+
Remove-AzureADMSRoleAssignment -Id 'qiho4WOb9UKKgng_LbPV7tvKaKRCD61PkJeKMh7Y458-1'
159157
```
160158

161159
## Next steps

0 commit comments

Comments
 (0)