<commit_before># Purpose : 
# 1) This script is used to create additional AD users to run various scenarios and creates AD Application and creates service principle to AD Application
# 2) This script should run by Global AD Administrator, You created Global AD Admin in previous script(CreateGlobalADAdmin.ps1)
# 3) This script should run before start deployment of ARM Templates
Param(
	[string] [Parameter(Mandatory=$true)] $azureADDomainName, # Provide your Azure AD Domain Name
	[string] [Parameter(Mandatory=$true)] $subscriptionID, # Provide your Azure subscription ID
	[string] [Parameter(Mandatory=$true)] $suffix, #This is used to create a unique website name in your organization. This could be your company name or business unit name
	# [string] [Parameter(Mandatory=$true)] , # Provide an SQL AD Admin Password for the user sql<PASSWORD>@$azureADDomainName that complies to your AD's password policy. 
	[string] [Parameter(Mandatory=$true)] $azureADApplicationClientSecret, #Provide a Azure Application Password for setup of the app client access.
	[string] $customHostName = "pcipaas.com", # Provide CustomHostName which will be used for creating ASE subdomain.
	[bool]   $enableSSL = $false, # Provide boolean input to enable or disable SSL on application gateway 
	[string] $certificatePath # Provide Certificate path if you want to provide your own Application gateway certificate.
)

$ErrorActionPreference = 'Stop'

function Generate-Password ()
{
    (-join ((65..90) + (97..122) | Get-Random -Count 15 | % {[char]$_})) + (Get-Random -Maximum 9999)
}

function Convert-Certificate ($certPath)
{
$fileContentBytes = get-content "$certPath" -Encoding Byte
[System.Convert]::ToBase64String($fileContentBytes)
}

$ScriptFolder = Split-Path -Parent $PSCommandPath

###
#Imp: This script needs to be run by Global AD Administrator (aka Company Administrator)
###
Write-Host ("Pre-Requisite: This script needs to be run by Global AD Administrator (aka Company Administrator)" ) -ForegroundColor Gray
#Connect to the Azure AD
Connect-MsolService
$SQLADAdminName = "sqladmin@"+$azureADDomainName
$receptionistUserName = "receptionist_EdnaB@"+$azureADDomainName

#Generate bunch of strong passwords
$sqlADAdminPassword = Generate-Password
$receptionistPassword = Generate-Password
$bastionPassword = Generate-Password

$cloudwiseAppServiceURL = "http://localcloudneeti6i"+$azureADDomainName
Write-Host ("Step 1:Create AD Users for SQL AD Admin, Receptinist and Doctor to test various scenarios" ) -ForegroundColor Gray
$sqlADAdminObjectId = (Get-MsolUser -UserPrincipalName $SQLADAdminName -ErrorAction SilentlyContinue -ErrorVariable errorVariable).ObjectID
$sqlADAdminDetails = ""
if ($sqlADAdminObjectId -eq $null)  
{    
    $sqlADAdminDetails = New-MsolUser -UserPrincipalName $SQLADAdminName -DisplayName "SQLADAdministrator PCI Samples" -FirstName "SQL AD Administrator" -LastName "PCI Samples"
	$sqlADAdminObjectId= $sqlADAdminDetails.ObjectID
    # Make the new user a Global AD Administrator
	Add-MsolRoleMember -RoleName "Company Administrator" -RoleMemberObjectId $sqlADAdminObjectId
}
Set-MsolUserPassword -userPrincipalName $SQLADAdminName -NewPassword $sqlADAdminPassword -ForceChangePassword $false

$receptionistUserObjectId = (Get-MsolUser -UserPrincipalName $receptionistUserName -ErrorAction SilentlyContinue -ErrorVariable errorVariable).ObjectID
$receptionistuserDetails = ""
if ($receptionistUserObjectId -eq $null)  
{    
    $receptionistuserDetails = New-MsolUser -UserPrincipalName $receptionistUserName -DisplayName "<NAME>" -FirstName "Edna" -LastName "Benson"
}

