【问题标题】:Return Select-String results with context from Invoke-Command (appear blank)从 Invoke-Command 返回带有上下文的 Select-String 结果(显示为空白)
【发布时间】:2017-07-21 19:29:40
【问题描述】:

我正在尝试使用 Invoke-Command 在多个服务器上搜索日志文件并返回带有上下文的结果,但我没有成功。

这是命令:

Invoke-Command -Session $session {
  cd C:\LogDir\Log1
  sls -Pattern "ThingImLookingFor" -Path * -Context 1,5 
}

这将返回一个已删除大部分信息的反序列化 MatchInfo 对象。

如何获得类似于在本地运行 Select-String 的结果?

这是在我的主目录中使用与示例相同的上下文设置在 svg 上运行 sls 的结果:

  horizontalBlock.svg:30:     id="base"
> horizontalBlock.svg:31:     pagecolor="#ffffff"
  horizontalBlock.svg:32:     bordercolor="#666666"
  horizontalBlock.svg:33:     borderopacity="1.0"
  horizontalBlock.svg:34:     inkscape:pageopacity="0.0"
  horizontalBlock.svg:35:     inkscape:pageshadow="2"
  horizontalBlock.svg:36:     inkscape:zoom="1.3289991" 

【问题讨论】:

  • 管道 slsOut-String?
  • 我知道我错过了一些简单的东西。介意让它成为一个答案吗?

标签: powershell powershell-remoting select-string


【解决方案1】:

管道slsOut-String

Invoke-Command -Session $session {
  cd C:\LogDir\Log1
  sls -Pattern "ThingImLookingFor" -Path * -Context 1,5 | Out-String
}

【讨论】:

    【解决方案2】:

    由于某种原因,返回的 psobject 中的自定义格式显示为空白。这是另一种解决方法。

    Invoke-Command -Session $session {
      cd C:\LogDir\Log1
      select-string -Pattern "ThingImLookingFor" -Path * -Context 1,5 | select line
    }
    

    或者

    Invoke-Command -Session $session {
      cd C:\LogDir\Log1
      select-string -Pattern "ThingImLookingFor" -Path * -Context 1,5 
    } | select line, pscomputername
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-05-19
      • 2021-09-28
      • 1970-01-01
      • 1970-01-01
      • 2015-08-23
      • 1970-01-01
      相关资源
      最近更新 更多