0
votes

I have the code which tries to get a lync session running powershell script form c#. When trying to run the script "$CSSession = New-CsOnlineSession -Credential $cred\n" I am getting null reference exception.

Stack Trace:

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:

Code is :

    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

1 Answers

0
votes

I was told my MS support that invoking Lync Online PowerShell script from C# is currently not supported.