Set-MsolUserPassword -userPrincipalName $receptionistUserName -NewPassword $receptionistPassword -ForceChangePassword $false


Write-Host ("Created AD Users for SQL AD Admin, Receptinist and Doctor to test various scenarios" ) -ForegroundColor Gray
#------------------------------
Write-Host ("Step 2: Login to Azure AD and Azure. Please provide Global Administrator Credentials that has Owner/Contributor rights on the Azure Subscription ") -ForegroundColor Gray
Set-Location ".\"

$suffix = $suffix.Replace(' ', '').Trim()
$WebSiteName =         ("azurepcipaas" + $suffix)
$displayName =         ($suffix + "Azure PCI PAAS Sample")

# To login to Azure Resource Manager
	Try  
	{  
		Get-AzureRmContext -ErrorAction Continue  
	}  
	Catch [System.Management.Automation.PSInvalidOperationException]  
	{  
		 #Add-AzureRmAccount 
		Login-AzureRmAccount -SubscriptionId $subscriptionID
	} 

Start-Sleep -Seconds 10
# Grant 'SQL AD Admin' access to the Azure subscription
New-AzureRmRoleAssignment -ObjectId $sqlADAdminObjectId -RoleDefinitionName Contributor -Scope ('/subscriptions/' + $subscriptionID )

# To select a default subscription for your current session

$sub = Get-AzureRmSubscription -SubscriptionId $subscriptionID | Select-AzureRmSubscription 

### 2. Create Azure Active Directory apps in default directory
Write-Host ("Step 3: Create Azure Active Directory apps in default directory") -ForegroundColor Gray
    $u = (Get-AzureRmContext).Account
    $u1 = ($u -split '@')[0]
    $u2 = ($u -split '@')[1]
    $u3 = ($u2 -split '\.')[0]
    $defaultPrincipal = ($u1 + $u3 + ".onmicrosoft.com")
    # Get tenant ID
    $tenantID = (Get-AzureRmContext).Tenant.TenantId
    $homePageURL = ("http://" + $defaultPrincipal + "azurewebsites.net" + "/" + $WebSiteName)
    $replyURLs = @( $cloudwiseAppServiceURL, "http://*.azurewebsites.net","http://localhost:62080", "http://localhost:3026/")
    # Create Active Directory Application
    $azureAdApplication = New-AzureRmADApplication -DisplayName $displayName -HomePage $cloudwiseAppServiceURL -IdentifierUris $cloudwiseAppServiceURL -Password $AzureADApplicationClientSecret # -ReplyUrls $replyURLs
    Write-Host ("`tStep 3.1: Azure Active Directory apps creation successful. AppID is " + $azureAdApplication.ApplicationId) -ForegroundColor Gray

### 3. Create a service principal for the AD Application and add a Reader role to the principal

    Write-Host ("`tStep 3.2: Attempting to create Service Principal") -ForegroundColor Gray
    $principal = New-AzureRmADServicePrincipal -ApplicationId $azureAdApplication.ApplicationId
    Start-Sleep -s 30 # Wait till the ServicePrincipal is completely created. Usually takes 20+secs. Needed as Role assignment needs a fully deployed servicePrincipal
    Write-Host ("`tStep 3.3: Service Principal creation successful - " + $principal.DisplayName) -ForegroundColor Gray
    $scopedSubs = ("/subscriptions/" + $sub.Subscription)
    Write-Host ("`tStep 3.4: Attempting Reader Role assignment" ) -ForegroundColor Gray
    New-AzureRmRoleAssignment -RoleDefinitionName Reader -ServicePrincipalName $azureAdApplication.ApplicationId.Guid -Scope $scopedSubs
    Write-Host ("`tStep 3.5: Reader Role assignment successful" ) -ForegroundColor Gray

