C#调用PowerShell管理Exchange2010

3 篇文章 0 订阅
3 篇文章 0 订阅

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();
}

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值