基于PowerShell的Lync Server管理 使用C#

 这里所说的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;
using System.Security;
using System;
using System.Collections.ObjectModel;
public class RemoteRunspace //: IRunspace
{
    /// <summary>
    /// 远程运行空间创建成功后,就可以创建管理,运行Lync管理命令
    /// </summary>
    /// <returns></returns>
    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();
            System.Console.WriteLine("创建Remote Runspace成功!");
            return runspace;
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
            return null;
        }
    }
   
}

  

  3、远程运行空间创建成功后,就可以创建管理,运行Lync管理命令

  

 /// <summary>
    /// 远程运行空间创建成功后,就可以创建管理,运行Lync管理命令
    /// </summary>
    /// <param name="commandText"></param>
    /// <returns></returns>
    public Collection<PSObject> ExcuteCmand(string commandText)
    { 
using (var runspace = GetRunspace())
{
using (Pipeline pipe = runspace.CreatePipeline())
{
Command cmd = new Command(commandText, true);
pipe.Commands.Add(cmd);
return pipe.Invoke();
}
}

  

    }

  

  现在我们可以通过这种方式,远程执行各种管理命令,而不需要再登陆到Lync服务器。

  Exchange服务器的管理类似Lync,这方面微软做的还是不错的。

  4、常用的Lync管理命令

  启用:Enable-CsUser -Identity xx -RegistrarPool (lync注册池) -SipAddressType

  禁用:Disable-CsUser -identity xx

  开启语音功能:Set-CsUser -identity xx -EnterpriseVoiceEnable $true -LineURI "TLE:xx"

原文地址:https://www.cnblogs.com/Qbit/p/3303596.html