如何在PowerShell中创建将电子邮件移动到文件夹的Outlook规则?

yx2lnoni  于 2023-10-18  发布在  Shell
关注(0)|答案(2)|浏览(125)

我正在尝试编写PowerShell代码来创建Outlook规则来移动电子邮件。

注意我没有访问服务器的权限,因此New-InboxRule*-InboxRule插件不可用。

PowerShell和Outlook之间的COM互操作有些不稳定,因为它在C#中工作,但PowerShell中的相同代码却不工作。
C#代码:

Microsoft.Office.Interop.Outlook.Application outlook = null;

try
{
    outlook = (Microsoft.Office.Interop.Outlook.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Outlook.Application");
}
catch
{
}

if (outlook == null)
{
    outlook = new Microsoft.Office.Interop.Outlook.Application();
}

var inbox = outlook.Application.Session.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
var oMoveTarget = inbox.Folders["MoveTarget"];

// debugging
Console.WriteLine(inbox.FolderPath.ToString());
Console.WriteLine(oMoveTarget.FolderPath.ToString());

var rules = outlook.Session.DefaultStore.GetRules();
Console.WriteLine(string.Format("There are {0} rules", rules.Count));

var name = string.Format("Rule {0}", DateTime.Now.ToString("yyyyMMdd_HHmmss"));
var rule = rules.Create(name, Microsoft.Office.Interop.Outlook.OlRuleType.olRuleReceive);

// conditions
rule.Conditions.From.Recipients.Add("John Smith");
rule.Conditions.From.Recipients.ResolveAll();
rule.Conditions.From.Enabled = true;

// actions
rule.Actions.MoveToFolder.Folder = oMoveTarget;
rule.Actions.MoveToFolder.Enabled = true;
rules.Save(true);
  • 完全相同 *(除了语言语法)PowerShell代码:
try
{
    $outlook = [Runtime.InteropServices.Marshal]::GetActiveObject("Outlook.Application");
}
catch
{
}

if ($outlook -eq $null)
{
    $outlook = New-Object -ComObject Outlook.Application
}

$inbox = $outlook.Application.Session.GetDefaultFolder([Microsoft.Office.Interop.Outlook.OlDefaultFolders]::olFolderInbox);
$oMoveTarget = $inbox.Folders["MoveTarget"];

# debugging
[Console]::WriteLine($inbox.FolderPath.ToString());
[Console]::WriteLine($oMoveTarget.FolderPath.ToString());

$rules = $outlook.Session.DefaultStore.GetRules();
[Console]::WriteLine([string]::Format("There are {0} rules", $rules.Count));

$name = [string]::Format("Rule {0}", [DateTime]::Now.ToString("yyyyMMdd_HHmmss"));
$rule = $rules.Create($name, [Microsoft.Office.Interop.Outlook.OlRuleType]::olRuleReceive);

# conditions
$rule.Conditions.From.Recipients.Add("John Smith");
$rule.Conditions.From.Recipients.ResolveAll();
$rule.Conditions.From.Enabled = $true;

# actions
$rule.Actions.MoveToFolder.Folder = $oMoveTarget;
$rule.Actions.MoveToFolder.Enabled = $true;
$rules.Save($true);

PowerShell代码失败:

One or more rules cannot be saved because of invalid actions or conditions.
At line:1 char:1
+ $rules.Save($true);
+ ~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OperationStopped: (:) [], COMException
    + FullyQualifiedErrorId : System.Runtime.InteropServices.COMException

经过进一步调查,它是由这条线引起的,这条线没有做任何事情:

$rule.Actions.MoveToFolder.Folder = $oMoveTarget;

当我运行C#等效程序时,如果我立即查看该属性,它就 * 被 * 设置了。
在PowerShell中,它什么也不做。

vwkv1x7d

vwkv1x7d1#

您可以通过以下方式执行此操作:

# actions
$action = $rule.Actions.MoveToFolder
$action.Enabled = $true

[Microsoft.Office.Interop.Outlook.MoveOrCopyRuleAction].InvokeMember("Folder",[Reflection.BindingFlags]::SetProperty, $null, $action, $oMoveTarget)
4si2a6ki

4si2a6ki2#

你可以在PowerShell中运行C#代码。是否需要在powershell中重写它?

$code = @'
using System;

namespace Outlook
{
    public class Outlook
    {
        public static void Main(){
            Microsoft.Office.Interop.Outlook.Application outlook = null;

            try
            {
                outlook = (Microsoft.Office.Interop.Outlook.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("Outlook.Application");
            }
            catch
            {
            }

            if (outlook == null)
            {
                outlook = new Microsoft.Office.Interop.Outlook.Application();
            }

            var inbox = outlook.Application.Session.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
            var oMoveTarget = inbox.Folders["TargetFolder"];

            // debugging
            Console.WriteLine(inbox.FolderPath.ToString());
            Console.WriteLine(oMoveTarget.FolderPath.ToString());

            var rules = outlook.Session.DefaultStore.GetRules();
            Console.WriteLine(string.Format("There are {0} rules", rules.Count));

            var name = string.Format("Rule {0}", DateTime.Now.ToString("yyyyMMdd_HHmmss"));
            var rule = rules.Create(name, Microsoft.Office.Interop.Outlook.OlRuleType.olRuleReceive);

            // conditions
            rule.Conditions.From.Recipients.Add("John Smith");
            rule.Conditions.From.Recipients.ResolveAll();
            rule.Conditions.From.Enabled = true;

            // actions
            rule.Actions.MoveToFolder.Folder = oMoveTarget;
            rule.Actions.MoveToFolder.Enabled = true;
            rules.Save(true);
        }
    }
}
'@

$assemblies = ("Microsoft.Office.Interop.Outlook")

Add-Type -ReferencedAssemblies $assemblies  -TypeDefinition $code -Language CSharp

[Outlook.Outlook]::Main()

相关问题