I have a script that I use to modify users when they come into our department. The script is run as admin. Last part of the script is to create a draft mail that we can send when the account is modified. That part of the script needs to be run without admin credentials as it will not run.
It causes the following error when run in admin mode: 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)).
I found here: new Outlook.Application() thorws error if Outlook app is running and also other articles that this does not work if you run it in admin mode if outlook in normal user mode.
#Get Rank, Firstname and lastname of Incoming user
$Rank = Get-ADUser -Identity $username -properties title |Select-Object title -ExpandProperty title
$Firstname = Get-ADUser -Identity $username -properties givenName | Select-Object givenName -ExpandProperty givenName
$Lastname = Get-ADUser -Identity $username -properties Surname | Select-Object Surname -ExpandProperty Surname
#Create Subject of mail
$Subject = "Account modified"
#Create Body of mail
$Body = "Good day,<br>
Content mail
Kind regards,<br>
<br>
"
$Outlook = New-Object -comObject Outlook.Application
$Mail = $Outlook.CreateItem(0)
$Mail.Subject = $Subject
$Mail.HTMLBody = "$Body"
$Mail.save()
$inspector = $Mail.GetInspector
$inspector.Display()
As I am sharing this scripts with my colleagues I want this to work without having them to do.
So I need to run this from the script that I have opened with my admin rights. Unless someone has another way to create an outlook draft mail, that would also be great.
Found a lot of articles about the same issue, but none was a solution. I tried running with an invoke-command Tried running as a different user. Tried running it as a script in a script.
Any help would be much appreciated.