Sending Emails and checking if Outlook is already running

Yeah, sorry, my bad :sweat_smile: :sweat_smile:

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

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

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

:+1: :joy:

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

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.

2 Likes

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

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

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
}

:v:

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