Skip to content

Commit

Permalink
updated and bugs fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
Kalyan Krishna committed Sep 2, 2022
1 parent 7992525 commit 4b88455
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,17 @@ Function Cleanup
Write-Host "Removing 'webApp' (WebApp-MultiTenant-v2) if needed"
try
{
Get-MgApplication -Filter "DisplayName eq 'WebApp-MultiTenant-v2'" | ForEach-Object {Remove-MgApplication -ApplicationId $_.Id }
Get-MgApplication -Filter "DisplayName eq 'WebApp-MultiTenant-v2'" | ForEach-Object {Remove-MgApplication -ApplicationId $_.Id }
}
catch
{
Write-Host "Unable to remove the application 'WebApp-MultiTenant-v2' . Try deleting manually." -ForegroundColor White -BackgroundColor Red
$message = $_
Write-Warning $Error[0]
Write-Host "Unable to remove the application 'WebApp-MultiTenant-v2'. Error is $message. Try deleting manually." -ForegroundColor White -BackgroundColor Red
}

Write-Host "Making sure there are no more (WebApp-MultiTenant-v2) applications found, will remove if needed..."
$apps = Get-MgApplication -Filter "DisplayName eq 'WebApp-MultiTenant-v2'"
$apps = Get-MgApplication -Filter "DisplayName eq 'WebApp-MultiTenant-v2'" | Format-List Id, DisplayName, AppId, SignInAudience, PublisherDomain

if ($apps)
{
Expand All @@ -55,18 +57,20 @@ Function Cleanup

foreach ($app in $apps)
{
Remove-MgApplication -ApplicationId $app.Id
Remove-MgApplication -ApplicationId $app.Id -Debug
Write-Host "Removed WebApp-MultiTenant-v2.."
}

# also remove service principals of this app
try
{
Get-MgServicePrincipal -filter "DisplayName eq 'WebApp-MultiTenant-v2'" | ForEach-Object {Remove-MgServicePrincipal -ApplicationId $_.Id -Confirm:$false}
Get-MgServicePrincipal -filter "DisplayName eq 'WebApp-MultiTenant-v2'" | ForEach-Object {Remove-MgServicePrincipal -ServicePrincipalId $_.Id -Confirm:$false}
}
catch
{
Write-Host "Unable to remove ServicePrincipal 'WebApp-MultiTenant-v2' . Try deleting manually from Enterprise applications." -ForegroundColor White -BackgroundColor Red
$message = $_
Write-Warning $Error[0]
Write-Host "Unable to remove ServicePrincipal 'WebApp-MultiTenant-v2'. Error is $message. Try deleting manually from Enterprise applications." -ForegroundColor White -BackgroundColor Red
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,40 @@ Function GetRequiredPermissions([string] $applicationDisplayName, [string] $requ
}


Function UpdateLine([string] $line, [string] $value)
{
$index = $line.IndexOf(':')
$lineEnd = ''

if($line[$line.Length - 1] -eq ','){ $lineEnd = ',' }

if ($index -ige 0)
{
$line = $line.Substring(0, $index+1) + " " + '"' + $value+ '"' + $lineEnd
}
return $line
}

Function UpdateTextFile([string] $configFilePath, [System.Collections.HashTable] $dictionary)
{
$lines = Get-Content $configFilePath
$index = 0
while($index -lt $lines.Length)
{
$line = $lines[$index]
foreach($key in $dictionary.Keys)
{
if ($line.Contains($key))
{
$lines[$index] = UpdateLine $line $dictionary[$key]
}
}
$index++
}

Set-Content -Path $configFilePath -Value $lines -Force
}

Function ConfigureApplications
{
$isOpenSSl = 'N' #temporary disable open certificate creation
Expand Down Expand Up @@ -165,11 +199,15 @@ Function ConfigureApplications
Write-Host "Granted permissions."

# Update config file for 'webApp'
$configFile = $pwd.Path + "\..\appsettings.json"
# $configFile = $pwd.Path + "\..\appsettings.json"
$configFile = $(Resolve-Path ($pwd.Path + "\..\appsettings.json"))

$dictionary = @{ "ClientId" = $webAppAadApplication.AppId;"TenantId" = 'organizations';"Domain" = $tenantName;"ClientSecret" = $webAppAppKey };

Write-Host "Updating the sample code ($configFile)"
Write-Host "Updating the sample code ($configFile) with the following config values"
$dictionary

UpdateTextFile -configFilePath $configFile -dictionary $dictionary
if($isOpenSSL -eq 'Y')
{
Write-Host -ForegroundColor Green "------------------------------------------------------------------------------------------------"
Expand All @@ -193,7 +231,16 @@ Add-Content -Value "<thead><tr><th>Application</th><th>AppId</th><th>Url in the
$ErrorActionPreference = "Stop"

# Run interactively (will ask you for the tenant ID)
ConfigureApplications -tenantId $tenantId -environment $azureEnvironmentName

try
{
ConfigureApplications -tenantId $tenantId -environment $azureEnvironmentName
}
catch
{
$message = $_
Write-Warning $Error[0]
Write-Host "Unable to register apps. Error is $message." -ForegroundColor White -BackgroundColor Red
}
Write-Host "Disconnecting from tenant"
Disconnect-MgGraph
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"CodeConfiguration": [
{
"App": "webApp",
"SettingKind": "JSon",
"SettingKind": "JSON",
"SettingFile": "\\..\\appsettings.json",
"Mappings": [
{
Expand Down

0 comments on commit 4b88455

Please sign in to comment.