Skip to content

Tip: How to debug your PowerShell script in SCSM workflow

Tip: How to debug your PowerShell script in SCSM workflow published on 4 Comments on Tip: How to debug your PowerShell script in SCSM workflow

How often are you faced with problem when your PowerShell script working fine when running under your account but doesn’t work in SCSM workflow? It can be dozens reasons why it’s doesn’t work: permission issue, workflow account’s settings is wrong and so on. Even debugging the native workflow (WPF created with Visual Studio) is not easy. But what about PowerShell?

For debugging the PowerShell script you can use two features of the PowerShell: try-catch block and data conversion. First of all you must wrap your script with try-catch block:

    import-module SMLets
    $cl = Get-SCSMClass -Name "System.WorkItem.Icident$"
    #...... and so on

After this modification any exceptions raised inside of the PowerShell script will be handled and your can use exception object inside of the catch{} block”:


But how this can help us to debug the workflow if it still output to some Space? The next step is data conversion. We can convert any object in PowerShell to XML and then save this xml to file. To do that we must use ConvertTo-XML cmdlet:

    import-module SMLets
    $cl = Get-SCSMClass -Name "System.WorkItem.Incodent$"
    Get-SCSMObject -Class $cl
    # save variable to file
    ($_ | ConvertTo-XML).Save("d:\Test\exception.xml")

as result exception will be saved to XML:

<?xml version="1.0"?>
  <Object Type="System.Management.Automation.ErrorRecord">
    <Property Name="PSMessageDetails" Type="System.Object" />
    <Property Name="Exception" Type="System.Management.Automation.ParameterBindingValidationException">System.Management.Automation.ParameterBindingValidationException: Cannot bind argument to parameter 'Class' because it is null.
   at System.Management.Automation.ExceptionHandlingOps.CheckActionPreference(FunctionContext funcContext, Exception exception)
   at System.Management.Automation.Interpreter.ActionCallInstruction`2.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)</Property>
    <Property Name="TargetObject" Type="System.Object" />
    <Property Name="CategoryInfo" Type="System.Management.Automation.ErrorCategoryInfo">InvalidData: (:) [Get-SCSMObject], ParameterBindingValidationException</Property>
    <Property Name="FullyQualifiedErrorId" Type="System.String">ParameterArgumentValidationErrorNullNotAllowed,SMLets.GetSMObjectCommand</Property>
    <Property Name="ErrorDetails" Type="System.Management.Automation.ErrorDetails" />
    <Property Name="InvocationInfo" Type="System.Management.Automation.InvocationInfo">System.Management.Automation.InvocationInfo</Property>
    <Property Name="ScriptStackTrace" Type="System.String">at &lt;ScriptBlock&gt;, &lt;No file&gt;: line 5</Property>
    <Property Name="PipelineIterationInfo" Type="System.Collections.ObjectModel.ReadOnlyCollection`1[System.Int32]" />

You can use ConvertTo-XML with any objects. For example, you can check the value for given variable.

NOTE: Check permission to folder where you want to save the XML file. The good decision is set write permission for Users group.



Leave a Reply

Primary Sidebar

%d bloggers like this: