尝试做1个衬板来执行以下操作(将脚本传递给Powershell的系统的限制)
尝试将事件日志转换为CSV字符串,而没有换行符以将其读取为另一种语言
示例代码:
(get-eventlog -LogName system -Newest 2 | select instanceid,index,entrytype,message | convertto-csv -NoTypeInformation).replace("`"`"","`",`"")
显示:
"InstanceId","Index","EntryType","Message" "2147489661","19981","Information","The system uptime is 961904 seconds." "10000","19980","Error","The description for Event ID '10000' in Source 'DCOM' cannot be found. The local computer may not have the necessary registry information or message DLL files to display the message, or you may not have permission to access them. The following information is part of the event:'C:\Windows\System32\coredpussvr.exe -Embedding', '0', '{417976B7-917D-4F1E-8F14-C18FCCB0B3A8}'"
(get-eventlog -LogName system -Newest 2 | select instanceid,index,entrytype,message | convertto-csv -NoTypeInformation).contains("`r")
显示False
(get-eventlog -LogName system -Newest 2 | select instanceid,index,entrytype,message | convertto-csv -NoTypeInformation).contains("`n")
显示False
if ((get-eventlog -LogName system -Newest 2 | select instanceid,index,entrytype,message | convertto-csv -NoTypeInformation) -like "*`n*"){write-host "win"}
if ((get-eventlog -LogName system -Newest 2 | select instanceid,index,entrytype,message | convertto-csv -NoTypeInformation) -like "*`r"){write-host "win"}
也没有运气
当我在CharArray
中查看结果时,在显示的行之间看不到任何字符
onds." "10000
这就是为什么我尝试了在,
之间插入""
的第一个示例,但是似乎有一些强迫它插入换行符的原因。
当我尝试在测试字符串中用示例代码1将""
替换为","
时,{p>
有什么想法吗?
答案 0 :(得分:0)
此:
get-eventlog -LogName system -Newest 2 | select instanceid,index,entrytype,message | convertto-csv -NoTypeInformation
...正在返回字符串数组。因此,如果将其通过管道传递到“ Get-Member”,它将检测到它的字符串,因此它将告诉您有关字符串的信息。如果要查看其字面意思,可以键入:
(get-eventlog -LogName system -Newest 2 | select instanceid,index,entrytype,message | convertto-csv -NoTypeInformation).GetType().FullName
如果您希望将其全部作为一个带有回车符和换行符的单个字符串,则可以执行以下操作:
(get-eventlog -LogName system -Newest 2 | select instanceid,index,entrytype,message | convertto-csv -NoTypeInformation) -join "`r`n"
如果要替换空白行,可以添加一个“ Where-Object
”(别名:?
)过滤器,以过滤出空行,如下所示:
get-eventlog -LogName system -Newest 2 | select instanceid,index,entrytype,message | convertto-csv -NoTypeInformation | ? {$_}
如果要对集合执行“获取成员”操作,则可以通过在逗号前面添加逗号来使集合成为集合,例如:
,(get-eventlog -LogName system -Newest 2 | select instanceid,index,entrytype,message | convertto-csv -NoTypeInformation) | gm
有帮助吗?
答案 1 :(得分:0)
由于ConvertTo-Csv输出字符串的集合,因此您可以使用join operator创建带有选择的分隔符的单个字符串:
(Get-EventLog -LogName system -Newest 2 | select instanceid,index,entrytype,message | ConvertTo-Csv -NoTypeInformation) -join ','