### 4. Create a Self-signed certificate for ASE ILB and Application Gateway.<commit_msg>removed all grey color ref<commit_after># Purpose : 
# 1) This script is used to create additional AD users to run various scenarios and creates AD Application and creates service principle to AD Application
# 2) This script should run by Global AD Administrator, You created Global AD Admin in previous script(CreateGlobalADAdmin.ps1)
# 3) This script should run before start deployment of ARM Templates
Param(
	[string] [Parameter(Mandatory=$true)] $azureADDomainName, # Provide your Azure AD Domain Name
	[string] [Parameter(Mandatory=$true)] $subscriptionID, # Provide your Azure subscription ID
	[string] [Parameter(Mandatory=$true)] $suffix, #This is used to create a unique website name in your organization. This could be your company name or business unit name
	# [string] [Parameter(Mandatory=$true)] , # Provide an SQL AD Admin Password for the user sqladmin@$azureADDomainName that complies to your AD's password policy. 
	[string] [Parameter(Mandatory=$true)] $azureADApplicationClientSecret, #Provide a Azure Application Password for setup of the app client access.
	[string] $customHostName = "pcipaas.com", # Provide CustomHostName which will be used for creating ASE subdomain.
	[bool]   $enableSSL = $false, # Provide boolean input to enable or disable SSL on application gateway 
	[string] $certificatePath # Provide Certificate path if you want to provide your own Application gateway certificate.
)

$ErrorActionPreference = 'Stop'

function Generate-Password ()
{
    (-join ((65..90) + (97..122) | Get-Random -Count 15 | % {[char]$_})) + (Get-Random -Maximum 9999)
}

function Convert-Certificate ($certPath)
{
$fileContentBytes = get-content "$certPath" -Encoding Byte
[System.Convert]::ToBase64String($fileContentBytes)
}

$ScriptFolder = Split-Path -Parent $PSCommandPath

###
#Imp: This script needs to be run by Global AD Administrator (aka Company Administrator)
###
Write-Host ("Pre-Requisite: This script needs to be run by Global AD Administrator (aka Company Administrator)" ) -ForegroundColor Yellow
#Connect to the Azure AD
Connect-MsolService
$SQLADAdminName = "sqladmin@"+$azureADDomainName
$receptionistUserName = "receptionist_EdnaB@"+$azureADDomainName

#Generate bunch of strong passwords
$sqlADAdminPassword = Generate-Password
$receptionistPassword = Generate-Password
$bastionPassword = Generate-Password

$cloudwiseAppServiceURL = "http://localcloudneeti6i"+$azureADDomainName
Write-Host ("Step 1:Create AD Users for SQL AD Admin, Receptinist and Doctor to test various scenarios" ) -ForegroundColor Yellow
$sqlADAdminObjectId = (Get-MsolUser -UserPrincipalName $SQLADAdminName -ErrorAction SilentlyContinue -ErrorVariable errorVariable).ObjectID
$sqlADAdminDetails = ""
if ($sqlADAdminObjectId -eq $null)  
{    
    $sqlADAdminDetails = New-MsolUser -UserPrincipalName $SQLADAdminName -DisplayName "SQLADAdministrator PCI Samples" -FirstName "SQL AD Administrator" -LastName "PCI Samples"
	$sqlADAdminObjectId= $sqlADAdminDetails.ObjectID
    # Make the new user a Global AD Administrator
	Add-MsolRoleMember -RoleName "Company Administrator" -RoleMemberObjectId $sqlADAdminObjectId
}
Set-MsolUserPassword -userPrincipalName $SQLADAdminName -NewPassword $sqlADAdminPassword -ForceChangePassword $false

$receptionistUserObjectId = (Get-MsolUser -UserPrincipalName $receptionistUserName -ErrorAction SilentlyContinue -ErrorVariable errorVariable).ObjectID
$receptionistuserDetails = ""
if ($receptionistUserObjectId -eq $null)  
{    
    $receptionistuserDetails = New-MsolUser -UserPrincipalName $receptionistUserName -DisplayName "<NAME>" -FirstName "Edna" -LastName "Benson"
}

Set-MsolUserPassword -userPrincipalName $receptionistUserName -NewPassword $receptionistPassword -ForceChangePassword $false


