簡體   English   中英

如何在 PowerShell 中創建一個 Outlook 規則,該規則將 email 完美地移動到一個文件夾中,而不會出現錯誤,使用與 Z3D23D74EFA19FBE247 中相同的代碼?

[英]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 規則來移動 email,但它不起作用。

請注意,我無權訪問服務器,因此像New-InboxRule這樣的*-InboxRule cmdlet 不可用。

There's something wonky going on with the COM interop between PowerShell and Outlook, because it works perfectly in C#, but the identical code in PowerShell doesn't.

這是 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 中,它不會拋出錯誤或任何東西......它只是默默地什么都不做。

我怎樣才能讓它工作? 請幫忙!

你可以這樣做:

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

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM