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:
try { import-module SMLets $cl = Get-SCSMClass -Name "System.WorkItem.Icident$" #...... and so on } catch {}
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:
try { import-module SMLets $cl = Get-SCSMClass -Name "System.WorkItem.Incodent$" Get-SCSMObject -Class $cl } catch { # save variable to file ($_ | ConvertTo-XML).Save("d:\Test\exception.xml") }
as result exception will be saved to XML:
<?xml version="1.0"?> <Objects> <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 <ScriptBlock>, <No file>: line 5</Property> <Property Name="PipelineIterationInfo" Type="System.Collections.ObjectModel.ReadOnlyCollection`1[System.Int32]" /> </Object> </Objects>
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.
4 Comments
How to debug your Powershell script in an SCSM workflow – good tips! http://t.co/HM2AGxMkFN #SCSM @scsmru @ConcurrencyInc
How to debug your Powershell script in an SCSM workflow – good tips! http://t.co/NsPOWNGJGM #SCSM @scsmru – ^JH
RT @scsmru: New blog post: Tip: How to debug your #PowerShell script in #SCSM workflow http://t.co/LabQIjEQUg
RT @scsmru: New blog post: Tip: How to debug your #PowerShell script in #SCSM workflow http://t.co/YM88VyPfnI #SysCtr