尝试运行powershell脚本时,Microsoft.Rtc.Admin.AuthenticationHelper中的空引用异常

时间:2015-01-28 12:24:23

标签: c# powershell

我有代码试图让lync会话运行PowerShell脚本形式c#。当试图运行脚本“$ CSSession = New-CsOnlineSession -Credential $ cred \ n”我得到空引用异常。

堆栈追踪:

  

System.Management.Automation.CmdletInvocationException未处理
  HResult = -2146233087 Message =对象引用未设置为实例   一个对象。来源= System.Management.Automation
  WasThrownFromThrowStatement = false StackTrace:          在System.Management.Automation.Internal.PipelineProcessor.SynchronousExecuteEnumerate(Object   input,Hashtable errorResults,Boolean enumerate)          在System.Management.Automation.PipelineOps.InvokePipeline(对象输入,   Boolean ignoreInput,CommandParameterInternal [] [] pipeElements,   CommandBaseAst [] pipeElementAsts,CommandRedirection [] []   commandRedirections,FunctionContext funcContext)          在System.Management.Automation.Interpreter.ActionCallInstruction`6.Run(InterpretedFrame)   帧)          在System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame)   帧)          在System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame)   frame)InnerException:System.NullReferenceException          的HResult = -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()               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)               在Microsoft.Rtc.Management.LyncOnlineConnector.GetWebTicketCmdlet.BeginProcessing()               在System.Management.Automation.Cmdlet.DoBeginProcessing()               在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.
                }
            }
        }

    }

1 个答案:

答案 0 :(得分:0)

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