如何在 PowerShell 中创建一个 Outlook 规则,将电子邮件无错误地移动到文件夹,使用在 C# 中完美运行的相同代码?

How do I create an Outlook rule in PowerShell that moves email to a folder without an error, using the same code that works in C# perfectly?

我正在尝试编写 PowerShell 代码来创建 Outlook 规则来移动电子邮件,但它不起作用。

请注意,我无法访问服务器,因此 *-InboxRule cmdlet,如 New-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);

虽然 C# 代码成功,但 PS 代码失败:

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 中,它不会抛出错误或任何东西......它只是默默地 nothing.

我怎样才能让它工作?请帮忙!

您可以 运行 在 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()

您可以使用:

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

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