Skip to content

Commit

Permalink
Feat: rework (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
rhouthuijzen authored Jun 3, 2024
1 parent e24adb6 commit e7686c1
Show file tree
Hide file tree
Showing 14 changed files with 753 additions and 374 deletions.
382 changes: 221 additions & 161 deletions All-in-one setup/createform.ps1

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
[
{
"description": null,
"translateDescription": false,
"inputFieldType": 1,
"key": "Name",
"type": 0,
"options": 1
},
{
"description": null,
"translateDescription": false,
"inputFieldType": 1,
"key": "Alias",
"type": 0,
"options": 1
},
{
"description": null,
"translateDescription": false,
"inputFieldType": 1,
"key": "Organization",
"type": 0,
"options": 1
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[
{
"key": "text",
"type": 0
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
#######################################################################
# Template: RHo HelloID SA Powershell data source
# Name: function-check-online-mailbox-exists
# Date: 05-02-2024
#######################################################################

# For basic information about powershell data sources see:
# https://docs.helloid.com/en/service-automation/dynamic-forms/data-sources/powershell-data-sources.html

# Service automation variables:
# https://docs.helloid.com/en/service-automation/service-automation-variables.html

#region init
# Set TLS to accept TLS, TLS 1.1 and TLS 1.2
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls -bor [Net.SecurityProtocolType]::Tls11 -bor [Net.SecurityProtocolType]::Tls12

$VerbosePreference = "SilentlyContinue"
$InformationPreference = "Continue"
$WarningPreference = "Continue"

# global variables (Automation --> Variable libary):
$TenantId = $EntraTenantId
$AppID = $EntraAppID
$Secret = $EntraSecret
$Organization = $EntraOrganization

# variables configured in form:
$Maildomain = $datasource.Organization.Maildomain
$Name = $datasource.name
$Alias = $datasource.alias
$PrimarySmtpAddress = $Alias.Replace(" ", "") + "@$Maildomain"

# PowerShell commands to import
$commands = @("Get-User", "Get-Mailbox")
#endregion init

try {
#region import module
$actionMessage = "importing $moduleName module"

$importModuleParams = @{
Name = "ExchangeOnlineManagement"
Cmdlet = $commands
ErrorAction = 'Stop'
}

Import-Module @importModuleParams
#endregion import module

#region create access token
Write-Verbose "Creating Access Token"
$actionMessage = "creating access token"

$body = @{
grant_type = "client_credentials"
client_id = "$AppID"
client_secret = "$Secret"
resource = "https://outlook.office365.com"
}

$exchangeAccessTokenParams = @{
Method = 'POST'
Uri = "https://login.microsoftonline.com/$TenantId/oauth2/token"
Body = $body
ContentType = 'application/x-www-form-urlencoded'
UseBasicParsing = $true
}

$accessToken = (Invoke-RestMethod @exchangeAccessTokenParams).access_token
#endregion create access token

#region connect to Exchange Online
Write-Verbose "Connecting to Exchange Online"
$actionMessage = "connecting to Exchange Online"

$exchangeSessionParams = @{
Organization = $Organization
AppID = $AppID
AccessToken = $accessToken
CommandName = $commands
ShowBanner = $false
ShowProgress = $false
TrackPerformance = $false
ErrorAction = 'Stop'
}
Connect-ExchangeOnline @exchangeSessionParams

Write-Information "Successfully connected to Exchange Online"
#endregion connect to Exchange Online

#region check shared mailbox
$actionMessage = "getting shared mailbox"

$SharedMailboxParams = @{
Filter = "{Alias -eq '$Alias' -or Name -eq '$Name' -or PrimarySmtpAddress -eq '$PrimarySmtpAddress'}"
# RecipientTypeDetails = 'SharedMailbox'
ErrorAction = 'Stop'
}

$SharedMailbox = Get-Mailbox @SharedMailboxParams

if ([string]::IsNullOrEmpty($SharedMailbox)) {
Write-Information "Shared Mailbox name [$Name] is available"
$outputMessage = "Valid | Shared Mailbox name [$Name] is available"
$returnObject = @{
text = $outputMessage
}
}
else {
Write-Information "Shared Mailbox [$Name] exists. Please try another name"
$outputMessage = "Invalid | Shared Mailbox name [$Name] exists. Please try another name"
$returnObject = @{
text = $outputMessage
}
}
#endregion check shared mailbox
}
catch {
$ex = $PSItem
if ($($ex.Exception.GetType().FullName -eq 'Microsoft.PowerShell.Commands.HttpResponseException') -or
$($ex.Exception.GetType().FullName -eq 'System.Net.WebException')) {
$errorMessage = ($ex.ErrorDetails.Message | Convertfrom-json).error_description
}
else {
$errorMessage = $($ex.Exception.message)
}

Write-Error "Error $actionMessage for Exchange Online shared mailbox [$Name]. Error: $errorMessage"

$outputMessage = "Invalid | Error $actionMessage for Exchange Online shared mailbox [$Name]. Error: $errorMessage"
$returnObject = @{
text = $outputMessage
}
}
finally {
Write-Output $returnObject
}
#endregion lookup
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[
{
"Naam": "Enyoi",
"Maildomain": "enyoi.org"
},
{
"Naam": "Tools4ever",
"Maildomain": "Tools4ever.com"
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[
{
"key": "Naam",
"type": 0
},
{
"key": "Maildomain",
"type": 0
}
]

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "Exchange Online - Shared Mailbox - Create",
"runInCloud": false
}
169 changes: 169 additions & 0 deletions Manual resources/[task]_Exchange Online - Shared Mailbox - Create.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
#######################################################################
# Template: RHo HelloID SA Delegated form task
# Name: Exchange Online Shared Mailbox - Create
# Date: 02-05-2024
#######################################################################

# For basic information about delegated form tasks see:
# https://docs.helloid.com/en/service-automation/delegated-forms/delegated-form-powershell-scripts.html

# Service automation variables:
# https://docs.helloid.com/en/service-automation/service-automation-variables.html

#region init

# Set TLS to accept TLS, TLS 1.1 and TLS 1.2
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls -bor [Net.SecurityProtocolType]::Tls11 -bor [Net.SecurityProtocolType]::Tls12

$VerbosePreference = "SilentlyContinue"
$InformationPreference = "Continue"
$WarningPreference = "Continue"

# global variables (Automation --> Variable libary):
$TenantId = $EntraTenantId
$AppID = $EntraAppID
$Secret = $EntraSecret
$Organization = $EntraOrganization

# variables configured in form:
$Maildomain = $form.organization.Maildomain
$Name = $form.name
$Alias = $form.alias

# PowerShell commands to import
$commands = @("Get-User", "New-Mailbox", "Set-Mailbox")
#endregion init

#region functions

#endregion functions

try {
#region import module
$actionMessage = "importing $moduleName module"

$importModuleParams = @{
Name = "ExchangeOnlineManagement"
Cmdlet = $commands
ErrorAction = 'Stop'
}

Import-Module @importModuleParams
#endregion import module

#region create access token
Write-Verbose "Creating Access Token"
$actionMessage = "creating access token"

$body = @{
grant_type = "client_credentials"
client_id = "$AppID"
client_secret = "$Secret"
resource = "https://outlook.office365.com"
}

$exchangeAccessTokenParams = @{
Method = 'POST'
Uri = "https://login.microsoftonline.com/$TenantId/oauth2/token"
Body = $body
ContentType = 'application/x-www-form-urlencoded'
UseBasicParsing = $true
}

$accessToken = (Invoke-RestMethod @exchangeAccessTokenParams).access_token
#endregion create access token

#region connect to Exchange Online
Write-Verbose "Connecting to Exchange Online"
$actionMessage = "connecting to Exchange Online"

$exchangeSessionParams = @{
Organization = $Organization
AppID = $AppID
AccessToken = $accessToken
CommandName = $commands
ShowBanner = $false
ShowProgress = $false
TrackPerformance = $false
ErrorAction = 'Stop'
}
Connect-ExchangeOnline @exchangeSessionParams

Write-Information "Successfully connected to Exchange Online"
#endregion connect to Exchange Online

#region create shared mailbox
$actionMessage = "creating shared mailbox"
$CreateMailboxParams = @{
Shared = $true
Name = $Name
DisplayName = $Name
PrimarySmtpAddress = $Alias.Replace(" ", "") + "@$Maildomain"
Alias = $Alias.Replace(" ", "")
ErrorAction = 'Stop'
}

New-Mailbox @CreateMailboxParams

Write-Information "Shared Mailbox [$Name] created successfully"
$Log = @{
Action = "CreateResource" # optional. ENUM (undefined = default)
System = "Exchange Online" # optional (free format text)
Message = "Shared Mailbox [$Name] created successfully" # required (free format text)
IsError = $false # optional. Elastic reporting purposes only. (default = $false. $true = Executed action returned an error)
TargetDisplayName = $Name # optional (free format text)
TargetIdentifier = $([string]$Alias) # optional (free format text)
}
#send result back
Write-Information -Tags "Audit" -MessageData $log
#endregion create shared mailbox

#region update shared mailbox
$actionMessage = "updating shared mailbox"
Start-Sleep -Seconds 10

$UpdateMailboxParams = @{
Identity = "$($CreateMailboxParams.PrimarySmtpAddress)"
MessageCopyForSendOnBehalfEnabled = $true
MessageCopyForSentAsEnabled = $true
ErrorAction = 'Stop'
}

Set-Mailbox @UpdateMailboxParams

Write-Information "Shared Mailbox [$Name] updated successfully"
$Log = @{
Action = "CreateResource" # optional. ENUM (undefined = default)
System = "Exchange Online" # optional (free format text)
Message = "Shared Mailbox [$Name] updated successfully" # required (free format text)
IsError = $false # optional. Elastic reporting purposes only. (default = $false. $true = Executed action returned an error)
TargetDisplayName = $Name # optional (free format text)
TargetIdentifier = $([string]$Alias) # optional (free format text)
}
#send result back
Write-Information -Tags "Audit" -MessageData $log
#endregion update shared mailbox
}
catch {
$ex = $PSItem
if ($($ex.Exception.GetType().FullName -eq 'Microsoft.PowerShell.Commands.HttpResponseException') -or
$($ex.Exception.GetType().FullName -eq 'System.Net.WebException')) {
$errorMessage = ($ex.ErrorDetails.Message | Convertfrom-json).error_description
}
else {
$errorMessage = $($ex.Exception.message)
}

Write-Error "Error $actionMessage for Exchange Online shared mailbox [$Name]. Error: $errorMessage"

$Log = @{
Action = "CreateResource" # optional. ENUM (undefined = default)
System = "Exchange Online" # optional (free format text)
Message = "Error $actionMessage for Exchange Online shared mailbox [$Name]" # required (free format text)
IsError = $true # optional. Elastic reporting purposes only. (default = $false. $true = Executed action returned an error)
TargetDisplayName = $Name # optional (free format text)
TargetIdentifier = $([string]$Alias) # optional (free format text)
}
#send result back
Write-Information -Tags "Audit" -MessageData $log
}
Loading

0 comments on commit e7686c1

Please sign in to comment.