Powershell列出最后的日志并插入正文电子邮件

时间:2019-02-02 14:34:05

标签: powershell

你好,我需要发送最后一个.log并在$ body中插入get-content

$Logpath = "D:\Script\"

$LastLog = Get-ChildItem $Logpath | Where-Object {$_.LastWriteTime -gt (Get-Date).AddDays(-1)} | select-object Name -last 1

$fullpath = Write-Output $Logpath $LastLog

$body = Get-Content $fullpath | out-string

PS D:\Script\> Get-ChildItem $Logpath | Where-Object {$_.LastWriteTime -gt (Get-Date).AddDays(-1)} | select-object Name -last 1

Name                                                                                                                                                                                                                                                               
----                                                                                                                                                                                                                                                               
test05.txt

$ fullpath不起作用,$ body我也不知道如何放置..

thx 4 help im noob

1 个答案:

答案 0 :(得分:0)

我认为这可能对您有帮助

$Logpath   = "D:\Script\"
$yesterday = (Get-Date).AddDays(-1)

# this will get you a FileInfo object of the latest log file
$LastLog = Get-ChildItem $Logpath -File | Where-Object {$_.LastWriteTime -gt $yesterday} | Select-Object -Last 1

# the FileInfo object has a 'FullName' property
Write-Host "Reading file '$($LastLog.FullName)'"

# to get the content as a single string, use the '-Raw' switch
$body = Get-Content $LastLog.FullName -Raw 

# to send this as email, have a look at the 'Send-MailMessage' cmdlet at
# https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/send-mailmessage?view=powershell-6

#basic example on 'Send-MailMessage'
Send-MailMessage -To "someone@example.com" -From "me@example.com" -Subject "Log $($LastLog.Name)" -Body $body