Skip to content

Commit dfdf5d8

Browse files
authored
Fixing the app creation scripts (#6)
after the renaming in the parameters.json
1 parent 24aa691 commit dfdf5d8

File tree

6 files changed

+53
-31
lines changed

6 files changed

+53
-31
lines changed

1-Call-MsGraph-WithSecret/AppCreationScripts/Cleanup.ps1

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ param(
55
[string] $tenantId
66
)
77

8-
if ((Get-Module -ListAvailable -Name "AzureAD") -eq $null) {
8+
if ($null -eq (Get-Module -ListAvailable -Name "AzureAD")) {
99
Install-Module "AzureAD" -Scope CurrentUser
1010
}
1111
Import-Module AzureAD
12-
$ErrorActionPreference = 'Stop'
12+
$ErrorActionPreference = "Stop"
1313

1414
Function Cleanup
1515
{
@@ -44,20 +44,27 @@ This function removes the Azure AD applications for the sample. These applicatio
4444
$tenantId = $creds.Tenant.Id
4545
}
4646
$tenant = Get-AzureADTenantDetail
47-
$tenantName = ($tenant.VerifiedDomains | Where { $_._Default -eq $True }).Name
47+
$tenantName = ($tenant.VerifiedDomains | Where-Object { $_._Default -eq $True }).Name
4848

4949
# Removes the applications
5050
Write-Host "Cleaning-up applications from tenant '$tenantName'"
5151

5252
Write-Host "Removing 'client' (python-daemon-console) if needed"
53-
$app=Get-AzureADApplication -Filter "DisplayName eq 'python-daemon-console'"
53+
Get-AzureADApplication -Filter "DisplayName eq 'python-daemon-console'" | ForEach-Object {Remove-AzureADApplication -ObjectId $_.ObjectId }
54+
$apps = Get-AzureADApplication -Filter "DisplayName eq 'python-daemon-console'"
55+
if ($apps)
56+
{
57+
Remove-AzureADApplication -ObjectId $apps.ObjectId
58+
}
5459

55-
if ($app)
60+
foreach ($app in $apps)
5661
{
5762
Remove-AzureADApplication -ObjectId $app.ObjectId
58-
Write-Host "Removed."
63+
Write-Host "Removed python-daemon-console.."
5964
}
60-
65+
# also remove service principals of this app
66+
Get-AzureADServicePrincipal -filter "DisplayName eq 'python-daemon-console'" | ForEach-Object {Remove-AzureADServicePrincipal -ObjectId $_.Id -Confirm:$false}
67+
6168
}
6269

63-
Cleanup -Credential $Credential -tenantId $TenantId
70+
Cleanup -Credential $Credential -tenantId $TenantId

1-Call-MsGraph-WithSecret/AppCreationScripts/Configure.ps1

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ Function AddResourcePermission($requiredAccess, `
6565
}
6666

6767
#
68-
# Exemple: GetRequiredPermissions "Microsoft Graph" "Graph.Read|User.Read"
68+
# Example: GetRequiredPermissions "Microsoft Graph" "Graph.Read|User.Read"
6969
# See also: http://stackoverflow.com/questions/42164581/how-to-configure-a-new-azure-ad-application-through-powershell
7070
Function GetRequiredPermissions([string] $applicationDisplayName, [string] $requiredDelegatedPermissions, [string]$requiredApplicationPermissions, $servicePrincipal)
7171
{
@@ -165,18 +165,18 @@ Function ReplaceInTextFile([string] $configFilePath, [System.Collections.HashTab
165165
Set-Content -Path $configFilePath -Value $lines -Force
166166
}
167167

168-
169168
Set-Content -Value "<html><body><table>" -Path createdApps.html
170169
Add-Content -Value "<thead><tr><th>Application</th><th>AppId</th><th>Url in the Azure portal</th></tr></thead><tbody>" -Path createdApps.html
171170

171+
$ErrorActionPreference = "Stop"
172+
172173
Function ConfigureApplications
173174
{
174175
<#.Description
175176
This function creates the Azure AD applications for the sample in the provided Azure AD tenant and updates the
176177
configuration files in the client and service project of the visual studio solution (App.Config and Web.Config)
177178
so that they are consistent with the Applications parameters
178179
#>
179-
180180
$commonendpoint = "common"
181181

182182
# $tenantId is the Active Directory Tenant. This is a GUID which represents the "Directory ID" of the AzureAD tenant
@@ -208,7 +208,7 @@ Function ConfigureApplications
208208
$tenant = Get-AzureADTenantDetail
209209
$tenantName = ($tenant.VerifiedDomains | Where { $_._Default -eq $True }).Name
210210

211-
# Get the user running the script
211+
# Get the user running the script to add the user as the app owner
212212
$user = Get-AzureADUser -ObjectId $creds.Account.Id
213213

214214
# Create the client AAD application
@@ -218,12 +218,14 @@ Function ConfigureApplications
218218
$fromDate = [DateTime]::Now;
219219
$key = CreateAppKey -fromDate $fromDate -durationInYears 2 -pw $pw
220220
$clientAppKey = $pw
221+
# create the application
221222
$clientAadApplication = New-AzureADApplication -DisplayName "python-daemon-console" `
222223
-ReplyUrls "https://daemon" `
223224
-IdentifierUris "https://$tenantName/python-daemon-console" `
224225
-PasswordCredentials $key `
225226
-PublicClient $False
226227

228+
# create the service principal of the newly created application
227229
$currentAppId = $clientAadApplication.AppId
228230
$clientServicePrincipal = New-AzureADServicePrincipal -AppId $currentAppId -Tags {WindowsAzureActiveDirectoryIntegratedApp}
229231

@@ -265,7 +267,7 @@ Function ConfigureApplications
265267
# Update config file for 'client'
266268
$configFile = $pwd.Path + "\..\parameters.json"
267269
Write-Host "Updating the sample code ($configFile)"
268-
$dictionary = @{ "organizations" = $tenantName };
270+
$dictionary = @{ "Enter_the_Tenant_Name_Here" = $tenantName };
269271
ReplaceInTextFile -configFilePath $configFile -dictionary $dictionary
270272
Write-Host ""
271273
Write-Host -ForegroundColor Green "------------------------------------------------------------------------------------------------"
@@ -282,7 +284,8 @@ Function ConfigureApplications
282284
# Pre-requisites
283285
if ((Get-Module -ListAvailable -Name "AzureAD") -eq $null) {
284286
Install-Module "AzureAD" -Scope CurrentUser
285-
}
287+
}
288+
286289
Import-Module AzureAD
287290

288291
# Run interactively (will ask you for the tenant ID)

1-Call-MsGraph-WithSecret/AppCreationScripts/sample.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
"SettingFile": "\\..\\parameters.json",
6363
"Mappings": [
6464
{
65-
"key": "organizations",
65+
"key": "Enter_the_Tenant_Name_Here",
6666
"value": "$tenantName"
6767
}
6868
]

2-Call-MsGraph-WithCertificate/AppCreationScripts/Cleanup.ps1

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ param(
55
[string] $tenantId
66
)
77

8-
if ((Get-Module -ListAvailable -Name "AzureAD") -eq $null) {
8+
if ($null -eq (Get-Module -ListAvailable -Name "AzureAD")) {
99
Install-Module "AzureAD" -Scope CurrentUser
1010
}
1111
Import-Module AzureAD
12-
$ErrorActionPreference = 'Stop'
12+
$ErrorActionPreference = "Stop"
1313

1414
Function Cleanup
1515
{
@@ -44,20 +44,29 @@ This function removes the Azure AD applications for the sample. These applicatio
4444
$tenantId = $creds.Tenant.Id
4545
}
4646
$tenant = Get-AzureADTenantDetail
47-
$tenantName = ($tenant.VerifiedDomains | Where { $_._Default -eq $True }).Name
47+
$tenantName = ($tenant.VerifiedDomains | Where-Object { $_._Default -eq $True }).Name
4848

4949
# Removes the applications
5050
Write-Host "Cleaning-up applications from tenant '$tenantName'"
5151

5252
Write-Host "Removing 'client' (python-daemon-console) if needed"
53-
$app=Get-AzureADApplication -Filter "DisplayName eq 'python-daemon-console'"
53+
Get-AzureADApplication -Filter "DisplayName eq 'python-daemon-console'" | ForEach-Object {Remove-AzureADApplication -ObjectId $_.ObjectId }
54+
$apps = Get-AzureADApplication -Filter "DisplayName eq 'python-daemon-console'"
55+
if ($apps)
56+
{
57+
Remove-AzureADApplication -ObjectId $apps.ObjectId
58+
}
5459

55-
if ($app)
60+
foreach ($app in $apps)
5661
{
5762
Remove-AzureADApplication -ObjectId $app.ObjectId
58-
Write-Host "Removed."
63+
Write-Host "Removed python-daemon-console.."
5964
}
60-
65+
# also remove service principals of this app
66+
Get-AzureADServicePrincipal -filter "DisplayName eq 'python-daemon-console'" | ForEach-Object {Remove-AzureADServicePrincipal -ObjectId $_.Id -Confirm:$false}
67+
68+
# remove self-signed certificate
69+
Get-ChildItem -Path Cert:\CurrentUser\My | where { $_.subject -eq "CN=DaemonConsoleCert" } | Remove-Item
6170
}
6271

63-
Cleanup -Credential $Credential -tenantId $TenantId
72+
Cleanup -Credential $Credential -tenantId $TenantId

2-Call-MsGraph-WithCertificate/AppCreationScripts/Configure.ps1

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ Function AddResourcePermission($requiredAccess, `
3939
}
4040

4141
#
42-
# Exemple: GetRequiredPermissions "Microsoft Graph" "Graph.Read|User.Read"
42+
# Example: GetRequiredPermissions "Microsoft Graph" "Graph.Read|User.Read"
4343
# See also: http://stackoverflow.com/questions/42164581/how-to-configure-a-new-azure-ad-application-through-powershell
4444
Function GetRequiredPermissions([string] $applicationDisplayName, [string] $requiredDelegatedPermissions, [string]$requiredApplicationPermissions, $servicePrincipal)
4545
{
@@ -139,18 +139,18 @@ Function ReplaceInTextFile([string] $configFilePath, [System.Collections.HashTab
139139
Set-Content -Path $configFilePath -Value $lines -Force
140140
}
141141

142-
143142
Set-Content -Value "<html><body><table>" -Path createdApps.html
144143
Add-Content -Value "<thead><tr><th>Application</th><th>AppId</th><th>Url in the Azure portal</th></tr></thead><tbody>" -Path createdApps.html
145144

145+
$ErrorActionPreference = "Stop"
146+
146147
Function ConfigureApplications
147148
{
148149
<#.Description
149150
This function creates the Azure AD applications for the sample in the provided Azure AD tenant and updates the
150151
configuration files in the client and service project of the visual studio solution (App.Config and Web.Config)
151152
so that they are consistent with the Applications parameters
152153
#>
153-
154154
$commonendpoint = "common"
155155

156156
# $tenantId is the Active Directory Tenant. This is a GUID which represents the "Directory ID" of the AzureAD tenant
@@ -182,11 +182,12 @@ Function ConfigureApplications
182182
$tenant = Get-AzureADTenantDetail
183183
$tenantName = ($tenant.VerifiedDomains | Where { $_._Default -eq $True }).Name
184184

185-
# Get the user running the script
185+
# Get the user running the script to add the user as the app owner
186186
$user = Get-AzureADUser -ObjectId $creds.Account.Id
187187

188188
# Create the client AAD application
189189
Write-Host "Creating the AAD application (python-daemon-console)"
190+
# create the application
190191
$clientAadApplication = New-AzureADApplication -DisplayName "python-daemon-console" `
191192
-ReplyUrls "https://daemon" `
192193
-IdentifierUris "https://$tenantName/python-daemon-console" `
@@ -211,6 +212,7 @@ Function ConfigureApplications
211212
-StartDate $certificate.NotBefore `
212213
-EndDate $certificate.NotAfter
213214

215+
# create the service principal of the newly created application
214216
$currentAppId = $clientAadApplication.AppId
215217
$clientServicePrincipal = New-AzureADServicePrincipal -AppId $currentAppId -Tags {WindowsAzureActiveDirectoryIntegratedApp}
216218

@@ -246,13 +248,13 @@ Function ConfigureApplications
246248
# Update config file for 'client'
247249
$configFile = $pwd.Path + "\..\parameters.json"
248250
Write-Host "Updating the sample code ($configFile)"
249-
$dictionary = @{ "client_id" = $clientAadApplication.AppId;"thumbprint" = $thumbprint };
251+
$dictionary = @{ "client_id" = $clientAadApplication.AppId;"thumbprint" = $certBase64Thumbprint };
250252
UpdateTextFile -configFilePath $configFile -dictionary $dictionary
251253

252254
# Update config file for 'client'
253255
$configFile = $pwd.Path + "\..\parameters.json"
254256
Write-Host "Updating the sample code ($configFile)"
255-
$dictionary = @{ "organizations" = $tenantName };
257+
$dictionary = @{ "Enter_the_Tenant_Name_Here" = $tenantName };
256258
ReplaceInTextFile -configFilePath $configFile -dictionary $dictionary
257259
Write-Host ""
258260
Write-Host -ForegroundColor Green "------------------------------------------------------------------------------------------------"
@@ -269,7 +271,8 @@ Function ConfigureApplications
269271
# Pre-requisites
270272
if ((Get-Module -ListAvailable -Name "AzureAD") -eq $null) {
271273
Install-Module "AzureAD" -Scope CurrentUser
272-
}
274+
}
275+
273276
Import-Module AzureAD
274277

275278
# Run interactively (will ask you for the tenant ID)

2-Call-MsGraph-WithCertificate/AppCreationScripts/sample.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@
6262
"SettingFile": "\\..\\parameters.json",
6363
"Mappings": [
6464
{
65-
"key": "organizations",
65+
"key": "Enter_the_Tenant_Name_Here",
6666
"value": "$tenantName"
6767
}
6868
]

0 commit comments

Comments
 (0)