2

Using the remoting capabilities in Sitecore PowerShell Extensions, I'm trying to execute some PowerShell remotely and ensure that any errors that occur display in the local console. How can I do that?

Here's what I have:

Import-Module -Name SPE

$session = New-ScriptSession -Username "admin" -Password "b" -ConnectionUri "http://sitecore.local"
$jobId = Invoke-RemoteScript -Session $session -AsJob -ScriptBlock { 
    $templatesPath = "master:/sitecore/content/data/tempdata"

    if (Test-Path $templatesPath) {
        if ((Get-Item $templatesPath).HasChildren -eq "True") {
            $children = Get-ChildItem $templatesPath
            foreach ($childItem in $children) {
                "Deleting temp content at $($childItem.FullPath)"

                # I recognize this next line does not work
                # It's wrong on purpose to test what happens when an error occurs
                Remove-Item "$($childItem.FullPath)" -Force -Recurse
            }
        }
    }
}

Wait-RemoteScriptSession -Session $session -Id $jobId -Delay 1 -Verbose
Stop-ScriptSession -Session $session

If I check the logs after execution I see that an error occurred, but I don't see the error in the console where I executed the script.

1 Answer 1

4

Dan, a very good question. Bringing back error messages through the SPE Remoting is quite tricky because of the way PowerShell handles streams of data. Script output is handled one way, and errors (or informational messages) are handled in a different way.

Here are some possible ways for you to troubleshoot.

Example: Run script without the use of asynchronous jobs. If the script turns fairly quickly then it won't be an issue in your testing. Running as a scheduled task, however, may be more suitable for async jobs. The ErrorRecord object is returned in the output.

Import-Module -Name SPE

$session = New-ScriptSession -Username michael -Password b -ConnectionUri http://spe
Invoke-RemoteScript -Session $session -ScriptBlock { 
    $templatesPath = "master:/sitecore/content/pathdoesnotactuallyexist"
    Remove-Item "$($templatesPath)"-Force -ParameterDoesNotActuallyExist
}

Stop-ScriptSession -Session $session

Using the above example you will see this in the Windows PowerShell ISE (remotely).

ISE error message without async job

Now you may be thinking, "Well what if the job does take a while to run?"

Example: Run script asynchronously. A string version of the error is returned.

Import-Module -Name SPE

$session = New-ScriptSession -Username michael -Password b -ConnectionUri http://spe
$jobId = Invoke-RemoteScript -Session $session -ScriptBlock {
    try {
        $templatesPath = "master:/sitecore/content/pathdoesnotactuallyexist"
        Remove-Item "$($templatesPath)"-Force -ParameterDoesNotActuallyExist
    } catch {
        $_
    }
} -AsJob

Wait-RemoteScriptSession -Session $session -Id $jobId -Delay 1

Stop-ScriptSession -Session $session

Using the above example you will see this int he Windows PowerShell ISE (remotely).

ISE error message with async job

This currently looks a little less polished but still gets the job done.

Update

If you desire for your script to return an error message for commands that normally continue, add $ErrorActionPreference = "Stop" in your ScriptBlock.

Example: Run script asynchronously and stop on any error.

Import-Module -Name SPE

$session = New-ScriptSession -Username michael -Password b -ConnectionUri http://spe
$jobId = Invoke-RemoteScript -Session $session -ScriptBlock {
    $ErrorActionPreference = "Stop"
    try {
        $templatesPath = "master:/sitecore/content/pathdoesnotactuallyexist"
        Get-Item "$($templatesPath)"-Force
    } catch {
        $_
    }
} -AsJob

Wait-RemoteScriptSession -Session $session -Id $jobId -Delay 1

Stop-ScriptSession -Session $session

Error on Get-Item command

Update 2

Version 4.4.1+ should include support for Write-Verbose and other Write- commands.

You can read more about it here.

Streams

6
  • Unfortunately, the error that is thrown by Remove-Item if the path is not found does not get bubbled up to be caught when it's executed from a remoting session. When I run your code from your second example, I see the right result. However, if I remove the -ParameterDoesNotActuallyExist parameter and instead allow it to use an incorrect path, it fails silently. Are there multiple categories of errors that I need to handle differently? Commented Nov 29, 2016 at 13:59
  • The current version 4.2 really optimizes the authoring experience from within the Sitecore interface. I would encourage you to consider authoring more complex scripts there and then applying to the remoting script when ready. Commented Nov 29, 2016 at 14:01
  • Oops, I see your updated question now. The errors that are bubbled up include those that have syntax issues. If you ran for example Get-Item on a path that does not exist, the error doesn't stop the script from running. It's also more of a warning that the path doesn't exist. You could try using Test-Path in more places and Write-Output to return result messages back. Commented Nov 29, 2016 at 14:03
  • Sure, that makes sense. I guess my underlying question is more about making sure that all the errors get returned to the remote session, more than this specific case (obviously, I'll fix this one :) ). My concern is if some new error occurs, but in the remote session everything looks OK. I wouldn't know it had failed. Commented Nov 29, 2016 at 14:18
  • I completely agree and understand Dan. If I can come up with some additional examples to help out I'll update the answer. Feel free to share more barebones examples of what kind of message you need trapped and I'll see if it's possible. Commented Nov 29, 2016 at 14:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.