Powershell中的Truncat日志消息

时间:2019-06-28 12:14:48

标签: powershell events

我尝试使用Powershell截断wifi日志的第一行,但我不明白为什么它不起作用。谁能建议以下代码有什么问题?

(Get-WinEvent -Logname Microsoft-Windows-WLAN-AutoConfig/Operational | select -first 1).Message.split('`n')[0]

2 个答案:

答案 0 :(得分:1)

尝试:

(Get-WinEvent -Logname Microsoft-Windows-WLAN-AutoConfig/Operational | select -first 1).Message.split("`n")[0]

单引号的字符串被视为文字,我想您是在字母'n'上分开的:)

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_quoting_rules?view=powershell-6

答案 1 :(得分:1)

我认为您最好使用正则表达式-split将消息拆分为换行符:

((Get-WinEvent -Logname Microsoft-Windows-WLAN-AutoConfig/Operational | Select-Object -First 1).Message -split '\r?\n')[0]
\r       Match a carriage return character
   ?     Between zero and one times, as many times as possible, giving back as needed (greedy)
\n       Match a line feed character