C#编程:Exchange 2010远程电源Shell设置绕过的发件人

问题描述:

我试图读取与在c#远程的powershell旁路发件人列表,添加一个额外的电子邮件到列表然后远程设置它  权限是精细和它读取列表细群组。

TypeNameOfValue 这是从一开始ContentFilterConfig返回是

Deserialized.Microsoft.Exchange.Data.MultiValuedProperty`1 [Microsoft.Exchange.Data.SmtpAddress,Microsoft.Exchange.Data,版本= 14.0.0.0,培养=中性公钥= 31bf3856ad364e35]]鸟;  

但如果我尝试投值作为(的 MultiValuedProperty 跨度><的 Smtp地址跨度> 跨度>&GT)它总是返回null。

作为一种解决方法,我只需将值转换为字符串并重新创建地址列表。 

当我运行set-ContentFilterConfig命令时,我总是收到错误。

"无法在参数'BypassedSenders'上处理参数转换。无法转换价值\"电子邮件地址... \"键入\" Microsoft.Exchange.Data.MultiValuedProperty`1 [Microsoft.Exchange.Data.SmtpAddress] \"。错误:\"无法转换\"第一个电子邮件地址... \"值类型\" Deserialized.Microsoft.Exchange.Data.SmtpAddress \"键入\" Microsoft.Exchange.Data.SmtpAddress \"。\""

电子邮件地址...和第一个电子邮件地址...是被删除的电子邮件地址从具体的错误信息。

我不知道为什么错误输出,为什么演员阵容不适用于get,以及为什么值集的类型为 MultiValuedProperty 跨度><的 Smtp地址 > 在参数中无效。 感谢任何帮助。


我看过许多"获取"的例子。命令但没有"设置"在powershell中工作的命令。

几乎有效的代码:

