The pipeline has been stopped 1


在PowerShell 3.0 中执行SCOM 中的Get-SCOMManagementPack 命令时,遇到异常The pipeline has been stopped.

PS C:\Users\asttest> Get-SCOMManagementPack |  select -First 1

Sealed   Name                                 DisplayName
------   ----                                 -----------
True     Microsoft.SystemCenter.ApplicationMo 360 Application Monitoring
         nitoring.360.Template.Dashboards     Dashboards
Get-SCOMManagementPack : The pipeline has been stopped.
At line:1 char:1
+ Get-SCOMManagementPack |  select -First 1
+ ~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (Microsoft.Syste...mentPackCommand:GetSCManagementPackCommand) [Get-SCMana
   gementPack], PipelineStoppedException
    + FullyQualifiedErrorId : ExecutionError,Microsoft.SystemCenter.Core.Commands.GetSCManagementPackCommand

引发该异常的根本原因是PowerShell 3.0 中的Select-Object 命令优化引起的,为了提高效率如果管道中流过的对象到达Select 命令指定的个数时,管道就会终止。

这里给出三种解决方案:

  1. 使用Select-Object 中的-Wait参数
    Get-SCOMManagementPack | select -First 1 -Wait
    
  2. 给Select的上一条命令加括号
    (Get-SCOMManagementPack) | select -First 1 
  3. 给Select 的上一条命令指定ErrorAction
    Get-SCOMManagementPack -ErrorAction SilentlyContinue | select -First 1

前两个原理一样都是将管道从流模式转换成顺序模式,推荐使用第三种方案,因为它保留了Select-Object命令的优化逻辑,效率高。

本文链接: https://www.pstips.net/the-pipeline-has-been-stopped.html
请尊重原作者和编辑的辛勤劳动,欢迎转载,并注明出处!

关于 Mooser Lee

我是一个Powershell的爱好者,创建了PowerShell中文博客,热衷于Powershell技术的搜集和分享。本站部分内容来源于互联网,不足之处敬请谅解,并欢迎您批评指正。

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注

一条评论 “The pipeline has been stopped