Archive for November, 2010

When I got home from Berlin (TechEd Europe) last week and went through my email backlog. I was glad to find that I’ve been awarded with the MCC title.

Microsoft Community Contributor Award
Microsoft Community Award FAQ

It’s nice to be appreciated, time to start writing the next blog post!

Categories : Other
Comments (2)
Nov
18

Affected User SMTP Address

Posted by: | Comments (6)

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!