[英]Error while sending email through outlook in power shell script
I have a powershell script as below. 我有一个如下的powershell脚本。 Here I check whether any .err file has been created before 5 minutes and if yes, then I am sending an email for each error file with first 5 lines of the .err file.When I try to run the script I receive an email for the first file, but i get an error for the second file as shown in the snapshot below.
在这里,我检查5分钟前是否已创建任何.err文件,如果是,那么我将为每个错误文件发送一封电子邮件,其中包含.err文件的前5行。当我尝试运行脚本时,我收到一封电子邮件第一个文件,但是第二个文件却出现错误,如下面的快照所示。 I am new to powershell, and I am struggling to find any solution for this error.
我是Powershell的新手,我正在努力寻找解决此错误的方法。
$ChkFile = "D:\ErrorLog\*.err"
$ChkFilePath = "D:\ErrorLog"
$o = New-Object -comObject Outlook.Application
$mail = $o.CreateItem(0)
$mail.importance = 2
$mail.subject = “Error Log“
$mail.To = “email@email.com“
$FileExists = Test-Path $ChkFile
$FileCount = Get-ChildItem $ChkFilePath *.err | Measure-Object | %{$_.Count}
If ($FileExists -eq $True) {
If ($FileCount -gt 0)
{
Foreach($file in (Get-ChildItem $ChkFile))
{
Write-Host $file
$createtime = $file.LastWriteTime
$nowtime = get-date
if (($nowtime - $createtime).totalminutes -gt 5)
{
$GetFileContent = Get-Content $file -totalcount 5
$mail.body = $GetFileContent
Write-Host $GetFileContent
$mail.Send()
}
}
}
}
After you invoke the $mail.Send()
method you will likely need to recreate your mail object. 调用
$mail.Send()
方法后,您可能需要重新创建邮件对象。 You could do this by placing the object creation in the loop. 您可以通过将对象创建放在循环中来实现。
$ChkFile = "D:\ErrorLog\*.err"
$ChkFilePath = "D:\ErrorLog"
$o = New-Object -comObject Outlook.Application
$FileExists = Test-Path $ChkFile
$FileCount = Get-ChildItem $ChkFilePath *.err | Measure-Object | %{$_.Count}
If ($FileExists -eq $True) {
If ($FileCount -gt 0) {
Foreach($file in (Get-ChildItem $ChkFile)) {
Write-Host $file
$createtime = $file.LastWriteTime
$nowtime = get-date
if (($nowtime - $createtime).totalminutes -gt 5) {
$mail = $o.CreateItem(0)
$mail.importance = 2
$mail.subject = "Error Log"
$mail.To = "email@email.com"
$mail.importance = 2
$mail.subject = "Error Log"
$mail.To = "email@email.com"
$GetFileContent = Get-Content $file -totalcount 5
$mail.body = $GetFileContent
Write-Host $GetFileContent
$mail.Send()
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.