Start-Job including custom cmdlet terminates with strange error
Asked Answered
W

1

4

I developed some custom cmdlets that serve for different importing tasks to a SharePoint system. Currently all those cmdlets are being run in a serial kind in a single PowerShell script. I want to change this so that each cmdlet gets executed in a separate task (job).

The main script starts a new job with Start-Job relating to a separate script that contains the call to the cmdlet. The script starts and executes the cmdlet. I also debugged the code of the cmdlet that gets executed. So far so fine.

But after around 15-20 seconds the job just gets terminated with the following error message:

There is an error processing data from the background process. Error reported:
Cannot process an element with node type "Text". Only Element and EndElement
node types are supported..
    + CategoryInfo          : OperationStopped: (localhost:String) [], PSRemotingTransportException
    + FullyQualifiedErrorId : JobFailure
    + PSComputerName        : localhost

I can't find any information on how to handle such an error. I just don't know what is the problem here.

Do I have to add further functionalities to my custom cmdlets so they can be handled in a job?

Here are the scripts.

Main:

[object]$credentials = Get-Credential -UserName "domain\user" -Message "Log in"

$job = start-job -FilePath "C:\ImportItems.ps1" -Name ImportItems -ArgumentList $credentials
$job | Wait-Job

ImportItems:

[CmdletBinding()]
Param(
  [object]$credentials
)

Import-Module C:\Migration\MigrationShell.dll
Import-Items -Credential $credentials
Wagers answered 26/11, 2015 at 10:41 Comment(4)
That seems to be an error from Import-Items.Tricornered
I thought the same. When executing the cmdlet without a job everything works fine. When I debug the cmdlet while being executed through the job it works fine also. But like after 20 seconds it just terminates.Sideman
So how did you solve this? Using the answer below with [Console]::InputEncoding?Alleenallegation
Sorry I really can't remember how I handled this issue. You have the same problem and the below anwser helped you?Sideman
P
3

I found a workaround on uservoice, did the trick for me

https://windowsserver.uservoice.com/forums/301869-powershell/suggestions/14915283-job-cmdlets-fail-with-utf-8-codepage

if (
    [Console]::InputEncoding -is [Text.UTF8Encoding] -and
    [Console]::InputEncoding.GetPreamble().Length -ne 0
) {
    [Console]::InputEncoding = New-Object Text.UTF8Encoding $false
}
Pad answered 11/4, 2017 at 15:28 Comment(5)
More information about the problem: github.com/ansible/ansible/issues/15770#issuecomment-227898569 and why the workaround works: github.com/ansible/ansible/issues/15770#issuecomment-228225581Alleenallegation
@OhadSchneider: Note that the issue will be fixed in .NET Core 3.0 and therefore in whatever future PowerShell Core version that will be based on it: github.com/dotnet/corefx/issues/32004Ednaedny
@Ednaedny that seems to be Unix specific though? Not sure it would cover all cases, specifically on Windows: github.com/Microsoft/azure-pipelines-tasks/issues/9766Alleenallegation
@Ednaedny great stuff, thank you! As the OP, can you re-open the bug though? I fear that comments on closed bugs sometimes don't get much attention (I could be wrong though, probably depends on the repo)...Alleenallegation
Another good point, @OhadSchneider, thanks - I've deleted the comment about fixing the problem on Windows too and have created a new issue instead: github.com/dotnet/corefx/issues/35950Ednaedny

© 2022 - 2024 — McMap. All rights reserved.