C# 启用Lync用户
2011-11-18 16:34
260 查看
PowerShell ps; ps = PowerShell.Create(); ps.AddScript("Set-ExecutionPolicy Unrestricted"); ps.Invoke(); ps.AddScript(@"import-module Lync"); ps.Invoke(); ps.AddScript("Enable-CsUser -Identity {0}@domain.com -RegistrarPool lyncpool.domain.com -SipAddressType UserPrincipalName"); ps.Commands.AddCommand("Out-String");
Collection<PSObject> results = ps.Invoke();; foreach (PSObject obj in results) { stringBuilder.Append(obj.ToString()); } foreach (ErrorRecord error in ps.Streams.Error) { stringBuilder.Append(error.ToString()); }
通过C#执行powershell命令,需要导入System.Management.Automation.dll。该组件在powershell的安装目录。
1、首先需要powershell的执行环境
2、导入Lync模块
3、执行启用Lync用户的命令
以上代码在控制台程序中运行通过,但放在IIS运行,就会出现权限不足的情况。
如果执行成功就不会有任何的返回值。如果失败就会返回错误信息。
第二种方式
RunspaceConfiguration config = RunspaceConfiguration.Create(); Runspace myRs = RunspaceFactory.CreateRunspace(config); myRs.ApartmentState = System.Threading.ApartmentState.STA; myRs.ThreadOptions = PSThreadOptions.UseCurrentThread; myRs.Open(); RunspaceInvoke scriptInvoker = new RunspaceInvoke(myRs); scriptInvoker.Invoke("Set-ExecutionPolicy Unrestricted"); scriptInvoker.Invoke(@"Import-Module Lync"); Pipeline pipeline = myRs.CreatePipeline(); pipeline.Commands.AddScript("Enable-CsUser -Identity {0}@domain.com -RegistrarPool lyncpool.domain.com -SipAddressType UserPrincipalName"); Collection<PSObject> results = pipeline.Invoke();; foreach (PSObject obj in results) { stringBuilder.Append(obj.ToString()); }
这种方式可以在IIS中运行。
参考文档
http://stackoverflow.com/questions/6266108/powershell-how-to-import-module-in-a-runspace
http://gotspeech.net/blogs/marshallharrison/archive/2011/01/07/calling-powershell-from-c.aspx
http://stackoverflow.com/questions/6568160/error-executing-powershell-commandlets-using-c-sharp/6568919#6568919
相关文章推荐
- 在日常维护中,如何实现Microsoft Lync Server 2010自动启用和同步Active Directory 域服务中所有用户帐户的信息?
- Lync server 2013新建持久聊天室提示用户未启用SIP
- Lync-技巧-2.启用-用户-启用-语音-设置-线路URI
- lync 2010启用用户访问权限不够!
- [10月16日的脚本] 在微软Lync Server 2010中启用用户和企业语音
- Lync Server 2010控制面板中启用并配置用户
- Lync 项目经验-29-批量-启用用户-启用企业语音-设置分机号(项目中)
- lync 批量启用用户
- 在Lync中启用域管理员用户作为Lync用户 推荐
- lync 2010启用用户访问权限不够!
- 通过PowerShell命令批量启用Lync用户
- 批量启用lync用户-2
- [11月15日的脚本] 在微软Lync Server 2010中搜索启用特定功能的用户
- Lync Server 2010:启用Domain Admins组用户
- Lync 小技巧-14-为用户启用统一的联系人存储库-Lync-无联系人
- lync 2010 批量启用用户
- [10月16日的脚本] 在微软Lync Server 2010中启用用户和企业语音
- Lync Server 2010的部署系列_第十章 启用或禁用外部用户访问
- 为Active Directory 用户批量启用lync
- 部署企业版lync2013之三:lync安装及用户启用