Skip to content

Commit 3263c79

Browse files
authored
Update AzureStack.Identity.psm1
Updated Create Service Principal method to go against the PEP endpoint for an AD FS scenario
1 parent cca33bd commit 3263c79

File tree

1 file changed

+17
-66
lines changed

1 file changed

+17
-66
lines changed

Identity/AzureStack.Identity.psm1

Lines changed: 17 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,12 @@ function Get-AzsDirectoryTenantidentifier {
2828

2929
<#
3030
.Synopsis
31-
This function is used to create a Service Principal on teh AD Graph
31+
This function is used to create a Service Principal on the AD Graph in anAD FS topology
3232
.DESCRIPTION
3333
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.
3434
.EXAMPLE
35-
$servicePrincipal = New-AzsAdGraphServicePrincipal -DisplayName "mySPApp" -AdminCredential $(Get-Credential) -Verbose
36-
.EXAMPLE
37-
$servicePrincipal = New-AzsAdGraphServicePrincipal -DisplayName "mySPApp" -AdminCredential $(Get-Credential) -DeleteAndCreateNew -Verbose
38-
#>
35+
$servicePrincipal = New-AzsAdGraphServicePrincipal -DisplayName "myapp12" -AdminCredential $(Get-Credential) -Verbose
36+
#>
3937

4038
function New-AzsAdGraphServicePrincipal {
4139
[CmdletBinding()]
@@ -47,75 +45,28 @@ function New-AzsAdGraphServicePrincipal {
4745
Position = 0)]
4846
$DisplayName,
4947

50-
# Adfs Machine name
51-
[Parameter(Mandatory = $true, Position = 1)]
48+
# PEP Machine name
5249
[string]
53-
$AdfsMachineName,
50+
$ERCSMachineName = "Azs-ERCS01",
5451

5552
# Domain Administrator Credential to create Service Principal
5653
[Parameter(Mandatory = $true,
5754
Position = 2)]
5855
[System.Management.Automation.PSCredential]
59-
$AdminCredential,
60-
61-
# Switch to delete existing Service Principal with Provided Display Name and recreate
62-
[Parameter(Mandatory = $false)]
63-
[switch]
64-
$DeleteAndCreateNew
56+
$AdminCredential
6557
)
6658

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-
$applicationGroup = Get-GraphApplicationGroup -ApplicationGroupName $applicationGroupName -Timeout $defaultTimeOut
87-
Write-Verbose $applicationGroup
88-
if ($applicationGroup) {
89-
Write-Warning -Message "Deleting existing application group with name '$applicationGroupName'."
90-
Remove-GraphApplicationGroup -TargetApplicationGroup $applicationGroup -Timeout $defaultTimeOut
91-
}
92-
}
93-
94-
Write-Verbose -Message "Creating new application group with name '$applicationGroupName'."
95-
$applicationParameters = @{
96-
Name = $applicationGroupName
97-
Description = $applicationGroupDescription
98-
ClientType = 'Confidential'
99-
ClientId = $shellSiteApplicationId
100-
ClientDisplayName = $shellSiteDisplayName
101-
ClientRedirectUris = $shellSiteRedirectUri
102-
ClientDescription = $shellSiteClientDescription
103-
ClientCertificates = $ClientCertificate
104-
}
105-
$defaultTimeOut = New-TimeSpan -Minutes 10
106-
$applicationGroup = New-GraphApplicationGroup @applicationParameters -PassThru -Timeout $defaultTimeOut
107-
108-
Write-Verbose -Message "Shell Site ApplicationGroup: $($applicationGroup | ConvertTo-Json)"
109-
return [pscustomobject]@{
110-
ObjectId = $applicationGroup.Identifier
111-
ApplicationId = $applicationParameters.ClientId
112-
Thumbprint = $ClientCertificate.Thumbprint
113-
}
114-
}
115-
$domainAdminSession = New-PSSession -ComputerName $AdfsMachineName -Credential $AdminCredential -Authentication Credssp -Verbose
116-
$output = Invoke-Command -Session $domainAdminSession -ScriptBlock $scriptBlock -ArgumentList @($DisplayName, $ClientCertificate, $DeleteAndCreateNew.IsPresent) -Verbose -ErrorAction Stop
117-
Write-Verbose "AppDetails: $(ConvertTo-Json $output -Depth 2)"
118-
return $output
59+
$ApplicationGroupName = $DisplayName
60+
$computerName = $ERCSMachineName
61+
$cloudAdminCredential = $AdminCredential
62+
$domainAdminSession = New-PSSession -ComputerName $computerName -Credential $cloudAdminCredential -configurationname privilegedendpoint -Verbose
63+
$GraphClientCertificate = New-SelfSignedCertificate -CertStoreLocation "cert:\CurrentUser\My" -Subject "CN=$ApplicationGroupName" -KeySpec KeyExchange
64+
$graphRedirectUri = "https://localhost/".ToLowerInvariant()
65+
$ApplicationName = $ApplicationGroupName
66+
$application = Invoke-Command -Session $domainAdminSession -Verbose -ErrorAction Stop `
67+
-ScriptBlock { New-GraphApplication -Name $using:ApplicationName -ClientRedirectUris $using:graphRedirectUri -ClientCertificates $using:GraphClientCertificate }
68+
69+
return $application
11970
}
12071

12172
# Exposed Functions

0 commit comments

Comments
 (0)