locked
Powershell script not working in a workflow. RRS feed

  • Question

  • I am using a workflow with a powershell script to grab the affected user email address and update it to an extended data field on the incident form.  This is for an 3rd party survey company.  With Incidents everything works fine.  I was asked to have the surveys sent out for service requests as well.  I extended the Service request form for the additional field and created a new workflow for service requests.  The Powershell script will work from a powershell command but does not seem to work with the workflow.  The workflow status says succeeded but the field that I need does not get updated.  Is there any special trick to getting this to work? Below is the Incident script that works and then the Service Request one which does not.

    Incidents

    Import-module smlets -Force
    
    $affectedUserRelClass = Get-SCSMRelationshipClass System.WorkItemAffectedUser$
    
    $userPref = Get-SCSMRelationshipClass System.UserHasPreference$
    
    $incident = Get-SCSMIncident -ID $incidentId |%{$_.Object}
    
    $affectedUser = Get-SCSMRelatedObject -SMObject $incident -Relationship $AffectedUserRelClass
    
        if($affectedUser -eq $null)
        {
            remove-module SMLets -Force
        }
        else
        {
            $endPoint = Get-SCSMRelatedObject -SMObject $affectedUser -Relationship $userPref|?{$_.DisplayName -like '*SMTP'}
        }
            
            if($endPoint -eq $null)
            {
                remove-module SMLets -Force
            }
            else
            {
               if($endPoint.TargetAddress.length -gt 0)
                    {
                        get-scsmobject -class (get-scsmclass -name system.workitem.incident$) -Filter "Id -eq $incidentId" | Set-SCSMObject -Property CustomerSurveyEmail -Value $endPoint.TargetAddress
                        
                         remove-module SMLets -Force
                    }
                    else
                    {
                        remove-module SMLets -Force
                    }
          }

    Service Requests

    Import-module smlets -Force
    
    $affectedUserRelClass = Get-SCSMRelationshipClass System.WorkItemAffectedUser$
    
    $userPref = Get-SCSMRelationshipClass System.UserHasPreference$
    
    $servicerq = Get-SCSMObject -Class (Get-SCSMClass -Name System.WorkItem.ServiceRequest$) -Filter "Id -eq $workid"
    
    $affectedUser = Get-SCSMRelatedObject -SMObject $servicerq -Relationship $AffectedUserRelClass
    
        if($affectedUser -eq $null)
        {
            remove-module SMLets -Force
        }
        else
        {
            $endPoint = Get-SCSMRelatedObject -SMObject $affectedUser -Relationship $userPref|?{$_.DisplayName -like '*SMTP'}
        }
            
            if($endPoint -eq $null)
            {
                remove-module SMLets -Force
            }
            else
            {
               if($endPoint.TargetAddress.length -gt 0)
                    {
                        $servicerq | Set-SCSMObject -Property CustomerSurveyEmail -Value $endPoint.TargetAddress
                        
                         remove-module SMLets -Force
                    }
                    else
                    {
                        remove-module SMLets -Force
                    }
            }

    Wednesday, January 9, 2013 12:46 AM

Answers

  • After doing some further investigation it appears that the cause of this problem was due to me creating this workflow with the Service Manager Authoring Tool   7.5.1561.0 on Windows 8.  While the Authoring tool installs and runs just fine and all tasks seem to complete correctly, workflows and or DLL's generated with the Authoring tool do not work correctly in Service Manager.  I verified this by creating a workflow that just had a parameter workid specified to use the service request ID.  The only thing the Workflow was configured to do was run this command:

    ($workid | ConvertTo-XML).Save("c:\SCSM_workid.xml")

    The Windows 8\Authoring tool combination always returned

    <?xml version="1.0"?>
    -<Objects> <Object Type="System.String"/> </Objects>

    so nothing was being passed.  I tried this for incident ant service request workflows.  When I created a workflow with Windows 7 and the same Authoring tool everything worked as expected.  Thank you Anton for your suggestions on logging the workflow.  It proved very valuable in troubleshooting this problem.

    • Marked as answer by Misha Rudiy Wednesday, January 23, 2013 4:07 PM
    Wednesday, January 23, 2013 4:07 PM

