【问题标题】:Using Get-EventLog in PowerShell how can I show only 10 characters in the message在 PowerShell 中使用 Get-EventLog 如何在消息中仅显示 10 个字符
【发布时间】:2014-05-20 03:57:34
【问题描述】:

我目前有以下代码:

Get-EventLog -LogName Application 
| Where-Object EventID -EQ 1033 
| Select-Object EventID, Message

所以我的问题是如何只显示消息的前 10 个字符?

【问题讨论】:

    标签: powershell get-eventlog


    【解决方案1】:

    在你的消息属性上使用 substring 方法。

    Get-EventLog -LogName Application | Select-Object EventID, @{Label='Message';Expression={$_.Message.Substring(0,10)}}
    

    【讨论】:

      【解决方案2】:

      作为后续:

      Get-EventLog -LogName Application 
      | Where-Object EventID -EQ 1033 
      | Select-Object EventID, @{l="Message";e={$_.message.substring(0,10)}}
      

      【讨论】:

      • 那只会得到前 9 个字符 :) substring 的第一个参数是从哪个索引开始,而第二个参数是你想要多少个字符。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-09
      • 2017-01-01
      • 2011-07-08
      • 2020-11-14
      • 2016-02-07
      • 2014-04-06
      相关资源
      最近更新 更多