Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 尝试运行powershell脚本时Microsoft.Rtc.Admin.AuthenticationHelper中出现空引用异常_C#_Powershell - Fatal编程技术网

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

C# 尝试运行powershell脚本时Microsoft.Rtc.Admin.AuthenticationHelper中出现空引用异常,c#,powershell,C#,Powershell,我有一段代码,试图让lync会话运行powershell脚本表单c。尝试运行脚本$CSSession=New CsOnlineSession-Credential$cred\n时,我遇到空引用异常 堆栈跟踪: System.Management.Automation.CmdletInvocationException未处理 HResult=-2146233087 Message=对象引用未设置为实例 指一个物体。Source=系统管理自动化 WassThrownFromThrowStatemen

我有一段代码,试图让lync会话运行powershell脚本表单c。尝试运行脚本$CSSession=New CsOnlineSession-Credential$cred\n时,我遇到空引用异常

堆栈跟踪:

System.Management.Automation.CmdletInvocationException未处理 HResult=-2146233087 Message=对象引用未设置为实例 指一个物体。Source=系统管理自动化 WassThrownFromThrowStatement=假堆栈跟踪: 位于System.Management.Automation.Internal.PipelineProcessor.SynchronousExecuteEnumerateObject 输入,哈希表错误结果,布尔枚举 在System.Management.Automation.PipelineOps.InvokePipelineObject输入中, Boolean ignoreInput,CommandParameterInternal[][]pipeElements, CommandBaseAst[]pipeElementAsts,CommandRedirection[] 命令重定向,FunctionContext funcContext at系统。管理。自动化。解释器。ActionCallInstruction`6.RunExpressionFrame 框架 在System.Management.Automation.Transparer.EnterTryCatchFinallyInstruction.RunTransparedFrame 框架 在System.Management.Automation.Transparer.EnterTryCatchFinallyInstruction.RunTransparedFrame 帧内部异常:System.NullReferenceException HResult=-2147467261 Message=对象引用未设置为对象的实例。 Source=Microsoft.Rtc.Admin.AuthenticationHelper 堆栈跟踪: 在Microsoft.Rtc.Admin.Authentication.managedICRL..ctorIDCRLMode模式下 位于Microsoft.Rtc.Admin.Authentication.managedICRL..ctor 在Microsoft.Rtc.Management.LynchOnlineConnector.GetWebTicketCmdlet.CreateanInitializeManageDIDCRL 在Microsoft.Rtc.Management.LynchOnlineConnector.GetWebTicketCmdlet.get_ManagedIdcrl上 位于Microsoft.Rtc.Management.LyncOnlineConnector.GetWebTicketCmdlet.GetLiveIdTokenString remoteFqdn、Int32端口、PSCredential creds 位于Microsoft.Rtc.Management.LyncOnlineConnector.GetWebTicketCmdlet.ConnectToWebTicketServiceString fqdn,Int32端口,PSCredential creds 位于Microsoft.Rtc.Management.LyncOnlineConnector.GetWebTicketCmdlet.BeginProcessing 位于System.Management.Automation.Cmdlet.DoBeginProcess 位于System.Management.Automation.CommandProcessorBase.DoBegin 内部异常:

代码是:


我的MS支持人员告诉我,目前不支持从C调用Lync Online PowerShell脚本

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

    }