Update check-outlook.ps1

This commit is contained in:
Markus Fleschutz 2022-10-12 15:12:38 +02:00
parent e0b603f1b6
commit 3f9ee0d023

View File

@ -1,8 +1,8 @@
<# <#
.SYNOPSIS .SYNOPSIS
Checks Outlook's Inbox Checks Outlook's inbox
.DESCRIPTION .DESCRIPTION
This PowerShell script checks the inbox of Outlook for new mails. This PowerShell script checks the inbox of Outlook for new/unread mails.
.EXAMPLE .EXAMPLE
PS> ./check-outlook PS> ./check-outlook
.LINK .LINK
@ -17,23 +17,14 @@ try {
$Namespace = $Outlook.GetNameSpace("MAPI") $Namespace = $Outlook.GetNameSpace("MAPI")
$Inbox = $Namespace.GetDefaultFolder(6) # 6 = olFolderInbox $Inbox = $Namespace.GetDefaultFolder(6) # 6 = olFolderInbox
[int]$Unread = 0 [int]$Unread = 0
$Sender = ""
[switch]$SameSender = $true
foreach($Mail in $Inbox.Items) { foreach($Mail in $Inbox.Items) {
if ($Mail.Unread -eq $false) { continue } if ($Mail.Unread -eq $false) { continue }
"⚠️ New mail '$($Mail.Subject)' from $($Mail.SenderName)."
$Unread++ $Unread++
if ("$Sender" -eq "") { $Sender = $Mail.SenderName
} elseif ("$Sender" -ne "$($Mail.SenderName)") { $SameSender = $false }
$Subject = $Mail.Subject
} }
if ($Unread -eq 0) { $Reply = "No new mails." if ($Unread -eq 0) { "✅ No new mails." }
} elseif ($Unread -eq 1) { $Reply = "One new mail from $Sender regarding $Subject."
} elseif ($SameSender) { $Reply = "$Unread new mails from $Sender."
} else { $Reply = "$Unread new mails."
}
"$Reply"
exit 0 # success exit 0 # success
} catch { } catch {
"Sorry: $($Error[0])" "Sorry: $($Error[0])"
exit 1 exit 1
} }