包含自定义 cmdlet 的 Start-Job 以奇怪的错误终止 [英] Start-Job including custom cmdlet terminates with strange error
问题描述
我开发了一些自定义 cmdlet,用于向 SharePoint 系统执行不同的导入任务.目前,所有这些 cmdlet 都在单个 PowerShell 脚本中以串行方式运行.我想更改此设置,以便每个 cmdlet 在单独的任务(作业)中执行.
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).
主脚本开始一个新作业,其中 Start-Job
与包含对 cmdlet 调用的单独脚本相关.该脚本启动并执行 cmdlet.我还调试了执行的 cmdlet 的代码.到目前为止一切顺利.
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.
但在大约 15-20 秒后,作业将终止并显示以下错误消息:
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.
我是否必须向我的自定义 cmdlet 添加更多功能,以便在作业中处理它们?
Do I have to add further functionalities to my custom cmdlets so they can be handled in a job?
这是脚本.
主要内容:
[object]$credentials = Get-Credential -UserName "domain\user" -Message "Log in"
$job = start-job -FilePath "C:\ImportItems.ps1" -Name ImportItems -ArgumentList $credentials
$job | Wait-Job
导入项目:
[CmdletBinding()]
Param(
[object]$credentials
)
Import-Module C:\Migration\MigrationShell.dll
Import-Items -Credential $credentials
推荐答案
我在 uservoice 上找到了一个解决方法,对我有用
I found a workaround on uservoice, did the trick for me
if (
[Console]::InputEncoding -is [Text.UTF8Encoding] -and
[Console]::InputEncoding.GetPreamble().Length -ne 0
) {
[Console]::InputEncoding = New-Object Text.UTF8Encoding $false
}
这篇关于包含自定义 cmdlet 的 Start-Job 以奇怪的错误终止的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!