Skip to content
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
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
# Changelog for ScriptMessage PowerShell Module

## [1.0.7](https://github.com/Sekers/ScriptMessage/tree/1.0.7) - (2024-12-26)

### Features

- New messaging class "MessageServiceType" and corresponding parameters for the Send-ScriptMessage Cmdlet to allow multiple/combined service & type parameters in one call.

### Other

- Results returned from Send-ScriptMessage has recipients adjusted from Hashtables to PSCustomObjects. This better handles collections (arrays) of addresses than hashtables being returned. Originally it was hashtables to mimic what Graph uses but that's not great for our purpose.

Author: [**@Sekers**](https://github.com/Sekers)

---
## [1.0.6](https://github.com/Sekers/ScriptMessage/tree/1.0.6) - (2024-12-19)

### Fixes
Expand Down
46 changes: 32 additions & 14 deletions ScriptMessage/Public/Send-ScriptMessage.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -135,17 +135,26 @@ function Send-ScriptMessage
[CmdletBinding()]
param(
[Parameter(
ParameterSetName = 'ServiceAndTypeSeparate',
Mandatory = $true,
ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true)]
[MessagingService[]]$Service,

[Parameter(
ParameterSetName = 'ServiceAndTypeSeparate',
Mandatory = $false,
ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true)]
[MessageType[]]$Type = 'Mail',

[Parameter(
ParameterSetName = 'ServiceAndTypeCombined',
Mandatory = $true,
ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true)]
[MessageServiceType[]]$ServiceType,

[Parameter(
Mandatory = $true,
ValueFromPipeline = $true,
Expand Down Expand Up @@ -212,17 +221,19 @@ function Send-ScriptMessage
)

begin
{
# Set the necessary configuration variables.
$ScriptMessageConfig = Get-ScriptMessageConfig
}

process
{
# Make sure that at least one of, To, CC, or BCC is provided.
if ([string]::IsNullOrEmpty($To) -and [string]::IsNullOrEmpty($CC) -and [string]::IsNullOrEmpty($BCC))
{
throw 'Please provide at least one parameter value for any of the following: To, CC, or BCC'
}

# Remove Message Service & Message Type duplicates.
$Service = $Service | Select-Object -Unique
$Type = $Type | Select-Object -Unique

# Convert recipient types into properly formatted PSObject.
$From = ConvertTo-ScriptMessageRecipientObject -Recipient $From # Note that From is NOT an array. There should only be one.
[array]$ReplyTo = ConvertTo-ScriptMessageRecipientObject -Recipient $ReplyTo
Expand All @@ -233,23 +244,30 @@ function Send-ScriptMessage
# Convert body into properly formatted PSObject
$Body = ConvertTo-ScriptMessageBodyObject -Body $Body

# Set the necessary configuration variables.
$ScriptMessageConfig = Get-ScriptMessageConfig
}
if ($null -ne $Service) # If ServiceAndTypeSeparate
{
# Remove message service & message type duplicates.
$Service = $Service | Select-Object -Unique
$Type = $Type | Select-Object -Unique

process
{
foreach ($serviceItem in $Service)
# Create the ServiceType class object\hash.
[MessageServiceType]$ServiceType = @{
Service = $Service
Type = $Type
}
}

foreach ($serviceTypeObj in $ServiceType)
{
# Set the connection parameters.
$ConnectionParameters = @{
ServiceConfig = $ScriptMessageConfig.$serviceItem
ServiceConfig = $ScriptMessageConfig.$($serviceTypeObj.Service)
}

# Connect to the messaging service, if necessary (e.g., API service).
Connect-ScriptMessage -Service $serviceItem -ErrorAction Stop
Connect-ScriptMessage -Service $($serviceTypeObj.Service) -ErrorAction Stop

switch ($serviceItem)
switch ($($serviceTypeObj.Service))
{
'MgGraph' {
$SendMessageParameters = [ordered]@{
Expand All @@ -263,7 +281,7 @@ function Send-ScriptMessage
Body = $Body
Attachment = $Attachment
SenderId = $SenderId
Type = $Type
Type = $serviceTypeObj.Type
}

Send-ScriptMessage_MgGraph @SendMessageParameters
Expand Down
2 changes: 1 addition & 1 deletion ScriptMessage/ScriptMessage.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
RootModule = 'ScriptMessage.psm1'

# Version number of this module.
ModuleVersion = '1.0.6'
ModuleVersion = '1.0.7'

# Supported PSEditions
CompatiblePSEditions = @('Desktop','Core')
Expand Down
8 changes: 7 additions & 1 deletion ScriptMessage/ScriptMessage.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Public Enum
# Name: MessagingService
enum MessagingService {
MgGraph
MgGraph
}

# Public Enum
Expand All @@ -13,6 +13,12 @@ enum MessageType {
Chat
}

# Public Class
class MessageServiceType {
[MessagingService[]]$Service
[MessageType[]]$Type
}

# Import Private Functions
$ScriptMessageFunctions = @(Get-ChildItem -Path $PSScriptRoot\Private\*.ps1)
Foreach($ScriptMessageFunction in $ScriptMessageFunctions)
Expand Down
51 changes: 29 additions & 22 deletions ScriptMessage/Services/MgGraph.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -270,9 +270,6 @@ function Send-ScriptMessage_MgGraph
{
# Set the Service ID.
$ServiceId = 'MgGraph'

# Set the necessary configuration variables.
$ScriptMessageConfig = Get-ScriptMessageConfig
}

process
Expand Down Expand Up @@ -329,26 +326,36 @@ function Send-ScriptMessage_MgGraph
$SendEmailMessageResult = Send-MgUserMail -UserId $SenderId -BodyParameter $EmailParams -PassThru

# Collect Return Info
$SendScriptMessageResult = [ordered]@{}
$SendScriptMessageResult.MessageService = $ServiceId
$SendScriptMessageResult.MessageType = $typeItem
$SendScriptMessageResult.Status = $SendEmailMessageResult # The SDK only returns $true and nothing else (and only that because of the 'PassThru')
$SendScriptMessageResult.Error = $null
$SendScriptMessageResult.SentFrom = @{}
$SendScriptMessageResult.SentFrom.Name = $From.Name
$SendScriptMessageResult.SentFrom.Address = $From.AddressObj
$SendScriptMessageResult.Recipients = [ordered]@{}
$SendScriptMessageResult.Recipients.All = $null # Create this before populating for ordered list purposes.
[array]$SendScriptMessageResult.Recipients.To = @(($Message.To).EmailAddress | Sort-Object $_.Value)
[array]$SendScriptMessageResult.Recipients.CC = @(($Message.CC).EmailAddress | Sort-Object $_.Value)
[array]$SendScriptMessageResult.Recipients.BCC = @(($Message.BCC).EmailAddress | Sort-Object $_.Value)
[array]$SendScriptMessageResult.Recipients.All = @( # Since Address is also a PSMethod we need to do some fun stuff (List<psobject> doesn't have a method called Address) so we don't get the dreaded 'OverloadDefinitions'.
[System.Linq.Enumerable]::ToList([psobject[]]$SendScriptMessageResult.Recipients.To).Address
[System.Linq.Enumerable]::ToList([psobject[]]$SendScriptMessageResult.Recipients.CC).Address
[System.Linq.Enumerable]::ToList([psobject[]]$SendScriptMessageResult.Recipients.BCC).Address
$SendScriptMessageResult_SentFrom = [PSCustomObject]@{
Name = $From.Name
Address = $From.AddressObj
}

[array]$SendScriptMessageResult_Recipients_To = foreach ($i in ($Message.To).EmailAddress) {[PSCustomObject]$i} # Converts hashtables to PSCustomObjects
[array]$SendScriptMessageResult_Recipients_CC = foreach ($i in ($Message.CC).EmailAddress) {[PSCustomObject]$i} # Converts hashtables to PSCustomObjects
[array]$SendScriptMessageResult_Recipients_BCC = foreach ($i in ($Message.BCC).EmailAddress) {[PSCustomObject]$i} # Converts hashtables to PSCustomObjects
[array]$SendScriptMessageResult_Recipients_All = @( # Since Address is also a PSMethod we need to do some fun stuff (List<psobject> doesn't have a method called Address) so we don't get the dreaded 'OverloadDefinitions'.
[System.Linq.Enumerable]::ToList([PSObject[]]$SendScriptMessageResult_Recipients_To).Address
[System.Linq.Enumerable]::ToList([PSObject[]]$SendScriptMessageResult_Recipients_CC).Address
[System.Linq.Enumerable]::ToList([PSObject[]]$SendScriptMessageResult_Recipients_BCC).Address
)
[array]$SendScriptMessageResult.Recipients.All = $SendScriptMessageResult.Recipients.All | Sort-Object -Unique # Remove duplicate items.

[array]$SendScriptMessageResult_Recipients_All = $SendScriptMessageResult_Recipients_All | Sort-Object -Unique # Remove duplicate items.
$SendScriptMessageResult_Recipients = [PSCustomObject]@{
To = $SendScriptMessageResult_Recipients_To
CC = $SendScriptMessageResult_Recipients_CC
BCC = $SendScriptMessageResult_Recipients_BCC
All = $SendScriptMessageResult_Recipients_All
}

$SendScriptMessageResult = [PSCustomObject]@{
MessageService = $ServiceId
MessageType = $typeItem
Status = $SendEmailMessageResult # The SDK only returns $true and nothing else (and only that because of the 'PassThru')
Error = $null
SentFrom = $SendScriptMessageResult_SentFrom
Recipients = $SendScriptMessageResult_Recipients
}

# If successful, output result info.
$SendScriptMessageResult
}
Expand Down