Friday, October 18, 2013

Orchestrator: Get Email Address of a SCSM user CI

When working with orchestrator there might come a day when you need the Email address of a SCSM user Object. This could be if you are going to used the Send Mail activity in a runbook.

When opening up a CI object in Service Manager we can see under the Notification tab that there is defined a SMTP address to the CI object. This is the address that SCSM will use when sending notifications in workflows and so on..

 This is because this is a combination object to the user object. So to find the email address we need to do some powershell magic in the runbook to find the email address.

To find the email address in powershell, I will be using the SMLETS module extension that includes  cmlets for managing Service Manager through PowerShell.

You can find this module on Codeplex:
http://smlets.codeplex.com/

Install the smlests on the Service Manager server.

The reason for this is that we do not want to have the Powershell module to be loaded on the Runbook server. So in order to use powershell against SCSM, we will use remote powershell.

The Runbook server will start a powershell session on the SCSM server by using remote powershell.

By default as you probably know, running remote powershell on a server is a no go. To do this you need to open up for remote management on servers. 

1.Set-ExecutionPolicy RemoteSigned (depending on your security settings in the environment)

2.If Windows Remote Management (WinRM) is not installed and configured, WinRM scripts do not run and    the Winrm command-line tool cannot perform data operations.


Command: Winrm quickconfig
http://msdn.microsoft.com/en-us/library/aa384372(v=vs.85).aspx
This can be performed in several ways based on your security policy.

Now when this is done, open a new runbook and select the "Run .Net Script" activity and add the following script:

$session = New-PSSession -ComputerName OSLWVSSM001L
    Invoke-Command -Session $session -ScriptBlock {
        
        Import-Module smlets
        $Userdisplayname = "DISPLAYNAME OF THE USER "
        $userpreferenceclass = Get-SCSMRelationshipClass -name system.userhaspreference 
        $class2 = get-scsmclass -name system.user$ 
        $user = Get-SCSMObject -class $class2 -filter ”Displayname -eq $Userdisplayname”
        $mail = (Get-scsmrelatedobject –smobject $user –relationship $userpreferenceclass | where{$_.displayname –match “smtp”}).targetaddress
             
    }
    $mail = Invoke-Command -Session $session -ScriptBlock { $mail }

Remove-PSSession $Session

The "DISPLAYNAME  OF THE USER" needs to be gathered from a source. This could be a SCSM Get-Object activity, where the name is published to the .Net Activity.

The Displayname needs to match the display name of a CI user in SCSM, if not the script will fail in the lookup.


Then we need to publish the result to the next activity.

In this example the variable "mail" in the PS will be mapped to the Published Data called. "Email Address".


The Email address will now be available to use in a Send Mail activity.

Just to point it out! Editing directly in the ".Net activity" is not a preferred way of working with powershell in Orchestrator. The editor do not support CTRL -Z and gives you more trouble that good functionality. So working with the command in "Notepadd++ , "Powershell ISE" or another tool is a better approach.