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
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328

1 Answers1

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
}
mizuki nakeshu
  • 1,315
  • 2
  • 9
  • 12
  • More information about the problem: https://github.com/ansible/ansible/issues/15770#issuecomment-227898569 and why the workaround works: https://github.com/ansible/ansible/issues/15770#issuecomment-228225581 – Ohad Schneider Mar 07 '19 at 15:37
  • @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: https://github.com/dotnet/corefx/issues/32004 – mklement0 Mar 10 '19 at 13:17
  • 1
    @mklement0 that seems to be Unix specific though? Not sure it would cover all cases, specifically on Windows: https://github.com/Microsoft/azure-pipelines-tasks/issues/9766 – Ohad Schneider Mar 10 '19 at 17:08
  • 1
    @mklement0 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)... – Ohad Schneider Mar 11 '19 at 09:54
  • Another good point, @OhadSchneider, thanks - I've deleted the comment about fixing the problem on Windows too and have created a new issue instead: https://github.com/dotnet/corefx/issues/35950 – mklement0 Mar 11 '19 at 14:20