1+ # Copyright (c) Microsoft Corporation. All rights reserved.
2+ # See LICENSE.txt in the project root for license information.
3+
4+ <#
5+ . Synopsis
6+ This function is used to create a Service Principal on teh AD Graph
7+ . DESCRIPTION
8+ The command creates a certificate in the cert store of the local user and uses that certificate to create a Service Principal in the Azure Stack Stamp Active Directory.
9+ . EXAMPLE
10+ $servicePrincipal = New-ADGraphServicePrincipal -DisplayName "mySPApp" -AdminCredential $(Get-Credential) -Verbose
11+ . EXAMPLE
12+ $servicePrincipal = New-ADGraphServicePrincipal -DisplayName "mySPApp" -AdminCredential $(Get-Credential) -DeleteAndCreateNew -Verbose
13+ #>
14+ function New-ADGraphServicePrincipal
15+ {
16+ [CmdletBinding ()]
17+ Param
18+ (
19+ # Display Name of the Service Principal
20+ [Parameter (Mandatory = $true ,
21+ Position = 0 )]
22+ [ValidatePattern (“ [a-zA-Z0-9-]{3,}” )]
23+ $DisplayName ,
24+
25+ # Adfs Machine name
26+ [Parameter (Mandatory = $false ,
27+ Position = 1 )]
28+ [string ]
29+ $AdfsMachineName = " mas-adfs01.azurestack.local" ,
30+
31+ # Domain Administrator Credential to create Service Principal
32+ [Parameter (Mandatory = $true ,
33+ Position = 2 )]
34+ [System.Management.Automation.PSCredential ]
35+ $AdminCredential ,
36+
37+ # Switch to delete existing Service Principal with Provided Display Name and recreate
38+ [Parameter (Mandatory = $false )]
39+ [switch ]
40+ $DeleteAndCreateNew
41+ )
42+ Write-Verbose " Creating a Certificate for the Service Principal.."
43+ $clientCertificate = New-SelfSignedCertificate - CertStoreLocation " cert:\CurrentUser\My" - Subject " CN=$DisplayName " - KeySpec KeyExchange
44+ $scriptBlock = {
45+ param ([string ] $DisplayName , [System.Security.Cryptography.X509Certificates.X509Certificate2 ] $ClientCertificate , [bool ] $DeleteAndCreateNew )
46+ $VerbosePreference = " Continue"
47+ $ErrorActionPreference = " stop"
48+
49+ Import-Module ' ActiveDirectory' - Verbose:$false 4> $null
50+
51+ # Application Group Name
52+ $applicationGroupName = $DisplayName + " -AppGroup"
53+ $applicationGroupDescription = " Application group for $DisplayName "
54+ $shellSiteDisplayName = $DisplayName
55+ $shellSiteRedirectUri = " https://localhost/" .ToLowerInvariant()
56+ $shellSiteApplicationId = [guid ]::NewGuid().ToString()
57+ $shellSiteClientDescription = " Client for $DisplayName "
58+ $defaultTimeOut = New-TimeSpan - Minutes 5
59+
60+ if ($DeleteAndCreateNew )
61+ {
62+ $applicationGroup = Get-GraphApplicationGroup - ApplicationGroupName $applicationGroupName - Timeout $defaultTimeOut
63+ Write-Verbose $applicationGroup
64+ if ($applicationGroup )
65+ {
66+ Write-Warning - Message " Deleting existing application group with name '$applicationGroupName '."
67+ Remove-GraphApplicationGroup - TargetApplicationGroup $applicationGroup - Timeout $defaultTimeOut
68+ }
69+ }
70+
71+ Write-Verbose - Message " Creating new application group with name '$applicationGroupName '."
72+ $applicationParameters = @ {
73+ Name = $applicationGroupName
74+ Description = $applicationGroupDescription
75+ ClientType = ' Confidential'
76+ ClientId = $shellSiteApplicationId
77+ ClientDisplayName = $shellSiteDisplayName
78+ ClientRedirectUris = $shellSiteRedirectUri
79+ ClientDescription = $shellSiteClientDescription
80+ ClientCertificates = $ClientCertificate
81+ }
82+ $defaultTimeOut = New-TimeSpan - Minutes 10
83+ $applicationGroup = New-GraphApplicationGroup @applicationParameters - PassThru - Timeout $defaultTimeOut
84+
85+ Write-Verbose - Message " Shell Site ApplicationGroup: $ ( $applicationGroup | ConvertTo-Json ) "
86+ return [pscustomobject ]@ {
87+ ObjectId = $applicationGroup.Identifier
88+ ApplicationId = $applicationParameters.ClientId
89+ Thumbprint = $ClientCertificate.Thumbprint
90+ }
91+ }
92+ $domainAdminSession = New-PSSession - ComputerName $AdfsMachineName - Credential $AdminCredential - Authentication Credssp - Verbose
93+ $output = Invoke-Command - Session $domainAdminSession - ScriptBlock $scriptBlock - ArgumentList @ ($DisplayName , $ClientCertificate , $DeleteAndCreateNew.IsPresent ) - Verbose - ErrorAction Stop
94+ Write-Verbose " AppDetails: $ ( ConvertTo-Json $output - Depth 2 ) "
95+ return $output
96+ }
0 commit comments