1+ # Copyright (c) Microsoft Corporation. All rights reserved.
2+ # See LICENSE.txt in the project root for license information.
3+
4+ <#
5+ . Synopsis
6+ Get the Guid of the directory tenant
7+ . DESCRIPTION
8+ This function fetches the OpenID configuration metadata from the identity system and parses the Directory TenantID out of it.
9+ Azure Stack AD FS is configured to be a single tenanted identity system with a TenantID.
10+ . EXAMPLE
11+ Get-DirectoryTenantIdentifier -authority https://login.windows.net/microsoft.onmicrosoft.com
12+ . EXAMPLE
13+ Get-DirectoryTenantIdentifier -authority https://adfs.local.azurestack.external/adfs
14+ #>
15+ function Get-DirectoryTenantIdentifier
16+ {
17+ [CmdletBinding ()]
18+ Param
19+ (
20+ # Param1 help description
21+ [Parameter (Mandatory = $true ,
22+ Position = 0 )]
23+ $Authority
24+ )
25+
26+ return $ (Invoke-RestMethod $ (" {0}/.well-known/openid-configuration" -f $authority.TrimEnd (' /' ))).issuer.TrimEnd(' /' ).Split(' /' )[-1 ]
27+ }
28+
29+ <#
30+ . Synopsis
31+ This function is used to create a Service Principal on teh AD Graph
32+ . DESCRIPTION
33+ 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.
34+ . EXAMPLE
35+ $servicePrincipal = New-ADGraphServicePrincipal -DisplayName "mySPApp" -AdminCredential $(Get-Credential) -Verbose
36+ . EXAMPLE
37+ $servicePrincipal = New-ADGraphServicePrincipal -DisplayName "mySPApp" -AdminCredential $(Get-Credential) -DeleteAndCreateNew -Verbose
38+ #>
39+ function New-ADGraphServicePrincipal
40+ {
41+ [CmdletBinding ()]
42+ Param
43+ (
44+ # Display Name of the Service Principal
45+ [Parameter (Mandatory = $true ,
46+ Position = 0 )]
47+ [ValidatePattern (“ [a-zA-Z0-9-]{3,}” )]
48+ $DisplayName ,
49+
50+ # Adfs Machine name
51+ [Parameter (Mandatory = $false ,
52+ Position = 1 )]
53+ [string ]
54+ $AdfsMachineName = " mas-adfs01.azurestack.local" ,
55+
56+ # Domain Administrator Credential to create Service Principal
57+ [Parameter (Mandatory = $true ,
58+ Position = 2 )]
59+ [System.Management.Automation.PSCredential ]
60+ $AdminCredential ,
61+
62+ # Switch to delete existing Service Principal with Provided Display Name and recreate
63+ [Parameter (Mandatory = $false )]
64+ [switch ]
65+ $DeleteAndCreateNew
66+ )
67+ Write-Verbose " Creating a Certificate for the Service Principal.."
68+ $clientCertificate = New-SelfSignedCertificate - CertStoreLocation " cert:\CurrentUser\My" - Subject " CN=$DisplayName " - KeySpec KeyExchange
69+ $scriptBlock = {
70+ param ([string ] $DisplayName , [System.Security.Cryptography.X509Certificates.X509Certificate2 ] $ClientCertificate , [bool ] $DeleteAndCreateNew )
71+ $VerbosePreference = " Continue"
72+ $ErrorActionPreference = " stop"
73+
74+ Import-Module ' ActiveDirectory' - Verbose:$false 4> $null
75+
76+ # Application Group Name
77+ $applicationGroupName = $DisplayName + " -AppGroup"
78+ $applicationGroupDescription = " Application group for $DisplayName "
79+ $shellSiteDisplayName = $DisplayName
80+ $shellSiteRedirectUri = " https://localhost/" .ToLowerInvariant()
81+ $shellSiteApplicationId = [guid ]::NewGuid().ToString()
82+ $shellSiteClientDescription = " Client for $DisplayName "
83+ $defaultTimeOut = New-TimeSpan - Minutes 5
84+
85+ if ($DeleteAndCreateNew )
86+ {
87+ $applicationGroup = Get-GraphApplicationGroup - ApplicationGroupName $applicationGroupName - Timeout $defaultTimeOut
88+ Write-Verbose $applicationGroup
89+ if ($applicationGroup )
90+ {
91+ Write-Warning - Message " Deleting existing application group with name '$applicationGroupName '."
92+ Remove-GraphApplicationGroup - TargetApplicationGroup $applicationGroup - Timeout $defaultTimeOut
93+ }
94+ }
95+
96+ Write-Verbose - Message " Creating new application group with name '$applicationGroupName '."
97+ $applicationParameters = @ {
98+ Name = $applicationGroupName
99+ Description = $applicationGroupDescription
100+ ClientType = ' Confidential'
101+ ClientId = $shellSiteApplicationId
102+ ClientDisplayName = $shellSiteDisplayName
103+ ClientRedirectUris = $shellSiteRedirectUri
104+ ClientDescription = $shellSiteClientDescription
105+ ClientCertificates = $ClientCertificate
106+ }
107+ $defaultTimeOut = New-TimeSpan - Minutes 10
108+ $applicationGroup = New-GraphApplicationGroup @applicationParameters - PassThru - Timeout $defaultTimeOut
109+
110+ Write-Verbose - Message " Shell Site ApplicationGroup: $ ( $applicationGroup | ConvertTo-Json ) "
111+ return [pscustomobject ]@ {
112+ ObjectId = $applicationGroup.Identifier
113+ ApplicationId = $applicationParameters.ClientId
114+ Thumbprint = $ClientCertificate.Thumbprint
115+ }
116+ }
117+ $domainAdminSession = New-PSSession - ComputerName $AdfsMachineName - Credential $AdminCredential - Authentication Credssp - Verbose
118+ $output = Invoke-Command - Session $domainAdminSession - ScriptBlock $scriptBlock - ArgumentList @ ($DisplayName , $ClientCertificate , $DeleteAndCreateNew.IsPresent ) - Verbose - ErrorAction Stop
119+ Write-Verbose " AppDetails: $ ( ConvertTo-Json $output - Depth 2 ) "
120+ return $output
121+ }
0 commit comments