-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathcreate_service_principal.ps1
32 lines (26 loc) · 1.54 KB
/
create_service_principal.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
Param(
[string] $sourceSubscriptionId,
[string] $destinationSubscriptionId,
[string] $resourceGroupName,
[string] $servicePrincipalName,
[string] $servicePrincipalPassword
)
# Create a new Azure AD application
$azureAdApplication = New-AzureRmADApplication `
-DisplayName "My Azure Image Copy Process" `
-HomePage $servicePrincipalName `
-IdentifierUris $servicePrincipalName `
-Password $servicePrincipalPassword
# Create a new service principal associated with the designated application
New-AzureRmADServicePrincipal -ApplicationId $azureAdApplication.ApplicationId
#wait wait . . . gives time for service principal creation to complete
Start-Sleep 15
# Assign Reader role to the newly created service principal. Both subscriptions below are associated with the same Azure AD tenant.
New-AzureRmRoleAssignment -RoleDefinitionName Reader `
-ServicePrincipalName $azureAdApplication.ApplicationId.Guid
New-AzureRmRoleAssignment -RoleDefinitionName Contributor `
-ServicePrincipalName $azureAdApplication.ApplicationId.Guid `
-Scope "/subscriptions/${sourceSubscriptionId}/resourceGroups/${resourceGroupName}"
New-AzureRmRoleAssignment -RoleDefinitionName Contributor `
-ServicePrincipalName $azureAdApplication.ApplicationId.Guid `
-Scope "/subscriptions/${destinationSubscriptionId}/resourceGroups/${resourceGroupName}"