使用c#remotly在lync server中启用CsUser

使用c#remotly在lync server中启用CsUser,c#,lync-2013,C#,Lync 2013,我想从我的服务器上启用CsUser trought c#,该服务器在我的测试实验室中加入了我的域 lync server的FQDN为lync.mohsen.com,我希望在我的lync server中启用“asadi”(“asadi”存在于ldap和exchange中”) 这是我的密码 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threadi

我想从我的服务器上启用CsUser trought c#,该服务器在我的测试实验室中加入了我的域

lync server的FQDN为lync.mohsen.com,我希望在我的lync server中启用“asadi”(“asadi”存在于ldap和exchange中”)

这是我的密码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Management.Automation;
using System.Management.Automation.Remoting;
using System.Management.Automation.Runspaces;
using System.Security;

namespace ConsoleApplication2
{
    class Program
    {
        static SecureString GetSecurePassword(string password)
        {
            var securePassword = new SecureString();
            foreach (var c in password)
            {
                 securePassword.AppendChar(c);
            }

            return securePassword;
        }
        static void Main(string[] args)
        {

            string lyncUser = "administrator";
            string _pass = "abc@123";
            var lyncPW = GetSecurePassword(_pass);
            var fqdnlync = "lync.mohsen.com";
            var user = "asadi";
            var sipadd = "asadi@mohsen.com";
            // lync server 2013
            //this code is same as EXchange code in schima
            PSCredential creds = new PSCredential(lyncUser, lyncPW);

            WSManConnectionInfo conn = new WSManConnectionInfo(new Uri("https://lync.mohsen.com/ocspowershell"), "http://schemas.microsoft.com/powershell/Microsoft.PowerShell", creds);

            conn.AuthenticationMechanism = AuthenticationMechanism.Default;

            Runspace runspace_2 = RunspaceFactory.CreateRunspace(conn);
            PowerShell powershell_2 = PowerShell.Create();

            PSCommand command_2 = new PSCommand();
            command_2.AddCommand("Enable-CsUser");
            command_2.AddParameter("Identity", user);
            command_2.AddParameter("RegistrarPool", fqdnlync);
            command_2.AddParameter("SipAddressType", sipadd);
            powershell_2.Commands = command_2;

            try
            {
                runspace_2.Open();
                powershell_2.Runspace = runspace_2;
                powershell_2.Invoke();
            }
            catch (Exception ex)
            {
                //string er = ex.InnerException.ToString();
            }
            finally
            {
                runspace_2.Dispose();
                runspace_2 = null;

                powershell_2.Dispose();
                powershell_2 = null;

            }
        }
    }
}
但是这个代码不起作用


我哪里出错了?

我自己解决了这个问题

  • 确保在lync server中的IIS/internal(在我的情况下)/ocspowershell/authentication/windows身份验证设置为启用

  • 更改
    var sipadd=”asadi@mohsen.com“---->var sipadd=“sip:asadi@mohsen.com“

    3.改变

    command_2.AddParameter(“SipAddressType”,sipadd);-->command_2.AddParameter(“SipAddress”,sipadd);


  • 好的,thanx

    如果您给出错误消息,那就太好了,因为您的代码没有按预期工作。其他人很容易向您提供解决方案或建议。