Start-Job including custom cmdlet terminates with strange error

此生再无相见时 提交于 2019-12-20 02:23:06

问题


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

回答1:


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
}


来源:https://stackoverflow.com/questions/33936510/start-job-including-custom-cmdlet-terminates-with-strange-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!