【问题标题】:Reading date and time from a text file using powershell Get-Content使用 powershell Get-Content 从文本文件中读取日期和时间
【发布时间】:2020-02-26 11:17:55
【问题描述】:

我有一个文本文件,其中包含日志文件的最后更新。

 Volume in drive E is ISCSI
 Volume Serial Number is XXXXXX

 Directory of E:\Apps\LOGS

02/26/2020  11:39 AM           762,661-vx_02262020.log
               1 File(s)        762,661 bytes
               0 Dir(s)  12,554,166,272 bytes free

我正在使用下面的 powershell 脚本来阅读内容并通过电子邮件发送相同的内容:

$Date = (Get-Date -format "MMddyyyy")

$body = Get-Content -path C:\Desktop\Logs\LOG_TIME_$Date.txt -raw

Send-MailMessage -To "abc@gmail.com2" -From "abc@gmail.com"  -Subject "LOG TIME $Date" -Body "$body" -SmtpServer "xxxxx.main.glb.corp.local"

代码运行良好,并将正文中文件的所有内容发送到我的电子邮件。但我只希望在电子邮件中发送日期和时间(会不断变化)。

我尝试了很多选项,但似乎没有一个可行:

  • 二手 |选择字符串“05/”
  • 使用 Where-Object
  • 使用 -Pattern '$Date' -AllMatches |

有人可以帮我从文本文件中仅填充电子邮件正文中的日期和时间吗?

【问题讨论】:

  • 你能详细说明一下吗?电子邮件的所需正文是什么?仅从日志文件中读取日期和时间,或者?附:您不应该在代码中使用大引号 。这在 word 文档中可能看起来不错,但会在代码中产生奇怪的错误。将它们替换为直引号"
  • 感谢 Theo 的 cmets。已完成更改并详细说明了声明。是的 - 我只需要文本文件中的日期和时间出现在电子邮件正文中。日期和时间会不断变化。

标签: powershell powershell-2.0


【解决方案1】:

要解析文件中的日期和时间,您可以使用:

$Date = (Get-Date -format "MMddyyyy")

$content = Get-Content -Path "C:\Desktop\Logs\LOG_TIME_$Date.txt" -Raw
if ($content -match '(\d{2}/\d{2}/\d{4}\s+\d{1,2}:\d{2}\s+[AP]M)') {
    $dateString = $matches[1] -replace '\s+', ' '
    $dateInFile = [DateTime]::ParseExact($dateString, 'MM/dd/yyyy h:mm tt', [cultureinfo]'en-UK')
}

$mailParams = @{
    To         = "abc@gmail.com"
    From       = "abc@gmail.com"
    Subject    = "LOG TIME $Date"
    Body       = $dateInFile
    SmtpServer = "xxxxx.main.glb.corp.local"
}

Send-MailMessage @mailParams

正则表达式详细信息:

(             # Match the regular expression below and capture its match into backreference number 1
   \d         # Match a single digit 0..9
      {2}     # Exactly 2 times
   /          # Match the character "/" literally
   \d         # Match a single digit 0..9
      {2}     # Exactly 2 times
   /          # Match the character "/" literally
   \d         # Match a single digit 0..9
      {4}     # Exactly 4 times
   \s         # Match a single character that is a "whitespace character" (spaces, tabs, line breaks, etc.)
      +       # Between one and unlimited times, as many times as possible, giving back as needed (greedy)
   \d         # Match a single digit 0..9
      {1,2}   # Between one and 2 times, as many times as possible, giving back as needed (greedy)
   :          # Match the character ":" literally
   \d         # Match a single digit 0..9
      {2}     # Exactly 2 times
   \s         # Match a single character that is a "whitespace character" (spaces, tabs, line breaks, etc.)
      +       # Between one and unlimited times, as many times as possible, giving back as needed (greedy)
   [AP]       # Match a single character present in the list "AP"
   M          # Match the character "M" literally
)

【讨论】:

  • 完美理论。该解决方案工作正常,但我没有得到 AM 和 PM。输出只是日期和时间:“02/26/2020 11:39:00” - 提前非常感谢。
  • @AnkitDalal 电子邮件中日期的格式可以是任何你喜欢的格式。目前,它只是使用本地机器中设置的.ToString() 格式,但您可以添加自己的格式,使用StandardCustom 格式字符串。
  • @AnkitDalal 如果您对从文件 (MM/dd/yyyy h:mm tt) 解析的格式没问题,您甚至可以注释掉将日期解析为 [DateTime] 对象的行并设置 @ 987654327@ 在正文中。
【解决方案2】:

使用正则表达式轻松完成:

$date = $content | ? { $_ -match '(\d{1,2}\/\d{1,2}\/\d{2,4})' } | % { $_ -replace '^(.*)(\d{1,2}\/\d{1,2}\/\d{2,4})(.*)$', '$2' }

【讨论】:

  • 我应该把它放在代码的什么地方?它似乎不起作用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-12-11
  • 2015-06-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多