【发布时间】:2015-01-28 12:24:22
【问题描述】:
我的代码试图让 lync 会话从 c# 运行 powershell 脚本。尝试运行脚本时 "$CSSession = New-CsOnlineSession -Credential $cred\n" 我收到空引用异常。
堆栈跟踪:
System.Management.Automation.CmdletInvocationException 未处理
HResult=-2146233087 Message=对象引用未设置为实例 的一个对象。来源=System.Management.Automation
WasThrownFromThrowStatement=false 堆栈跟踪: 在 System.Management.Automation.Internal.PipelineProcessor.SynchronousExecuteEnumerate(对象 输入,哈希表错误结果,布尔枚举) 在 System.Management.Automation.PipelineOps.InvokePipeline(对象输入, 布尔忽略输入,CommandParameterInternal[][] pipeElements, CommandBaseAst[] pipeElementAsts, CommandRedirection[][] 命令重定向,FunctionContext funcContext) 在 System.Management.Automation.Interpreter.ActionCallInstruction`6.Run(InterpretedFrame 框架) 在 System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame 框架) 在 System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame 框架)InnerException:System.NullReferenceException H结果=-2147467261 Message=对象引用未设置为对象的实例。 源=Microsoft.Rtc.Admin.AuthenticationHelper 堆栈跟踪: 在 Microsoft.Rtc.Admin.Authentication.ManagedIdcrl..ctor(IDCRLMode 模式) 在 Microsoft.Rtc.Admin.Authentication.ManagedIdcrl..ctor() 在 Microsoft.Rtc.Management.LyncOnlineConnector.GetWebTicketCmdlet.CreateAndInitializeManagedIdcrl() 在 Microsoft.Rtc.Management.LyncOnlineConnector.GetWebTicketCmdlet.get_ManagedIdcrl() 在 Microsoft.Rtc.Management.LyncOnlineConnector.GetWebTicketCmdlet.GetLiveIdToken(字符串 remoteFqdn、Int32 端口、PSCredential 凭据) 在 Microsoft.Rtc.Management.LyncOnlineConnector.GetWebTicketCmdlet.ConnectToWebTicketService(字符串 fqdn、Int32 端口、PSCredential 凭据) 在 Microsoft.Rtc.Management.LyncOnlineConnector.GetWebTicketCmdlet.BeginProcessing() 在 System.Management.Automation.Cmdlet.DoBeginProcessing() 在 System.Management.Automation.CommandProcessorBase.DoBegin() 内部异常:
代码是:
public static void GetLyncUsers(string userName, string plainPassword)
{
RunspaceConfiguration config = RunspaceConfiguration.Create();
using (Runspace myRs = RunspaceFactory.CreateRunspace(config))
{
myRs.Open();
RunspaceInvoke scriptInvoker = new RunspaceInvoke(myRs);
scriptInvoker.Invoke("Set-ExecutionPolicy Unrestricted");
using (PowerShell powerShellInstance = PowerShell.Create())
{
powerShellInstance.Runspace = myRs;
// Import module.
powerShellInstance.Commands.AddCommand("Import-Module")
.AddArgument(
@"C:\Program Files\Common Files\Microsoft Lync Server 2013\Modules\LyncOnlineConnector\LyncOnlineConnector.psd1");
powerShellInstance.Invoke();
powerShellInstance.Commands.Clear();
// Set credentials
SecureString password = new SecureString();
foreach (var passChar in plainPassword)
{
password.AppendChar(passChar);
}
PSCredential credential = new PSCredential(userName, password);
powerShellInstance.AddCommand("Set-Variable");
powerShellInstance.AddParameter("Name", "cred");
powerShellInstance.AddParameter("Value", credential);
powerShellInstance.Invoke();
powerShellInstance.Commands.Clear();
// Run the script
var script = string.Format(
"$CSSession = New-CsOnlineSession -Credential $cred\n");
powerShellInstance.AddScript(script);
Collection<PSObject> psOutput = powerShellInstance.Invoke(); //Getting exception here.
// check the other output streams (for example, the error stream)
if (powerShellInstance.Streams.Error.Count > 0)
{
// error records were written to the error stream.
// do something with the items found.
}
}
}
}
【问题讨论】:
标签: c# powershell