Skip to content

Instantly share code, notes, and snippets.

@neerajks77
Last active October 8, 2022 05:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save neerajks77/007e5a785aef5df2e7b59b5d1c0a182e to your computer and use it in GitHub Desktop.
Save neerajks77/007e5a785aef5df2e7b59b5d1c0a182e to your computer and use it in GitHub Desktop.
This PowerShell Runbook script is used to provision Email accounts in Exchange Online and it also creates the user in azure Active Directory. It will also check if the request for new email is for the correct domain and the email already exists or not and accordingly populates the variables, which can be used to log errors. This script depends o…
###############################################
#Author:Neeraj Kumar
#Description:This script intakes the parameters for email creation and processes the request. It also created a user in Azure AD
#Date: 03/10/2022
###############################################
param(
[Parameter(Mandatory=$true)]
[string] $EmailIDtobeProvisioned,
[Parameter(Mandatory=$true)]
[string] $Alias,
[Parameter(Mandatory=$true)]
[string] $FirstName,
[Parameter(Mandatory=$true)]
[string] $LastName
)
# Enforcing validation of code
Set-StrictMode -Version Latest
##################################################
#Connect to ExchangeOnline and process Email Creation Request
##################################################
Function ProvisionEmailAccount(){
try{
if ($EmailIDtobeProvisioned -match "^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$")
{
try
{
#Check if the email request if for the correct domain
$position = $EmailIDtobeProvisioned.IndexOf("@")
$tld = $EmailIDtobeProvisioned.Substring($position+1)
if($tld -ne $Organization)
{
$Global:LogMessage = "The email address requested is for an incorrect domain. The ticket has been marked as closed imcompleted."
$Global:State = "4"
$Global:RequestState = "Closed_Incomplete"
}
else{
#Connect to Exchange Online
Connect-ExchangeOnline -AppId $AppId -CertificateThumbPrint $ThumbPrint -Organization $Organization -ShowBanner:$false -ShowProgress:$false #-ErrorAction Stop
#Check if the email already exists for the user. If exists, update the ticket with comments
[string] $GetEmail = Get-Mailbox -Identity $EmailIDtobeProvisioned -ErrorAction SilentlyContinue
#Write 'GetEmail: ' $GetEmail
if (($GetEmail -eq "") -or ($GetEmail -eq $null)){
Write 'Correctly Inside Email Creation'
try{
New-Mailbox -Alias $Alias -Name $DisplayName -FirstName $FirstName -LastName $LastName -DisplayName $DisplayName -MicrosoftOnlineServicesID $EmailIDtobeProvisioned -Password (ConvertTo-SecureString -String 'P@ssw0rd' -AsPlainText -Force) -ResetPasswordOnNextLogon $true
$Global:LogMessage = "Mailbox has been provisioned for "+ $FirstName + " with Email ID - " + $EmailIDtobeProvisioned + " and the user has been provisioned in the Azure AD. Request Completed by Neeraj Kumar. Please change the default password. Default password is P@ssw0rd"
$Global:State = "3"
$Global:RequestState = "Closed_Complete"
$Global:EmailProvisioned = $true
}
catch{
Write-Host $_.Exception.ToString()
$error[0] | Format-List -Force
}
}
else{
$Global:LogMessage = "The email address requested already exists. The ticket has been marked as closed imcompleted."
$Global:State = "4"
$Global:RequestState = "Closed_Incomplete"
}
}
}
Catch
{
Write-Host $_.Exception.ToString()
$error[0] | Format-List -Force
}
}
}
Catch
{
Write-Host $_.Exception.ToString()
$error[0] | Format-List -Force
}
}
# Initiate the variable here
##################################
[Boolean] $EmailExists = $false
[Boolean] $Global:EmailProvisioned = $false
[string] $Organization = Get-AutomationVariable -Name 'Organization'
[string] $Username = Get-AutomationVariable -Name 'username'
[string] $Password = Get-AutomationVariable -Name 'password'
[string] $ClientID = Get-AutomationVariable -Name 'ClientID'
[string] $ClientSecret = Get-AutomationVariable -Name 'ClientSecret'
[string] $Thumbprint = Get-AutomationVariable -Name 'CERT_THUMBPRINT'
[string] $upn = Get-AutomationVariable -Name 'upnname'
[string] $AppId = Get-AutomationVariable -Name 'AppID'
#[string] $ExchangeOnlineCertThumbPrint = (Get-AzureAutomationCertificate -ServiceName "ServiceNowIntegration" -Name "ExchangeCert").Thumbprint
[string] $Global:LogMessage = ''
[string] $Global:State = "2"
[string] $Global:RequestState = "2"
[string] $DisplayName = $FirstName + ' ' + $LastName
#####################################
# call the function to provision email id
#####################################
ProvisionEmailAccount
################################################################# End Main Section ############################################################
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment