Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Exchange Module V3 #2

Merged
merged 1 commit into from
Jan 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
303 changes: 180 additions & 123 deletions All-in-one setup/createform.ps1

Large diffs are not rendered by default.

23 changes: 23 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Change Log

All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com), and this project adheres to [Semantic Versioning](https://semver.org).

## [v2.0.0] - 03-06-2024

- Rework to new logging and use exchange online module V3.

## [v1.0.1] - 16-11-2021

- Added version number and updated all-in-one script.

## [v1.0.0] - 24-06-2021

This is the first official release of _HelloID-Conn-SA-Full-Exchange-Online-SharedMailboxDelete_.

### Added

### Changed

### Deprecated

### Removed
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
[
{
"description": null,
"translateDescription": false,
"inputFieldType": 1,
"key": "searchValue",
"type": 0,
"options": 1
}
{
"description": null,
"translateDescription": false,
"inputFieldType": 1,
"key": "searchValue",
"type": 0,
"options": 1
}
]
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
[
{
"key": "name",
"type": 0
},
{
"key": "primarySmtpAddress",
"type": 0
},
{
"key": "id",
"type": 0
},
{
"key": "userPrincipalName",
"type": 0
}
{
"key": "primarySmtpAddress",
"type": 0
},
{
"key": "userPrincipalName",
"type": 0
},
{
"key": "id",
"type": 0
},
{
"key": "name",
"type": 0
}
]
Original file line number Diff line number Diff line change
@@ -1,51 +1,136 @@
# Connect to Office 365
try{
Write-Information "Connecting to Office 365.."
#######################################################################
# Template: HelloID SA Powershell data source
# Name: Shared-mailbox-generate-table-delete
# Date: 28-11-2024
#######################################################################

$module = Import-Module ExchangeOnlineManagement
# For basic information about powershell data sources see:
# https://docs.helloid.com/en/service-automation/dynamic-forms/data-sources/powershell-data-sources.html

$securePassword = ConvertTo-SecureString $ExchangeOnlineAdminPassword -AsPlainText -Force
$credential = [System.Management.Automation.PSCredential]::new($ExchangeOnlineAdminUsername,$securePassword)
# Service automation variables:
# https://docs.helloid.com/en/service-automation/service-automation-variables.html

$exchangeSession = Connect-ExchangeOnline -Credential $credential -ShowBanner:$false -ShowProgress:$false -TrackPerformance:$false -ErrorAction Stop
#region init
# Enable TLS1.2
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor [System.Net.SecurityProtocolType]::Tls12

Write-Information "Successfully connected to Office 365"
}catch{
Write-Error "Could not connect to Exchange Online, error: $_"
}
$VerbosePreference = "SilentlyContinue"
$InformationPreference = "Continue"
$WarningPreference = "Continue"

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

# variables configured in form:
$searchValue = $datasource.searchValue
$searchQuery = "*$searchValue*"

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

try {
$searchValue = $datasource.searchValue
$searchQuery = "*$searchValue*"

if(-not [String]::IsNullOrEmpty($searchValue)) {
#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"

if (-not [String]::IsNullOrEmpty($searchValue)) {
Write-information "searchQuery: $searchQuery"

$exchangeMailboxes = Get-Mailbox -Filter "{Alias -like '$searchQuery' -or Name -like '$searchQuery'}" -RecipientTypeDetails SharedMailbox -resultSize unlimited
$SharedMailboxParams = @{
Filter = "{Alias -like '$searchQuery' -or Name -like '$searchQuery'}"
RecipientTypeDetails = "SharedMailbox"
ResultSize = "Unlimited"
Verbose = $false
ErrorAction = "Stop"
}

$mailboxes = Get-Mailbox @SharedMailboxParams

$mailboxes = $exchangeMailboxes
$resultCount = @($mailboxes).Count

Write-Information "Result count: $resultCount"

if($resultCount -gt 0){
foreach($mailbox in $mailboxes){
if ($resultCount -gt 0) {
foreach ($mailbox in $mailboxes) {
$returnObject = @{
name="$($mailbox.displayName)";
id="$($mailbox.id)";
primarySmtpAddress ="$($mailbox.PrimarySmtpAddress)";
userPrincipalName ="$($mailbox.UserPrincipalName)"
name = "$($mailbox.displayName)";
id = "$($mailbox.ExchangeGuid)";
primarySmtpAddress = "$($mailbox.PrimarySmtpAddress)";
userPrincipalName = "$($mailbox.UserPrincipalName)"
}

Write-Output $returnObject
}
}
}
} catch {
$errorDetailsMessage = ($_.ErrorDetails.Message | ConvertFrom-Json).error.message
Write-Error ("Error searching for Exchange Shared mailboxes. Error: $($_)" + $errorDetailsMessage)
} finally {
Write-Information "Disconnecting from Office 365.."
$exchangeSessionEnd = Disconnect-ExchangeOnline -Confirm:$false -Verbose:$false -ErrorAction Stop
Write-Information "Successfully disconnected from Office 365"
#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 with the query [$searchQuery]. Error: $errorMessage"
}
#endregion lookup
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "Exchange Online - Shared Mailbox - Delete",
"runInCloud": false
}
139 changes: 139 additions & 0 deletions Manual resources/[task]_Exchange Online - Shared Mailbox - Delete.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
#######################################################################
# Template: HelloID SA Delegated form task
# Name: Exchange Online Shared Mailbox - Delete
# Date: 28-11-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

# Enable TLS1.2
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor [System.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:
$exchangeMailGUID = $form.sharedMailbox.id
$exchangeMailName = $form.sharedMailbox.name

# PowerShell commands to import
$commands = @("Get-User", "Remove-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 = "deleting shared mailbox"
$RemoveMailboxParams = @{
Identity = $exchangeMailGUID
ErrorAction = 'Stop'
Confirm = $false
}

Remove-Mailbox @RemoveMailboxParams

Write-Information "Shared Mailbox [$exchangeMailName] deleted successfully"
$Log = @{
Action = "DeleteResource" # optional. ENUM (undefined = default)
System = "Exchange Online" # optional (free format text)
Message = "Shared Mailbox [$exchangeMailName] deleted successfully" # required (free format text)
IsError = $false # optional. Elastic reporting purposes only. (default = $false. $true = Executed action returned an error)
TargetDisplayName = $exchangeMailName # optional (free format text)
TargetIdentifier = $([string]$exchangeMailGUID) # optional (free format text)
}
#send result back
Write-Information -Tags "Audit" -MessageData $log
#endregion create 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 [$exchangeMailName]. Error: $errorMessage"

$Log = @{
Action = "CreateResource" # optional. ENUM (undefined = default)
System = "Exchange Online" # optional (free format text)
Message = "Error $actionMessage for Exchange Online shared mailbox [$exchangeMailName]" # required (free format text)
IsError = $true # optional. Elastic reporting purposes only. (default = $false. $true = Executed action returned an error)
TargetDisplayName = $exchangeMailName # optional (free format text)
TargetIdentifier = $([string]$exchangeMailGUID) # optional (free format text)
}
#send result back
Write-Information -Tags "Audit" -MessageData $log
}
4 changes: 0 additions & 4 deletions Manual resources/[task]_shared-mailbox-delete.mapping.json

This file was deleted.

Loading