公共静态int AddUserToByPassedSenders(字符串电子邮件)
    ;     {
            VAR connectionInfo =新WSManConnectionInfo(新URI(" HTTPS:/ /mydomain.com/powershell"),"http://schemas.microsoft.com/powershell/Microsoft.Exchange &QUOT ;, ExchangeCredential());
             Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo);
            connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Basic;
            PowerShell powershell = PowerShell.Create();
            var command = new PSCommand();
            command.AddCommand(QUOT;得到-ContentFilterConfig&QUOT);
            powershell.Commands = command;
           尝试
            {
                runspace.Open();
                powershell.Runspace =运行空间;
               收集和LT; PSObject>结果= powershell.Invoke();
               串bypassedSenders =结果[0] .Members [" BypassedSenders&QUOT]。Value.ToString();
                如果(!bypassedSenders.Contains(电子邮件))//电子邮件是不是已经在名单
             &NBSP ;  {
                    。串[] bypassedSendersList = bypassedSenders.ToLower()更换(QUOT;",",&QUOT)分割( '');。
                     //重新创建列表,因为投不工作
                 &NBSP ;  VAR地址=新MultiValuedProperty< Smtp地址>();
                 &NBSP ; 的foreach(在bypassedSendersList串EMAILADDRESS)
                    {
                      &NBSP ; addresses.Add(新Smtp地址(EMAILADDRESS));
                 &NBSP ;  }
                    addresses.Add(新Smtp地址(电子邮件));
                 &NBSP ;  VAR命令2 =新PSCommand();
                    command2.AddCommand(QUOT;设置-ContentFilterConfig&QUOT);
                     //添加新的列表中设置
                  &NBSP ; command2.AddParameter(QUOT; -BypassedSenders&QUOT ;,地址);
                &NBSP ;   powershell.Commands.Clear();
                    powershell.Commands =命令2;
                    powershell.Invoke();
                   回报(addresses.Count);

I am trying to read the bypassed senders list with remote powershell in c#, add an additional email to the list then set it remotely.  The permissions are fine and it reads the list fine. 

The TypeNameOfValue  that is returned from the get-ContentFilterConfig is

Deserialized.Microsoft.Exchange.Data.MultiValuedProperty`1[[Microsoft.Exchange.Data.SmtpAddress, Microsoft.Exchange.Data, Version=14.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]   

but if I try to cast the value as (MultiValuedProperty<SmtpAddress>) it always returns null.


As a work around I just cast the value to a string and recreate the address list. 

When I run the set-ContentFilterConfig command I always get the error.

"Cannot process argument transformation on parameter 'BypassedSenders'. Cannot convert value \"email addresses...\" to type \"Microsoft.Exchange.Data.MultiValuedProperty`1[Microsoft.Exchange.Data.SmtpAddress]\". Error: \"Cannot convert the \"first email address...\" value of type \"Deserialized.Microsoft.Exchange.Data.SmtpAddress\" to type \"Microsoft.Exchange.Data.SmtpAddress\".\""

email addresses... and first email address... are email addresses that are removed from the specific error message.

I have no idea why it is erroring out, why the cast does not work on the get, and why the value set is of type MultiValuedProperty<SmtpAddress> is not valid in the argument.  Any help is appreciated.

I have seen many examples of "get" commands but no "set" commands that work in powershell.


code that almost works:

 public static int AddUserToByPassedSenders(string email)
        {
            var connectionInfo = new WSManConnectionInfo(new Uri("https://mydomain.com/powershell"),"http://schemas.microsoft.com/powershell/Microsoft.Exchange", ExchangeCredential());
            Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo);
            connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Basic;
            PowerShell powershell = PowerShell.Create();
            var command = new PSCommand();
            command.AddCommand("get-ContentFilterConfig");
            powershell.Commands = command;
            try
            {
                runspace.Open();
                powershell.Runspace = runspace;
                Collection<PSObject> results = powershell.Invoke();
                string bypassedSenders = results[0].Members["BypassedSenders"].Value.ToString();
                if (!bypassedSenders.Contains(email))//email is not already on the list
                {
                    string[] bypassedSendersList = bypassedSenders.ToLower().Replace(" ", ",").Split(',');
                    //recreate the list because the cast does not work
                    var addresses = new MultiValuedProperty<SmtpAddress>();
                    foreach (string emailaddress in bypassedSendersList)
                    {
                        addresses.Add(new SmtpAddress(emailaddress));
                    }
                    addresses.Add(new SmtpAddress(email));
                    var command2 = new PSCommand();
                    command2.AddCommand("Set-ContentFilterConfig");
                    //add the new list to be set
                    command2.AddParameter("-BypassedSenders", addresses);
                    powershell.Commands.Clear();
                    powershell.Commands = command2;
                    powershell.Invoke();
                    return (addresses.Count);

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; }
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; }
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; catch(例外e)
            {
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; SendMail.SendErrorMessage(E);
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; }
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;最后
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; {
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; runspace.Dispose();
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; powershell.Dispose();
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; }
&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;返回-1;

                }
            }
            catch (Exception e)
            {
                SendMail.SendErrorMessage(e);
            }
            finally
            {
                runspace.Dispose();
                powershell.Dispose();
            }
            return -1;

        };

        };

查看错误可能是某种序列化/反序列化错误。 

我所做的就是复制所需的DLL版本Microsoft.Exchange.Data.Common.dll
Microsoft.Exchange.Data.Directory.dll
Microsoft.Exchange。 Data.dll
Microsoft.Exchange.Diagnostics.dll
Microsoft.Exchange.Net.dll
System.Management.Automation.dll

来自实际的Exchange 2010服务器所以我可以编译并运行这个项目。

我是否需要序列化和反序列化结果和输入?
Looking at the error it may be some sort of serialization/deserialization error. 

All I did was copy the DLL's needed
Microsoft.Exchange.Data.Common.dll
Microsoft.Exchange.Data.Directory.dll
Microsoft.Exchange.Data.dll
Microsoft.Exchange.Diagnostics.dll
Microsoft.Exchange.Net.dll
System.Management.Automation.dll

from the actual exchange 2010 server so I could compile and run this project.

Do I need to serialize and deserialize the results and inputs?