重建功能在Windows 7 PowerShell下工作

时间:2018-03-26 11:20:46

标签: powershell

function find_sn {
    $line_sn = $args[0]  | Select-String -Pattern "55518" -CaseSensitive
    $line_sn.line.split(' ').Where({$_.Trim() -ne ''})[1]
}

在Windows 10下工作得很好,但在Windows 7(powershell ver.5.1)中它说:

System.Management.Automation.RuntimeException: Method invocation failed because [System.String[]] doesn't contain a method named 'Where'.
   in System.Management.Automation.Runspaces.PipelineBase.Invoke(IEnumerable input)
   in System.Management.Automation.Runspaces.Pipeline.Invoke()
   in Ktr.Agent.Service.Scripts.PowerShellScriptsRunner.ExecutePipeline(Pipeline pipeline)

这是要分割的行(删除nr 55518并仅在输出中写入“CA015 ..”:

55518    CAO1501371925C3

任何人都可以帮我吗?

编辑: 这是在win7上安装的powershell:

Name                           Value
----                           -----
PSVersion                      5.1.14409.1005
PSEdition                      Desktop
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
BuildVersion                   10.0.14409.1005
CLRVersion                     4.0.30319.42000
WSManStackVersion              3.0
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1

1 个答案:

答案 0 :(得分:3)

使用Where-Object cmdlet而不是Where()方法:

@($line_sn.line.split(' ') |Where-Object {"$_".Trim() -ne ''})[1]