Home Advanced Request Offering
Options

Powershell - send email to affected user

Ryan_KelleyRyan_Kelley Customer IT Monkey ✭

User case: To send an email to affected user telling them that a ticket was made for them and security will reach out when possible but otherwise return to work. My script does not appear to be picking up the parent ID nor the affected user's email.

I am trying to get the email through AD but I do see an email in the user profile under notification/smtp.


Script:

# Parameters

param([guid]$parentId)


# Import the Active Directory module

if(-not(Get-Module ActiveDirectory))

{

Import-Module ActiveDirectory

Write-Output"Active Directory module imported."

}


if(-not(Get-Module Smlets))

{

Import-Module SMLets

Write-Output"SMLets module imported."

}


# Define the SMTP server, port and credential

$smtpServer = "exchange22.shermfin.com"

$smtpPort = 225


# Define sender and recipient email addresses

$from = "itservicenotifications@resurgent.com"

$cc = "rkelley@resurgent.com"


# Get the service request object

$ticket = Get-SCSMObject -Class (Get-SCSMClass -Name System.WorkItem.ServiceRequest$) -Filter"Id -eq $parentId"

if ($null-eq$ticket) {

Write-Output"Failed to retrieve the service request object with Id: $parentId"

exit1

} else {

Write-Output"Service request object retrieved: $($ticket.Id)"

}


# Get the Affected User

$affectedUser = Get-SCSMRelatedObject -SMObject $ticket | Where-Object { $_.DisplayName -like"Affected User*" }

if ($null-eq$affectedUser) {

Write-Output"Failed to retrieve the Affected User from ticket: $($ticket.Id)"

exit1

} else {

Write-Output"Affected User: $($affectedUser.UserName)"

}


# Fetch the user's details from AD

$adUser = Get-ADUser -Filter { SamAccountName -eq$affectedUser.UserName }

if ($null-eq$adUser) {

Write-Output"Failed to retrieve the AD User: $($affectedUser.UserName)"

exit1

} else {

Write-Output"AD User: $($adUser.SamAccountName)"

}


# Get the Affected User's email from AD

$to = $adUser.Email

if ($null-eq$to) {

Write-Output"Failed to retrieve the email address for AD User: $($adUser.SamAccountName)"

exit1

} else {

Write-Output"Recipient email address: $to"

}


# Define the subject of the email

$subject = "Security notification"


# Define the body of the email

$body = @"

Dear User,


We wish to inform you that a ticket has been generated within our Security Sentinel system in response to your recent service request. Our Security Team has been notified and will be reaching out to you at the earliest convenience.


In the interim, we kindly ask you to continue with your work as usual.


Should you experience any inability to perform your work or if immediate assistance is required, please do not hesitate to contact our Service Desk at 1 877-342-3670, or create a ticket on the Help Portal.


We appreciate your patience and cooperation in this matter.


Sincerely,

Your IT Team

"@


# Send the email

try {

Send-MailMessage -SmtpServer $smtpServer -Port $smtpPort -UseSsl -From$from -To $to -Cc $cc -Subject $subject -Body $body -ErrorAction Stop

Write-Output"Email sent successfully."

}

catch {

Write-Output"Error sending email: $_"

exit1

}


# Unloading the smlets and ActiveDirectory modules

try {

Remove-Module smlets -ErrorAction Stop

Remove-Module ActiveDirectory -ErrorAction Stop

Write-Output"Modules unloaded successfully."

}

catch {

Write-Output"Error unloading modules: $_"

exit1

}


When I run as is it says:

Active Directory module imported.

SMLets module imported.

Failed to retrieve the service request object with Id: c0a23c77-528e-be6e-6588-0a1dede7750c


When I take out the parent id check it says:

An error was written to the Error stream!

Cannot convert null to type "System.Guid".

An error was written to the Error stream!

Cannot convert null to type "System.Guid".Cannot bind argument to parameter 'SMObject' because it is null.

An error was written to the Error stream!

Cannot convert null to type "System.Guid".Cannot bind argument to parameter 'SMObject' because it is null.Variable: 'affectedUser' found in expression: $affectedUser.UserName is not defined.

Active Directory module imported.

SMLets module imported.

Service request object retrieved: 

Affected User: 

AD User: 

Recipient email address: 

Error sending email: Cannot validate argument on parameter 'To'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.

