When using PowerShell in Run.net script activity, the error message can be very hard to read, since it only outputs the error message and not the additional information about the error.

To make this better, you can use the following method:

  • Set your error action preference to stop.
$ErrorActionPreference = "Stop"

I always do this in orchestrator to make sure the activity stops on all errors (except the ones i choose).

  • Insert a trap in top of your script
#Detailed error message
   throw  ($_ | format-list * -force | out-string)

Or you can choose to use try/catch instead

#Detailed error message
    #run your code here
   throw  ($_ | format-list * -force | out-string)

This will make your error output similar to this:

PSMessageDetails      : 
Exception             : System.Management.Automation.ItemNotFoundException: Cannot find path 'C:\Users\JGS\test' because it does not exist.
                           at System.Management.Automation.LocationGlobber.ExpandMshGlobPath(String path, Boolean allowNonexistingPaths, PSDriveInfo drive, Containe
                        rCmdletProvider provider, CmdletProviderContext context)
                           at System.Management.Automation.LocationGlobber.ResolveDriveQualifiedPath(String path, CmdletProviderContext context, Boolean allowNonexi
                        stingPaths, CmdletProvider& providerInstance)
                           at System.Management.Automation.LocationGlobber.GetGlobbedMonadPathsFromMonadPath(String path, Boolean allowNonexistingPaths, CmdletProvi
                        derContext context, CmdletProvider& providerInstance)
                           at Microsoft.PowerShell.Commands.ContentCommandBase.ResolvePaths(String[] pathsToResolve, Boolean allowNonexistingPaths, Boolean allowEmp
                        tyResult, CmdletProviderContext currentCommandContext)
TargetObject          : C:\Users\JGS\test
CategoryInfo          : ObjectNotFound: (C:\Users\JGS\test:String) [Get-Content], ItemNotFoundException
FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetContentCommand
ErrorDetails          : 
InvocationInfo        : System.Management.Automation.InvocationInfo
ScriptStackTrace      : at <ScriptBlock>, E:\Data\System Center\SCO\Scripts\Error-MEssage.ps1: line 10
PipelineIterationInfo : {}

which contains all information about the error!