Write-Host ("Created AD Users for SQL AD Admin, and Receptinist user" ) -ForegroundColor Yellow
#------------------------------
Write-Host ("Step 2: Login to Azure AD and Azure. Please provide Global Administrator Credentials that has Owner/Contributor rights on the Azure Subscription ") -ForegroundColor yellow
Set-Location ".\"

$suffix = $suffix.Replace(' ', '').Trim()
$WebSiteName =         ("azurepcipaas" + $suffix)
$displayName =         ($suffix + "Azure PCI PAAS Sample")

# To login to Azure Resource Manager
	Try  
	{  
		Get-AzureRmContext -ErrorAction Continue  
	}  
	Catch [System.Management.Automation.PSInvalidOperationException]  
	{  
		 #Add-AzureRmAccount 
		Login-AzureRmAccount -SubscriptionId $subscriptionID
	} 

Start-Sleep -Seconds 10
# Grant 'SQL AD Admin' access to the Azure subscription
New-AzureRmRoleAssignment -ObjectId $sqlADAdminObjectId -RoleDefinitionName Contributor -Scope ('/subscriptions/' + $subscriptionID )

# To select a default subscription for your current session

$sub = Get-AzureRmSubscription -SubscriptionId $subscriptionID | Select-AzureRmSubscription 

### 2. Create Azure Active Directory apps in default directory
Write-Host ("Step 3: Create Azure Active Directory apps in default directory") -ForegroundColor Yellow
    $u = (Get-AzureRmContext).Account
    $u1 = ($u -split '@')[0]
    $u2 = ($u -split '@')[1]
    $u3 = ($u2 -split '\.')[0]
    $defaultPrincipal = ($u1 + $u3 + ".onmicrosoft.com")
    # Get tenant ID
    $tenantID = (Get-AzureRmContext).Tenant.TenantId
    $homePageURL = ("http://" + $defaultPrincipal + "azurewebsites.net" + "/" + $WebSiteName)
    $replyURLs = @( $cloudwiseAppServiceURL, "http://*.azurewebsites.net","http://localhost:62080", "http://localhost:3026/")
    # Create Active Directory Application
    $azureAdApplication = New-AzureRmADApplication -DisplayName $displayName -HomePage $cloudwiseAppServiceURL -IdentifierUris $cloudwiseAppServiceURL -Password $AzureADApplicationClientSecret # -ReplyUrls $replyURLs
    Write-Host ("`tStep 3.1: Azure Active Directory apps creation successful. AppID is " + $azureAdApplication.ApplicationId) -ForegroundColor Yellow

### 3. Create a service principal for the AD Application and add a Reader role to the principal

    Write-Host ("`tStep 3.2: Attempting to create Service Principal") -ForegroundColor Yellow
    $principal = New-AzureRmADServicePrincipal -ApplicationId $azureAdApplication.ApplicationId
    Start-Sleep -s 30 # Wait till the ServicePrincipal is completely created. Usually takes 20+secs. Needed as Role assignment needs a fully deployed servicePrincipal
    Write-Host ("`tStep 3.3: Service Principal creation successful - " + $principal.DisplayName) -ForegroundColor Yellow
    $scopedSubs = ("/subscriptions/" + $sub.Subscription)
    Write-Host ("`tStep 3.4: Attempting Reader Role assignment" ) -ForegroundColor Yellow
    New-AzureRmRoleAssignment -RoleDefinitionName Reader -ServicePrincipalName $azureAdApplication.ApplicationId.Guid -Scope $scopedSubs
    Write-Host ("`tStep 3.5: Reader Role assignment successful" ) -ForegroundColor Yellow

### 4. Create a Self-signed certificate for ASE ILB and Application Gateway.
{'fraction_non_alphanumeric': 0.09805615550755939, 'fraction_numerical': 0.00892728581713463, 'mean_word_length': 5.105934065934066, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3731313', 'n_tokens_mistral': 4014, 'n_tokens_neox': 3811, 'n_words': 1287}