Best Answers

  • Options
    Simon_ZeinhoferSimon_Zeinhofer Customer Ninja IT Monkey ✭✭✭✭
    edited July 2023 Answer ✓

    Can you change the

    $ticket = Get-SCSMObject -Class (Get-SCSMClass -Name System.WorkItem.ServiceRequest$) -Filter "Id -eq $parentId"
    

    to

    $ticket = Get-SCSMObject -Id $parentId
    


    Then it should work.

    And please, use whitespaces! I guess most of your code fails because the script itself throws errors because of the syntax.

  • Options
    Ryan_KelleyRyan_Kelley Customer IT Monkey ✭
    Answer ✓

    This code was able to get the UPN - in case anyone needs the code:

    # Parameters

    param([guid]$parentId)


    # Import the Active Directory module

    if(-not(Get-Module ActiveDirectory))

    {

      Import-Module ActiveDirectory

      Write-Output "Active Directory module imported."

    }


    if(-not(Get-Module Smlets))

    {

      Import-Module SMLets

      Write-Output "SMLets module imported."

    }


    # Define the SMTP server, port and credential

    $smtpServer = "exchange4.shermfin.com"

    $smtpPort = 255


    # Define sender and recipient email addresses

    $from = from"

    $cc = "to"


    # Get the service request object

    $ticket = Get-SCSMObject -Id $parentId

    if ($null -eq $ticket) {

      Write-Output "Failed to retrieve the service request object with Id: $parentId"

      exit 1

    } else {

      Write-Output "Service request object retrieved: $($ticket.Id)"

    }


    # Get the Affected User

    $affectedUserRelation = Get-SCSMRelationshipClass -Name "affected" -ComputerName "server" | Select-Object -First 1

    $affectedUser = Get-SCSMRelatedObject -SMObject $ticket -Relationship $affectedUserRelation

    if ($null -eq $affectedUser) {

      Write-Output "Failed to retrieve the Affected User from ticket: $($ticket.Id)"

      exit 1

    } else {

      Write-Output "Affected User: $($affectedUser.UserName)"

    }



    # Get the Affected User's email from SCSM UPN

    $to = $affectedUser.UPN

    if ($null -eq $to) {

      Write-Output "Failed to retrieve the UPN for Affected User: $($affectedUser.UserName)"

      exit 1

    } else {

      Write-Output "Recipient email address (UPN): $to"

    }


    # Define the subject of the email

    $subject = "Security notification"


    # Define the body of the email

    $body = @"

    Type the email

    "@


    # Send the email

    try {

      Send-MailMessage -SmtpServer $smtpServer -Port $smtpPort -UseSsl -From $from -To $to -Cc $cc -Subject $subject -Body $body -ErrorAction Stop

      Write-Output "Email sent successfully."

    }

    catch {

      Write-Output "Error sending email: $_"

      exit 1

    }


    # Unloading the smlets and ActiveDirectory modules

    try {

      Remove-Module smlets -ErrorAction Stop

      Remove-Module ActiveDirectory -ErrorAction Stop

      Write-Output "Modules unloaded successfully."

    }

    catch {

      Write-Output "Error unloading modules: $_"

      exit 1

    }

Answers

  • Options
    Simon_ZeinhoferSimon_Zeinhofer Customer Ninja IT Monkey ✭✭✭✭
    edited July 2023 Answer ✓

    Can you change the

    $ticket = Get-SCSMObject -Class (Get-SCSMClass -Name System.WorkItem.ServiceRequest$) -Filter "Id -eq $parentId"
    

    to

    $ticket = Get-SCSMObject -Id $parentId
    


    Then it should work.

    And please, use whitespaces! I guess most of your code fails because the script itself throws errors because of the syntax.

  • Options
    Ryan_KelleyRyan_Kelley Customer IT Monkey ✭
    Answer ✓

    This code was able to get the UPN - in case anyone needs the code:

    # Parameters

    param([guid]$parentId)


    # Import the Active Directory module

    if(-not(Get-Module ActiveDirectory))

    {

      Import-Module ActiveDirectory

      Write-Output "Active Directory module imported."

    }


    if(-not(Get-Module Smlets))

    {

      Import-Module SMLets

      Write-Output "SMLets module imported."

    }


    # Define the SMTP server, port and credential

    $smtpServer = "exchange4.shermfin.com"

    $smtpPort = 255


    # Define sender and recipient email addresses

    $from = from"

    $cc = "to"


    # Get the service request object

    $ticket = Get-SCSMObject -Id $parentId

    if ($null -eq $ticket) {

      Write-Output "Failed to retrieve the service request object with Id: $parentId"

      exit 1

    } else {

      Write-Output "Service request object retrieved: $($ticket.Id)"

    }


    # Get the Affected User

    $affectedUserRelation = Get-SCSMRelationshipClass -Name "affected" -ComputerName "server" | Select-Object -First 1

    $affectedUser = Get-SCSMRelatedObject -SMObject $ticket -Relationship $affectedUserRelation

    if ($null -eq $affectedUser) {

      Write-Output "Failed to retrieve the Affected User from ticket: $($ticket.Id)"

      exit 1

    } else {

      Write-Output "Affected User: $($affectedUser.UserName)"

    }



    # Get the Affected User's email from SCSM UPN

    $to = $affectedUser.UPN

    if ($null -eq $to) {

      Write-Output "Failed to retrieve the UPN for Affected User: $($affectedUser.UserName)"

      exit 1

    } else {

      Write-Output "Recipient email address (UPN): $to"

    }


    # Define the subject of the email

    $subject = "Security notification"


    # Define the body of the email

    $body = @"

    Type the email

    "@


    # Send the email

    try {

      Send-MailMessage -SmtpServer $smtpServer -Port $smtpPort -UseSsl -From $from -To $to -Cc $cc -Subject $subject -Body $body -ErrorAction Stop

      Write-Output "Email sent successfully."

    }

    catch {

      Write-Output "Error sending email: $_"

      exit 1

    }


    # Unloading the smlets and ActiveDirectory modules

    try {

      Remove-Module smlets -ErrorAction Stop

      Remove-Module ActiveDirectory -ErrorAction Stop

      Write-Output "Modules unloaded successfully."

    }

    catch {

      Write-Output "Error unloading modules: $_"

      exit 1

    }

Sign In or Register to comment.