I have a script that sends emails from Azure Automation. I am trying to reduce redundancy. The email portion of my script uses if
statements for group membership and sends the email subject and message based on group membership.
if ( $groups.DisplayName -eq "Finance" ) {
$params = @{
mail_subject = "User added to the Finance department."
mail_message = "A user has been added to the Finance department."
}
...followed by several more `elseif` statements for the remaining departments.
I'm trying to establish a single variable for the $mail_subject
and $mail_message
variables, then replace the name of the department. Something like this,
# This variable declared at the beginning of the script
$subject = "User added to the $($dept) department"
$message = "A user has been added to the $($dept) department".
... later in the script ...
if ( $groups.DisplayName -eq "Finance" ) {
$dept = "Finance"
$params = @{
mail_subject = $subject
mail_message = $message
}
...followed by the remaining `elseif` statements.
What's happening is the department name will not be in the email. How can I update the $subject
variable to contain the new $dept
based on the if statements? I'm trying to avoid having to type out the mail subject and message under each if
and elseif
(the actual message is much longer than the example here).
I've tried updating the $message
variable using the -update
switch, but that did not work.
if ( $groups.DisplayName -eq "Finance" ) {
$dept = "Finance"
$message = $message -update $dept, $dept
$params = @{
mail_subject = $subject
mail_message = $message
}
...followed by the remaining `elseif` statements.
What am I doing wrong?
What you're looking for can be accomplished via string formatting (the -f
operator), the code would be like:
# This variable declared at the beginning of the script
$subject = 'User added to the {0} department'
$message = 'A user has been added to the {0} department.'
# ... later in the script ...
if ($groups.DisplayName -eq 'Finance') {
$params = @{
mail_subject = $subject -f 'Finance'
mail_message = $message -f 'Finance'
}
}
# ...followed by the remaining `elseif` statements.
You could take it a step further and remove the chained if
conditions if the .DisplayName
value is always what you want to interpolate in the subject and message:
# This variable declared at the beginning of the script
$subject = 'User added to the {0} department'
$message = 'A user has been added to the {0} department.'
$params = @{
mail_subject = $subject -f $groups.DisplayName
mail_message = $message -f $groups.DisplayName
}
Or, you might be able to use a hash table where the keys are the .DisplayName
value and the values are what you want to interpolate in those strings, so something like:
# This variable declared at the beginning of the script
$subject = 'User added to the {0} department'
$message = 'A user has been added to the {0} department.'
$hash = @{
Finance = 'some value to inject for finance department'
Accounting = 'some value to inject for accounting department'
IT = 'some value to inject for IT department'
}
$params = @{
mail_subject = $subject -f $hash[$groups.DisplayName]
mail_message = $message -f $hash[$groups.DisplayName]
}