|
Exchange2010与Exchange2007不同,除了边缘传输服务器可以使用本地命令管理程序,要使用远程命令管理程序,否则无法使用Exchange2010里的cmdlet。
using System
using System.Management.Automation; //---直接在csproj文件中添加引用<Reference Include="System.Management.Automation" />
using System.Management.Automation.Runspaces;
using System.Collections.ObjectModel;
using System.Security;
//---管理员的密码
String admin_pwd = "XXXXXXXXXXX";
char[] admin_pwd_Chars = admin_pwd.ToCharArray();
SecureString admin_pwd_s = new SecureString();
foreach (char c in admin_pwd_Chars)
{ admin_pwd_s.AppendChar(c); }
//--设置凭据
PSCredential pCredential = new PSCredential("administrator", admin_pwd_s);
//-- 远程连接,xxx.com.cn为Exchange2010服务器地址
WSManConnectionInfo pConnectionInfo = new WSManConnectionInfo(new Uri("http://xxx.com.cn/PowerShell"),"http://schemas.microsoft.com/PowerShell/Microsoft.Exchange", pCredential);
//-- 建立远程 runspace
Runspace runspace = RunspaceFactory.CreateRunspace(pConnectionInfo);
下面是使用get-mailbox命令检查test1帐户信息,验证通过。
string sScript = "get-mailbox -identity 'test1'";
pipline.Commands.AddScript(sScript);
try
{ Collection<PSObject> results = pipline.Invoke();
StringBuilder stringBuilder = new StringBuilder();
foreach (PSObject obj in results)
{ stringBuilder.AppendLine(obj.ToString()); }
MessageBox.Show(stringBuilder.ToString());
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message.ToString());
}
finally
{
pipline.Dispose();
runspace.Close();
} |
|
|
|
|
|
|