forked from petehauge/DTL-VM-Generator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
New-ServicePrincipal.ps1
42 lines (32 loc) · 1.62 KB
/
New-ServicePrincipal.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
33
34
35
36
37
38
39
40
41
param
(
[Parameter(Mandatory=$true, HelpMessage="Subscription to create service principal into")]
[string] $SubscriptionId,
[Parameter(Mandatory=$true, HelpMessage="Name of app")]
[string] $ApplicationDisplayName
)
Import-Module Az.Resources
# Generate password lifted from this location: https://blogs.technet.microsoft.com/heyscriptingguy/2015/11/05/generate-random-letters-with-powershell/
Function Get-NewPassword() {
Param(
[int]$length=60
)
return (-join ((48..57) + (65..90) + (97..122) | Get-Random -Count $length | % {[char]$_}))
}
$sub = Get-AzSubscription -SubscriptionId $SubscriptionId
$ServicePrincipalPasswordPlainText = Get-NewPassword
$ServicePrincipalPassword = ConvertTo-SecureString -String $ServicePrincipalPasswordPlainText -AsPlainText -Force
# Create the service principal!
$ServicePrincipal = New-AzADServicePrincipal -DisplayName $ApplicationDisplayName -Password $ServicePrincipalPassword
Write-Host "--------------------------------------------------"
Write-Host "Service Principle Information"
Write-Host "Connection Name: $ApplicationDisplayName"
Write-Host "Subscription Id: $($sub.Id)"
Write-Host "Subscription Name: $($sub.Name)"
Write-Host "Service Principle Client Id: $($ServicePrincipal.ApplicationId)"
Write-Host "Service Principle Key: $ServicePrincipalPasswordPlainText"
Write-Host "Tenant Id: $($sub.TenantId)"
Write-Host "Object Id: $($ServicePrincipal.Id)"
Write-Host "--------------------------------------------------"
Start-Sleep -Seconds 30
New-AzRoleAssignment -ObjectId $ServicePrincipal.Id -Scope "/subscriptions/$($sub.Id)" -RoleDefinitionName "Contributor"