【问题标题】:Select only first row from output仅从输出中选择第一行
【发布时间】:2016-03-30 12:30:01
【问题描述】:

我如何才能在 powershell 中仅从该输出中选择第一行(名称)? Output

代码是这个:

1. Import-Module "C:\CMI\Entwicklung\MetaTool\packages\psake.4.5.0\tools\psake.psm1"
2. invoke-psake -buildFile "C:\CMI\Entwicklung\MetaTool\Build\default.ps1" -docs;

我只想拥有这个列表中的名字。

谢谢!

【问题讨论】:

    标签: powershell psake


    【解决方案1】:

    通过管道输出到选择对象:

    Import-Module "C:\CMI\Entwicklung\MetaTool\packages\psake.4.5.0\tools\psake.psm1"
    invoke-psake -buildFile "C:\CMI\Entwicklung\MetaTool\Build\default.ps1" -docs | select Name
    

    编辑

     $a = Invoke-psake default.ps1
    
     $a
     psake version 4.6.0
     Copyright (c) 2010-2014 James Kovacs & Contributors
    
     Executing Clean
     Executed Clean!
     Executing Compile
     Executed Compile!
     Executing Test
     Executed Test!
    
     Build Succeeded!
    
     ----------------------------------------------------------------------
     Build Time Report
     ----------------------------------------------------------------------
     Name    Duration        
     ----    --------        
     Clean   00:00:00.0193100
     Compile 00:00:00.0148280
     Test    00:00:00.0169533
     Total:  00:00:00.1112917
    
    
    $b=($a | select-string ":").count-1; ($a | Select-String ":")  -replace "\d{2}\:\d{2}:\d{2}.\d{7}"| select -First $b
    Clean   
    Compile 
    Test    
    

    【讨论】:

    • 将输出分配给 var 并获取 ist 类型:$a = invoke.....; $a.gettype() 这将让您了解是什么数据类型,以及我们是否可以使用 select 语句。
    • var 是一个 System.Array。以下是数组中每个值的类型: 1 + 2 = Microsoft.PowerShell.Commands.Internal.Format.StartData 3 - 9 = Microsoft.PowerShell.Commands.Internal.Format.PacketInfoData
    • 有没有一种简单的方法可以让这个模块在自己的环境中运行,而无需设置一些构建过程?如果有一个数组,包含“名称”,你应该使用 $Array | 访问它。选择名称。可能,他们正在使用 write-Output 语句,但这在功能上并不好。
    • 我会在 wiki 中再次查看...我尝试了很多(选择、拆分、格式化等)但似乎没有任何效果..
    • 您还可以查看函数,它们返回的类型等。但我认为,它是一个更大的函数,可以调用其他函数等。
    猜你喜欢
    • 2010-12-21
    • 2021-04-22
    • 2021-03-13
    • 1970-01-01
    • 2018-11-28
    • 1970-01-01
    • 1970-01-01
    • 2015-01-15
    • 2019-02-01
    相关资源
    最近更新 更多