Nov
18

Affected User SMTP Address

By

Last week I got a question on how to get the smtp address of the affected user in an incident using PowerShell. The scenario where the question surfaced was when a friend was trying to execute a PowerShell script that would send an email message to the affected user of an incident in a custom console task (targeting the Incident class).

After some investigation I came up with the following PowerShell script which should do the trick.

?View Code POWERSHELL
# Author: Patrik Sundqvist
# Author blog: http://blogs.litware.se
# Requires: SCSM PowerShell Cmdlets (Beta 1 or later)
 
#Pass the id of the incident you want to get the affected user smtp address for as command line argument to the script
#Example: "GetAffectedUserSMTP.ps1 IR58"
param($incidentId)
 
Import-module smlets -Force
 
$affectedUserRelClass = Get-SCSMRelationshipClass System.WorkItemAffectedUser$
$userPref = Get-SCSMRelationshipClass System.UserHasPreference$
 
#Update done 2011-04-05 based on blog post comment from German Minicucci, thanks for the feedback!
#Locate the incident you want to find the affected user smtp address for
$incident = Get-SCSMIncident -ID $incidentId |%{$_.Object}
 
#Make sure there was an incident located with the given id
if($incident -eq $null)
{
    Write-Host 'No incident exists with given id'
}
else
{
    #Get the affected user in the located incident
    $affectedUser = Get-SCSMRelatedObject -SMObject $incident -Relationship $AffectedUserRelClass
 
    #Make sure there is an affected user specified in the incident, else abort
    if($affectedUser -eq $null)
    {
        Write-Host 'No affected user is given for the incident'$incident.DisplayName
    }
    else
    {
        #Get the endpoint related to the affected user that specifies the smtp address
        $endPoint = Get-SCSMRelatedObject -SMObject $affectedUser -Relationship $userPref|?{$_.DisplayName -like '*SMTP'}
        if($endPoint -eq $null)
        {
            Write-Host 'There is no smtp channel declared for the affected user'$affectedUser.DisplayName
        }
        else
        {
        if($endPoint.TargetAddress.length -gt 0)
            {
                #Output the located smtp address
                Write-Host $endPoint.TargetAddress
            }
            else
            {
                #No valid smtp address located for the user in the CMDB
            	Write-Host 'No valid smtp address found for user'$affectedUser.DisplayName
            }
        }
    }
}

The result can be seen in the picture!

Enjoy!

Comments

  1. Great post! I know somebody that will be happy to see this!

    FYI – there can be multiple email addresses for each user so you might want to adjust your script to show all, pick specifically the first, or something like that.

  2. Philip Colmer says:

    I thought that this might solve the problem I’m having with the “Email affected user” task in that I could modify that PowerShell script that the task calls so that it just needs to be given the incident ID and the script pulls everything out.

    However, it looks like SMLets has to be installed on a Service Manager *server* rather than a workstation that just has the console installed. The SDK Binaries don’t get installed when you install the console.

    Unfortunately, if that is the case, it seems to rather reduce the usefulness of SMLets and this script, if everything has to run on the server(s), which tasks don’t do?

  3. German Minicucci says:

    Works great!

    Adding my 2-cents:
    I found that using this piece of code is taking too long (the 2nd line):
    $incidentClass = Get-SCSMClass System.WorkItem.Incident$
    $incident = Get-SCSMObject $incidentClass|?{$_.Id -eq $incidentId}

    So, replacing those 2 lines with this one made it work much faster:
    $incident = Get-SCSMIncident -ID $incidentId |%{$_.Object}

    Thanks, Patrik!

  4. Patrik Sundqvist says:

    Thanks German Minicussi for the feedback!
    I’ve updated the script based on your input.

  5. Zel says:

    Hi,

    I am new in developing SCSM. Anybody here who can translate the powershell in C# code.

    Thanks for this post!

    Regards,
    Zel

Leave a Reply