Я пишу сценарий, который в конце будет отправлять электронное письмо с журналами из завершенного процесса. Мы используем Outlook 2013 на работе, и я тестирую это как на Outlook 2013, так и на Outlook 2010 с теми же результатами.Отправить Outlook Mail (2013) Не работает
Сценарий будет генерировать почту только в том случае, если Outlook закрыт. Если он открыт, он выдает кучу ошибок. У кого-нибудь есть идея?
Вот код:
############ Setup the outlook object#####
$outlook = New-Object -ComObject Outlook.Application
$datestr = get-date -Format yyyyMM
$message = $outlook.CreateItem(0)
###### Create an email and add the logs as attachments ######
$toaddress1 = "[email protected]"
$msub = "Monthly load - logs attached $datestr"
$message.Recipients.Add($toaddress1)
$message.Subject = "$msub"
$message.htmlbody = "See attached logs for this months file load<br><br>"
$filepath = get-childitem "c:\path\log" | ? {$_.PSISContainer -eq $false} | select -ExpandProperty fullname
foreach($file in $filepath) {
$message.Attachments.Add($file)
$message.display()
}
Как я уже сказал, это работает, когда прогноз не является открытым, но с его открытым он генерирует следующие ошибки из PowerShell:
New-Object : Retrieving the COM class factory for component with CLSID
{0006F03A-0000-0000-C000-000000000046} failed due to the following error:
80080005 Server execution failed (Exception from HRESULT: 0x80080005
(CO_E_SERVER_EXEC_FAILURE)).
At c:\scripts\mailtest.ps1:4 char:13
+ $message = (New-Object -ComObject Outlook.Application)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ResourceUnavailable: (:) [New-Object], COMException
+ FullyQualifiedErrorId : NoCOMClassIdentified,Microsoft.PowerShell.Commands.NewObjectCommand
Exception calling "CreateItem" with "1" argument(s): "The RPC server is
unavailable. (Exception from HRESULT: 0x800706BA)"
At c:\scripts\mailtest.ps1:7 char:1
+ $message = $outlook.CreateItem(1)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : COMException
You cannot call a method on a null-valued expression.
At c:\scripts\mailtest.ps1:13 char:1
+ $message.Recipients.Add($toaddress1)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
At c:\scripts\mailtest.ps1:14 char:1
+ $message.Subject = "$msub"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [], COMException
+ FullyQualifiedErrorId : System.Runtime.InteropServices.COMException
The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
At c:\scripts\mailtest.ps1:15 char:1
+ $message.htmlbody = "Jeff attached are the logs from this months load ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [], COMException
+ FullyQualifiedErrorId : System.Runtime.InteropServices.COMException
You cannot call a method on a null-valued expression.
At c:\scripts\mailtest.ps1:20 char:1
+ $message.Attachments.Add($file)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
At c:\scripts\mailtest.ps1:21 char:1
+ $message.display()
+ ~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [], COMException
+ FullyQualifiedErrorId : System.Runtime.InteropServices.COMException
You cannot call a method on a null-valued expression.
At c:\scripts\mailtest.ps1:20 char:1
+ $message.Attachments.Add($file)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
At c:\scripts\mailtest.ps1:21 char:1
+ $message.display()
+ ~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [], COMException
+ FullyQualifiedErrorId : System.Runtime.InteropServices.COMException
You cannot call a method on a null-valued expression.
At c:\scripts\mailtest.ps1:20 char:1
+ $message.Attachments.Add($file)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
At c:\scripts\mailtest.ps1:21 char:1
+ $message.display()
+ ~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [], COMException
+ FullyQualifiedErrorId : System.Runtime.InteropServices.COMException
я пошел на MSDN и не удалось найти какие-либо обновленные методы для Outlook.Application
, так что, похоже, я делаю это правильно или, может быть, лучше? В этом случае Outlook всегда будет открыт в нашей среде.
Можете ли вы проверить файл, параметры, центр доверия, настройки центра доверия, программный доступ. Возможно, вам придется открыть настройки там, чтобы PowerShell мог манипулировать почтовыми файлами Outlook, пока он открыт. – Jeeped
Спасибо, хорошая идея, но, похоже, она настроена так, чтобы позволить прагматичному доступу отправлять почту от моего имени. Я также дошел до того, что временно отключил свой Антивирус, чтобы убедиться, что это позволит ему работать, но это не так. У меня есть другие скрипты VB, которые делают это успешно. Кажется глупым, что он работает только тогда, когда Outlook не работает. Спасибо ! – PCPaul
О, и я могу добавить, что это происходит как на Windows 8.1 Enterprise (Work), так и на Windows 10 Professional. Я сделал несколько исследований и нашел эту интересную статью об этом http://blogs.msdn.com/b/adioltean/archive/2005/06/24/432519.aspx – PCPaul