尝试 运行 powershell 脚本时 Microsoft.Rtc.Admin.AuthenticationHelper 出现空引用异常

Null reference exception in Microsoft.Rtc.Admin.AuthenticationHelper while trying to run powershell script

我有尝试从 c# 获取 lync 会话的代码 运行ning powershell 脚本。当尝试 运行 脚本时 "$CSSession = New-CsOnlineSession -Credential $cred\n" 我收到空引用异常。

堆栈跟踪:

System.Management.Automation.CmdletInvocationException was unhandled
HResult=-2146233087 Message=Object reference not set to an instance of an object. Source=System.Management.Automation
WasThrownFromThrowStatement=false StackTrace: at System.Management.Automation.Internal.PipelineProcessor.SynchronousExecuteEnumerate(Object input, Hashtable errorResults, Boolean enumerate) at System.Management.Automation.PipelineOps.InvokePipeline(Object input, Boolean ignoreInput, CommandParameterInternal[][] pipeElements, CommandBaseAst[] pipeElementAsts, CommandRedirection[][] commandRedirections, FunctionContext funcContext) at System.Management.Automation.Interpreter.ActionCallInstruction`6.Run(InterpretedFrame frame) at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame) at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame) InnerException: System.NullReferenceException HResult=-2147467261 Message=Object reference not set to an instance of an object. Source=Microsoft.Rtc.Admin.AuthenticationHelper StackTrace: at Microsoft.Rtc.Admin.Authentication.ManagedIdcrl..ctor(IDCRLMode mode) at Microsoft.Rtc.Admin.Authentication.ManagedIdcrl..ctor() at Microsoft.Rtc.Management.LyncOnlineConnector.GetWebTicketCmdlet.CreateAndInitializeManagedIdcrl() at Microsoft.Rtc.Management.LyncOnlineConnector.GetWebTicketCmdlet.get_ManagedIdcrl() at Microsoft.Rtc.Management.LyncOnlineConnector.GetWebTicketCmdlet.GetLiveIdToken(String remoteFqdn, Int32 port, PSCredential creds) at Microsoft.Rtc.Management.LyncOnlineConnector.GetWebTicketCmdlet.ConnectToWebTicketService(String fqdn, Int32 port, PSCredential creds) at Microsoft.Rtc.Management.LyncOnlineConnector.GetWebTicketCmdlet.BeginProcessing() at System.Management.Automation.Cmdlet.DoBeginProcessing() at System.Management.Automation.CommandProcessorBase.DoBegin() InnerException:

代码为:

    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.
                }
            }
        }

    }

我被告知我的 MS 支持目前不支持从 C# 调用 Lync Online PowerShell 脚本。