Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell cannot create Outlook COM object from Command Prompt

I have a script used to send mails via Microsoft Outlook from command prompt. This works fine if I run it from inside PowerShell or ISE console. But when I tried to execute same from classic Windows Command prompt (cmd.exe) even with Admin privileges, it was unable to create Outlook COM object. Here is the line to create COM object:

$objOutLook = New-Object -com Outlook.Application

This is how I call my script from cmd.exe (Administrative privileges) :

D:>powershell D:\MiscBuildTasks.ps1 -sendmail -MailTo '[email protected]'

and here is the error log:

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 D:\MiscBuildTasks.ps1:81 char:12 + $Outlook = New-Object -ComObject Outlook.Application + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ResourceUnavailable: (:) [New-Object], COMException + FullyQualifiedErrorId : NoCOMClassIdentified,Microsoft.PowerShell.Commands.NewObjectCommand

How can I make it work from cmd.exe (Windows Command Prompt)?? Thanks

like image 813
Farrukh Waheed Avatar asked Apr 21 '15 12:04

Farrukh Waheed


2 Answers

CO_E_SERVER_EXEC_FAILURE in case of Outlook means the calling app and the COM server are running in different security contexts. If the command prompt is running with elevated privileges, either make sure Outlook is started with elevated privileges as well or that it does not run at all when your code is executed - this way Outlook will be started by your code and it will run with the same elevated privileges.

like image 96
Dmitry Streblechenko Avatar answered Sep 24 '22 20:09

Dmitry Streblechenko


I had the same issue, I figured out it was a combination of 3 things.

  1. Running with the correct privilege level (admin)
  2. Using the same arch (32-bit vs 64-bit) for outlook and powershell
  3. Closing Outlook, since the script will open it.
like image 26
gryphon Avatar answered Sep 24 '22 20:09

gryphon