Powershell 从共享位置提取日志文件中的内容,并将表格格式的电子邮件以及新标题发送到电子邮件
我正在尝试使用PowerShell自动化一些日常健康检查任务 我希望(一步一步地)实现以下目标,尽管我在少数几个方面取得了部分成功Powershell 从共享位置提取日志文件中的内容,并将表格格式的电子邮件以及新标题发送到电子邮件,powershell,powershell-3.0,Powershell,Powershell 3.0,我正在尝试使用PowerShell自动化一些日常健康检查任务 我希望(一步一步地)实现以下目标,尽管我在少数几个方面取得了部分成功 通过定义$Path=Set location…等,提取位于共享位置(我已成功)的文本(日志)文件的内容 通过定义将电子邮件(成功)发送到邮箱 我需要的真正帮助就在这里 我想在电子邮件中添加标题,以及从步骤1中提取的原始文本 例如 原始文本如下所示(从共享位置的文本文件中提取): 01-01-2018成功对象数量-1 我想在电子邮件中添加此标题,如 date
$Path=Set location…
等,提取位于共享位置(我已成功)的文本(日志)文件的内容假设步骤1中收集的日志文件的内容是日志条目的字符串数组,其中每个字符串的格式类似于
'01-01-2018成功对象数-1'
在本例中,我将该数组称为$logEntries
# create an array to store the results objects in
$result = @()
# loop through this array of log entries
$logEntries | ForEach-Object {
# Here every text line is represented by the special variable $_
# From your question I gather they all have this format:
# '01-01-2018 Number of Successful object - 1 '
if ($_ -match '(?<date>\d{2}-\d{2}-\d{4})\s+(?<description>[^\-\d]+)[\s\-]+(?<number>\d+)\s*$') {
# Try to get the 'Succesful' or 'Failed' (??) text part out of the description
$description = ($matches['description'] -replace 'Number of|object[s]?', '').Trim() + ' object'
if ([int]$matches['number'] -ne 1) { $description += 's' }
# add to the result array
$result += New-Object -TypeName PSObject -Property ([ordered]@{
'Date' = $matches['date']
'Description' = $description
'Number of Objects' = $matches['number']
})
}
}
# now decide on the format of this result
# 1) as plain text in tabular form.
# This looks best when used with a MonoSpaced font like Courier or Consolas.
$result | Format-Table -AutoSize | Out-String
# or 2) as HTML table. You will have to style this table in your email.
# You may include a stylesheet using the '-CssUri' parameter, but inserting
# it in a nicely drawn-up HTML template will give you more creative freedom.
$result | ConvertTo-Html -As Table -Fragment
#创建一个数组以将结果存储在对象中
$result=@()
#循环遍历此日志项数组
$logEntries | ForEach对象{
#这里,每个文本行都由特殊变量表示$_
#根据你的问题,我推测他们都有以下格式:
#“01-01-2018成功对象数量-1”
如果($匹配'(?\d{2}-\d{2}-\d{4})\s+(?[^-\d]+)[\s\-]+(?\d+\s*$){
#尝试从描述中删除“成功”或“失败”(??)文本部分
$description=($matches['description']-替换'Number of | object[s]?','').Trim()+'object'
如果([int]$matches['number']-ne 1){$description+='s'}
#添加到结果数组中
$result+=新对象-TypeName PSObject-Property([ordered]@{
“日期”=$matches[“日期”]
“Description”=$Description
“对象数”=$matches['Number']
})
}
}
#现在决定这个结果的格式
#1)以表格形式显示为纯文本。
#当与Courier或Consoleas等等距字体一起使用时,这种字体看起来最好。
$result |格式表-自动调整大小|输出字符串
#或2)作为HTML表。您必须在电子邮件中设置此表格的样式。
#您可以使用“-CssUri”参数包括样式表,但插入
#它在一个精心设计的HTML模板将给你更多的创作自由。
$result |转换为Html-作为表-片段
p、 由于PSObject具有
[ordered]
属性,因此需要PowerShell 3.0或更高版本。作为第一步,您应该将提取的文本解析为自定义对象。请做一些研究,类似的问题以前也被问过(并回答过)。对于纯文本邮件,您将使用| Format Table | Out String
。对于HTML邮件,您可以使用转换为HTML
(请参阅)。如果您仍然需要PowerShell v3,只需使用[PSCustomObject]
而不是新对象
。
# create an array to store the results objects in
$result = @()
# loop through this array of log entries
$logEntries | ForEach-Object {
# Here every text line is represented by the special variable $_
# From your question I gather they all have this format:
# '01-01-2018 Number of Successful object - 1 '
if ($_ -match '(?<date>\d{2}-\d{2}-\d{4})\s+(?<description>[^\-\d]+)[\s\-]+(?<number>\d+)\s*$') {
# Try to get the 'Succesful' or 'Failed' (??) text part out of the description
$description = ($matches['description'] -replace 'Number of|object[s]?', '').Trim() + ' object'
if ([int]$matches['number'] -ne 1) { $description += 's' }
# add to the result array
$result += New-Object -TypeName PSObject -Property ([ordered]@{
'Date' = $matches['date']
'Description' = $description
'Number of Objects' = $matches['number']
})
}
}
# now decide on the format of this result
# 1) as plain text in tabular form.
# This looks best when used with a MonoSpaced font like Courier or Consolas.
$result | Format-Table -AutoSize | Out-String
# or 2) as HTML table. You will have to style this table in your email.
# You may include a stylesheet using the '-CssUri' parameter, but inserting
# it in a nicely drawn-up HTML template will give you more creative freedom.
$result | ConvertTo-Html -As Table -Fragment