通过powershell从outlook中删除电子邮件



我正试图在共享邮箱(Exchange(上运行powershell脚本。当我在我的个人邮箱上运行该脚本时,它是有效的,尽管当我将其更改为共享邮箱时,我只会收到错误消息。

$outlook = new-object -comobject outlook.application
$logfile = $PSScriptRoot + "email.log"
$namespace = $outlook.GetNameSpace('MAPI')
$folder = $namespace.Folders.Item('shared.mailbox@company.com').Folders.Item('Inbox')
$folder_2 = $namespace.Folders.Item('shared.mailbox@company.com').Folders.Item('Deleted Items')
$date_check = (get-date).AddDays(-1)  | Get-Date -UFormat "%m-%d-%Y"
$date_check_deleted_items = (get-date).AddDays(-2)  | Get-Date -UFormat "%m-%d-%Y"
$emailToDelete = $folder.items | Where-Object { $_.ReceivedTime -lt $date_check; }
$emptyDeletedItems = $folder_2.items | Where-Object { $_.ReceivedTime -lt $date_check_deleted_items; }
Get-Date | Out-File $logfile -Append
"Amount of emails being deleted:" | Out-File $logfile -Append
$emailToDelete.Count | Out-File $logfile -Append
"Subject of Emails in the Inbox folder:" | Out-File $logfile -Append
$emailToDelete.subject | Out-File $logfile -Append
"Amount of emails being deleted from deleted items:" | Out-File $logfile -Append
$emptyDeletedItems.Count | Out-File $logfile -Append
"Subject of Emails in the Deleted Items folder folder:" | Out-File $logfile -Append
$emptyDeletedItems.subject | Out-File $logfile -Append
write-host "Emails received before" $date_check "(MM/dd/YYYY) will be deleted"
write-host "deleting emails in 5"
Start-Sleep -s 1
write-host "deleting emails in 4"
Start-Sleep -s 1
write-host "deleting emails in 3"
Start-Sleep -s 1
write-host "deleting emails in 2"
Start-Sleep -s 1
write-host "deleting emails in 1"
Start-Sleep -s 1
write-host "deleting" $emailToDelete.Count "emails"

#$EmailToDelete.Delete()
write-host $emptyDeletedItems.count "Emails to be deleted from the deleted items folder"
write-host "Emails received before" $date_check_deleted_items "(MM/dd/YYYY) will be deleted from that folder"
$emptyDeletedItems.Delete()
write-host $emailToDelete.count "Emails are deleted from the Inbox folder and" $emptyDeletedItems.count "Emails are deleted from the Deleted Items folder, application will close in 5 seconds"
Start-Sleep -s 5

我收到以下错误:

The attempted operation failed.  An object could not be found.
At C:UsersusernameDesktopemails.ps1:7 char:1
+ $folder = $namespace.Folders.Item('shared.mailbox@company.com').Fold ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : OperationStopped: (:) [], COMException
+ FullyQualifiedErrorId : System.Runtime.InteropServices.COMException
The attempted operation failed.  An object could not be found.
At C:UsersusernameDesktopemails.ps1:9 char:1
+ $folder_2 = $namespace.Folders.Item('shared.mailbox@company.com').Fo ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo          : OperationStopped: (:) [], COMException
+ FullyQualifiedErrorId : System.Runtime.InteropServices.COMException

你能帮帮我吗?我做错了什么?

我正试图在共享邮箱(Exchange(上运行powershell脚本

不完全如此。您正试图通过共享邮箱自动化Outlook。并且该帐户是在Outlook中本地配置的。您似乎无法从共享位置访问它。

另一个方面是,Microsoft目前不建议也不支持从任何无人参与的非交互式客户端应用程序或组件(包括ASP、ASP.NET、DCOM和NT服务(自动化Microsoft Office应用程序,因为在这种环境中运行Office时,Office可能会表现出不稳定的行为和/或死锁。

如果您正在构建一个在服务器端上下文中运行的解决方案,则应该尝试使用那些可以安全执行无人参与的组件。或者,您应该尝试找到至少允许部分代码运行客户端的替代方案。如果使用服务器端解决方案中的Office应用程序,则该应用程序将缺乏成功运行所需的许多功能。此外,您将在整体解决方案的稳定性方面承担风险。

请参阅"Office服务器端自动化的注意事项"一文中的更多内容。

最新更新