# Sending Emails and checking if Outlook is already running

**URL:** <https://forums.powershell.org/t/sending-emails-and-checking-if-outlook-is-already-running/23591>\
**Category:** PowerShell Help\
**Created:** [February 16, 2024, 9:07am UTC](https://forums.powershell.org/t/sending-emails-and-checking-if-outlook-is-already-running/23591 "2024-02-16T09:07:29Z")\
**Posts on this page:** 4\
**Page:** 2

<div class="post-metadata">

**Author:** ![Kolojo](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/kolojo/32/5189_2.png) [@Kolojo](https://forums.powershell.org/u/Kolojo)\
**Post date:** [February 20, 2024, 8:30am UTC](https://forums.powershell.org/t/sending-emails-and-checking-if-outlook-is-already-running/23591/21 "2024-02-20T08:30:27Z")

</div>

> [@Olaf](#):
>
> NOT AS IMAGES

Yeah, sorry, my bad 😅 😅

```auto
New-Object : The COM class factory for the component CLSID {0006F03A-0000-0000-C000-000000000046} could
cannot be retrieved due to the following error: 80080005 Server startup failed (exception from
HRESULT: 0x80080005 (CO_E_SERVER_EXEC_FAILURE)).
In line:32 characters:18
+ return @(& $origNewObject @psBoundParameters)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : ResourceUnavailable: (:) [New-Object], COMException
    + FullyQualifiedErrorId : NoCOMClassIdentified,Microsoft.PowerShell.Commands.NewObjectCommand

It is not possible to call a method on an expression that has ZERO.
In C:\Users\------\Documents\uptime_restart_email.ps1:35 Zeichen:1
+ $mail = $outlook.CreateItem(0)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

```

> [@Olaf](#):
>
> `$PSVersionTable`

This is what I get in my PowerShell, starting via Windows. It’s the exact same as in VScode.

```auto
Name Value
---- -----
PSVersion 5.1.22621.2506
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.22621.2506
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1

```

> [@Olaf](#):
>
> That’s always a great idea and very recommended. 😉

👍 😂

By the way, big thanks to everyone who tried to help so far! 😄

---

<div class="post-metadata">

**Author:** ![krzydoug](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/krzydoug/32/1008_2.png) [@krzydoug](https://forums.powershell.org/u/krzydoug)\
**Post date:** [February 20, 2024, 10:02am UTC](https://forums.powershell.org/t/sending-emails-and-checking-if-outlook-is-already-running/23591/22 "2024-02-20T10:02:53Z")

</div>

If your outlook is opened normally (not “as admin”, as it should be) then if you run powershell as admin it won’t be able to talk to the open outlook. You can confirm this easily. If powershell is ran uac elevated (run as admin) and you get that error, and you can close outlook and then it works, that’s why. Run powershell not as admin so it can access outlook under the correct token.

---

<div class="post-metadata">

**Author:** ![Kolojo](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/kolojo/32/5189_2.png) [@Kolojo](https://forums.powershell.org/u/Kolojo)\
**Post date:** [February 20, 2024, 10:31am UTC](https://forums.powershell.org/t/sending-emails-and-checking-if-outlook-is-already-running/23591/23 "2024-02-20T10:31:44Z")

</div>

> [@krzydoug](#):
>
> Run powershell not as admin so it can access outlook under the correct token.

You were right!!  
Now it doesn’t matter whether Outlook is closed or open!  
Thank you so much!  
**I appreciate everyone who helped**. ❤

_Just In case someone happens to need the same thing at some point, here is the whole script:_

```auto
Add-Type -AssemblyName System.Web.Extensions

# Changing the execution policy to "Bypass" (shouldn't actually be needed)
Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass

# Setting the maximum operating time in hours and days
$maxBetriebszeitInStunden = 72
$maxBetriebszeitInTagen = 14

# Set the scheduled restart time (e.g., 10:00 p.m.)
$geplanteNeustartzeit = Get-Date -Hour 22 -Minute 0 -Second 0

# Get the current uptime of the computer
$betriebszeit = (Get-CimInstance Win32_OperatingSystem).LastBootUpTime
$jetzt = Get-Date
$betriebszeitInStunden = ($jetzt - $betriebszeit).TotalHours
$betriebszeitInTagen = ($jetzt - $betriebszeit).TotalDays

# Check if the operating time has exceeded the maximum duration (72 hours)
if ($betriebszeitInStunden -ge $maxBetriebszeitInStunden) {
    
# Check if Outlook is already open
$outlookProcess = Get-Process -Name Outlook -ErrorAction SilentlyContinue

if ($null -ne $outlookProcess) {
   #if Outlook is already running, use the running instance
    $outlook = [Runtime.Interopservices.Marshal]::GetActiveObject('Outlook.Application')
} 
else {
  #if Outlook is not running, create a new instance
    $outlook = New-Object -ComObject Outlook.Application
}

Start-Sleep 5

$mail = $outlook.CreateItem(0)

# Set email properties (german LOL)
$mail.Subject = "Warnung: Empfohlene Betriebszeit überschritten" -replace "ä", [char]0x00E4 -replace "ö", [char]0x00F6 -replace "ü", [char]0x00FC
$mail.Body = "Die Betriebszeit Ihres Geräts hat 72 Stunden (3 Tage) überschritten. Bitte planen Sie einen Neustart ein.
Nach 14 Tagen Betriebszeit wird das Gerät ohne Vorwarnung neu gestartet.

(Dies ist eine automatisierte Nachricht)"
$mail.To = $outlook.Session.CurrentUser.Address

# Ersetzen Sie die Umlaute durch ihre Unicode-Werte (will only be needed in germany)
$mail.Body = $mail.Body -replace "ä", [char]0x00E4
$mail.Body = $mail.Body -replace "ö", [char]0x00F6
$mail.Body = $mail.Body -replace "ü", [char]0x00FC

# send the e-mail
$mail.Send()

# Close Outlook if it was not open before sending the email (I did it without this code, may not be working right)
# Stop-Process -Name OUTLOOK -Force -ErrorAction SilentlyContinue

}

# Check that the current time is the scheduled reboot time and the computer has been running for at least 14 days
if ($jetzt.Hour -eq $geplanteNeustartzeit.Hour -and $jetzt.Minute -eq $geplanteNeustartzeit.Minute -and $betriebszeitInTagen -ge $maxBetriebszeitInTagen) {
    # Restarting the computer without warning
    Restart-Computer -Force
}

```

✌

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)\
**Post date:** [March 21, 2024, 10:32am UTC](https://forums.powershell.org/t/sending-emails-and-checking-if-outlook-is-already-running/23591/24 "2024-03-21T10:32:09Z")

</div>

Closed on March 21, 2024, 10:32am UTC

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.

[Previous page](https://forums.powershell.org/t/sending-emails-and-checking-if-outlook-is-already-running/23591.md?page=1)
