【发布时间】:2011-07-02 08:18:56
【问题描述】:
我编写了一个非常小的应用程序,它访问 Exchanger Server 2010 SP1 的 Remote Power Shell 并执行一些脚本。这是示例代码。一切都在 try and catch 块中。
string insecurePassword = "mypassword";
SecureString securePassword = new SecureString();
foreach (char passChar in insecurePassword.ToCharArray())
{
securePassword.AppendChar(passChar);
}
PSCredential credential = new PSCredential("mydomain\\administrator", securePassword);
WSManConnectionInfo connectionInfo = new WSManConnectionInfo(new Uri("http://exchange2010.domain.com/powershell?serializationLevel=Full"), "http://schemas.microsoft.com/powershell/Microsoft.Exchange", credential);
connectionInfo.AuthenticationMechanism = AuthenticationMechanism.Kerberos;
Runspace runspace = System.Management.Automation.Runspaces.RunspaceFactory.CreateRunspace(connectionInfo);
PowerShell powershell = PowerShell.Create();
PSCommand command = new PSCommand();
ICollection<System.Management.Automation.PSObject> results;
//The command I want to execute is Set-MailContact with some parameters.
command.AddCommand("Set-MailContact");
command.AddParameter("Identity", "SomeIdentityOfContact");
command.AddParameter("EmailAddressPolicyEnabled", false);
command.AddParameter("PrimarySmtpAddress", "myEmailAddress@domain.com");
command.AddParameter("Confirm", false);
command.AddParameter("Force", true);
powershell.Commands = command;
// open the remote runspace
runspace.Open();
// associate the runspace with powershell
powershell.Runspace = runspace;
// invoke the powershell to obtain the results
results = powershell.Invoke();
我正在尝试设置 MailContact 的 PrimarySmtpAddress,但由于某些原因,我收到以下异常:
System.Management.Automation.RemoteException:无法处理参数“PrimarySmtpAddress”的参数转换。无法将值“SMTP:myEmailAddress@domain.com”转换为类型“Microsoft.Exchange.Data.SmtpAddress”
我认为这一定是由于序列化/反序列化。有人知道如何正确传递电子邮件地址的值吗?
任何提示帮助将不胜感激!
【问题讨论】:
标签: c# powershell exchange-server