All replies

  • I have run into a similar issue before with a workflow I created.

    It turned out to be an issue with " or '

    when I copied and pasted the script, it is like the " or ' got changed.

    I searched through my script using notepad and just back-spaced and re-added " or ' depending on what was needed.

    Hope this makes sense.

    F.


    As long as you learn something new every day, the day is not wasted.

    Thursday, January 10, 2013 9:39 AM
  • The script seems fine.  Is there some way to tell the script to output any errors to a text file?  Like write-out or -ErrorVariable?  I am not sure the correct syntax to use for them.
    Friday, January 11, 2013 10:43 PM
  • It is too bad We can't just use this in the email template to get the target address for the affected user.

    $Context/Path[Relationship='CustomSystem_SupportingItem_Library!System.UserHasPreference' TypeConstraint='CustomSystem_Notifications_Library!System.Notification.Endpoint']/Property[Type='CustomSystem_Notifications_Library!System.Notification.Endpoint']/TargetAddress$

    Saturday, January 12, 2013 12:12 AM
  • To debug your script in workflow you can use this snippet:

    try
    {
         # all your script below
    
         # end of your script
    }
    catch
    {
         ($_.Exception | ConvertTo-XML).Save("c:\SCSM_Error.xml")
    }


    SCSMSolutions
    email: freemanru (at) gmail (dot) com

    Tuesday, January 15, 2013 1:46 AM
  • Using the code to catch the error I am getting:

    <?xml version="1.0"?>
    <Objects>
      <Object Type="System.Management.Automation.ParameterBindingValidationException">
        <Property Name="Message" Type="System.String">Cannot bind argument to parameter 'SMObject' because it is null.</Property>
        <Property Name="ParameterName" Type="System.String">SMObject</Property>
        <Property Name="ParameterType" Type="System.RuntimeType">Microsoft.EnterpriseManagement.Common.EnterpriseManagementObject</Property>
        <Property Name="TypeSpecified" Type="System.Type" />
        <Property Name="Line" Type="System.Int64">14</Property>
        <Property Name="Offset" Type="System.Int64">48</Property>
        <Property Name="CommandInvocation" Type="System.Management.Automation.InvocationInfo">System.Management.Automation.InvocationInfo</Property>
        <Property Name="ErrorRecord" Type="System.Management.Automation.ErrorRecord">Cannot bind argument to parameter 'SMObject' because it is null.</Property>
        <Property Name="StackTrace" Type="System.String">   at System.Management.Automation.ParameterBinderBase.ValidateNullOrEmptyArgument(CommandParameterInternal parameter, CompiledCommandParameter parameterMetadata, Type argumentType, Object parameterValue, Boolean recurseIntoCollections)
       at System.Management.Automation.ParameterBinderBase.BindParameter(CommandParameterInternal parameter, CompiledCommandParameter parameterMetadata, ParameterBindingFlags flags)
       at System.Management.Automation.CmdletParameterBinderController.BindParameter(CommandParameterInternal argument, MergedCompiledCommandParameter parameter, ParameterBindingFlags flags)
       at System.Management.Automation.CmdletParameterBinderController.BindParameter(UInt32 parameterSets, CommandParameterInternal argument, MergedCompiledCommandParameter parameter, ParameterBindingFlags flags)
       at System.Management.Automation.CmdletParameterBinderController.BindParameters(UInt32 parameterSets, Collection`1 arguments, CommandMetadata commandMetadata)
       at System.Management.Automation.CmdletParameterBinderController.BindCommandLineParametersNoValidation(Collection`1 arguments)
       at System.Management.Automation.CmdletParameterBinderController.BindCommandLineParameters(Collection`1 arguments)
       at System.Management.Automation.CommandProcessor.BindCommandLineParameters(CommandParameterInternal[] parameters)
       at System.Management.Automation.CommandProcessor.Prepare(CommandParameterInternal[] parameters)
       at System.Management.Automation.CommandProcessorBase.DoPrepare(CommandParameterInternal[] parameters)
       at System.Management.Automation.Internal.PipelineProcessor.Start(Boolean incomingStream)
       at System.Management.Automation.Internal.PipelineProcessor.SynchronousExecuteEnumerate(Object input, Hashtable errorResults, Boolean enumerate)</Property>
        <Property Name="WasThrownFromThrowStatement" Type="System.Boolean">False</Property>
        <Property Name="Data" Type="System.Collections.ListDictionaryInternal" />
        <Property Name="InnerException" Type="System.Exception" />
        <Property Name="TargetSite" Type="System.Reflection.RuntimeMethodInfo">Void ExecuteStatement(System.Management.Automation.ParseTreeNode, System.Array, System.Management.Automation.Internal.Pipe, System.Collections.ArrayList ByRef, System.Management.Automation.ExecutionContext)</Property>
        <Property Name="HelpLink" Type="System.String" />
        <Property Name="Source" Type="System.String">System.Management.Automation</Property>
      </Object>
    </Objects>
    It looks like there is something wrong with line:
    $affectedUser = Get-SCSMRelatedObject -SMObject $servicerq -Relationship $AffectedUserRelClass
    
    of my workflow script.  It works from a powershell script.  The only thing that is different is that when I run it independently I specify a $workid = "SRxxx" variable.  When it is run in a workflow the workid parameter is provided by the workflow.

    Tuesday, January 22, 2013 5:21 PM
  • CAn be a case what you set the Internal Id (Guid) instead if regular ID in variable mapping?

    You can check the $workid value with same aproach:

    ($workid | ConvertTo-XML).Save("c:\SCSM_workid.xml")
    


    SCSMSolutions
    email: freemanru (at) gmail (dot) com

    Tuesday, January 22, 2013 11:17 PM
  • I appended my script to include

    ($workid | ConvertTo-XML).Save("c:\SCSM_workid.xml")

    and the scsm_workid.xml contains

    <?xml version="1.0"?>
    -<Objects> <Object Type="System.String"/> </Objects>
    I am positive that I set the variable mapping to ID and not ID (Internal) but it looks like it is not being pushed through as it is empty.
    Tuesday, January 22, 2013 11:56 PM
  • After doing some further investigation it appears that the cause of this problem was due to me creating this workflow with the Service Manager Authoring Tool   7.5.1561.0 on Windows 8.  While the Authoring tool installs and runs just fine and all tasks seem to complete correctly, workflows and or DLL's generated with the Authoring tool do not work correctly in Service Manager.  I verified this by creating a workflow that just had a parameter workid specified to use the service request ID.  The only thing the Workflow was configured to do was run this command:

    ($workid | ConvertTo-XML).Save("c:\SCSM_workid.xml")

    The Windows 8\Authoring tool combination always returned

    <?xml version="1.0"?>
    -<Objects> <Object Type="System.String"/> </Objects>

    so nothing was being passed.  I tried this for incident ant service request workflows.  When I created a workflow with Windows 7 and the same Authoring tool everything worked as expected.  Thank you Anton for your suggestions on logging the workflow.  It proved very valuable in troubleshooting this problem.

    • Marked as answer by Misha Rudiy Wednesday, January 23, 2013 4:07 PM
    Wednesday, January 23, 2013 4:07 PM