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.
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.
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:
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
}