这里所说的Lync Server管理,指通过C#管理Lync账号的启用,禁用,开启账户的语音功能。

  Lync服务器安装后,会自动创建一个用于远程管理的应用程序,通过IIS查看,其应用程序名为:

  Lync Server Internal Web Site下边的OcsPowershell,通过浏览他的目录可以看出,这个是Lync用于远程执行管理命令的一个WebService,准备工作做好之后,接下来可以测试连接。

  1、引用System.Management.Automation.dll

  项目依赖于这个dll,地址在:C:Program FilesReference AssembliesMicrosoftWindowsPowerShellv1.0System.Management.Automation.dll,这个应该是电脑安装了Power Shell才会有的;

  2、创建远程运行空间

using System.Management.Automation;
using System.Management.Automation.Runspaces;
using System.Management.Automation.Host;
public class RemoteRunspace : IRunspace
{
public Runspace CreateRunspace()
{
//Lync管理webservice地址
String uri = "https://服务器名/OcsPowerShell";
//命令解析uri
String shellUri = http://schemas.microsoft.com/powershell/Microsoft.Powershell;
//管理账户
String userName = "xxx";
//管理密码
String userPwd = "xxx";
Char[] userPwds = userPwd.ToCharArray();
SecureString secPwd = new SecureString();
foreach (Char c in userPwds)
{
secPwd.AppendChar(c);
}
RunspaceConfiguration config = RunspaceConfiguration.Create();
WSManConnectionInfo connInfo = new WSManConnectionInfo(new Uri(uri), shellUri, new PSCredential(userName, secPwd));
Runspace runspace = RunspaceFactory.CreateRunspace(connInfo);
try
{
runspace.Open();
Console.WriteLine("创建Remote Runspace成功!");
return runspace;
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
return null;
}
}
}