![](/img/trans.png)
[英]How do I programatically create an exchange 2010 mailbox using C# without powershell
[英]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.