1

I have following c# code that will call a PowerShell script to get Lync users.

But the line below throws an exception:

powerShellInstance.EndInvoke(result); 

Exception:

[2018-11-21 12:12:10] - [ERROR] – System.Management.Automation.RemoteException: Value cannot be null. Parameter name: source at System.Management.Automation.Runspaces.AsyncResult.EndInvoke() at System.Management.Automation.PowerShell.EndInvoke(IAsyncResult asyncResult) at O365Management.PowershellWrapper.RunPowerShellScript(String userName, String plainPassword, PowerShellScriptType powerShellScriptType, String groupId) at O365Management.PowershellWrapper.GetLyncUsers(String userName, String plainPassword)

Any suggestion?

System.Management.Automation.RemoteException: 
Value cannot be null. 
Parameter name: source at System.Management.Automation.Runspaces.AsyncResult.EndInvoke()
at System.Management.Automation.PowerShell.EndInvoke(IAsyncResult asyncResult)


using (PowerShellProcessInstance instance = new PowerShellProcessInstance(new Version(4, 0), null, null, false))
        {
            using (var runspace =
                RunspaceFactory.CreateOutOfProcessRunspace(new TypeTable(new string[0]), instance))
            {
                runspace.Open();
                using (PowerShell powerShellInstance = PowerShell.Create())
                {
                    powerShellInstance.Runspace = runspace;

                    var filePath =
                        "c:\\GetLyncUsers.ps1"; // this script has a method, which call method of includeScript.ps1
                    powerShellInstance.Commands.AddScript(File.ReadAllText(filePath));

                    var includeScript = "c:\\includeScript.ps1";
                    powerShellInstance.AddParameters(new List<string>
                    {
                        userName,
                        plainPassword,
                        includeScript
                    });


                    PSDataCollection<PSObject> psOutput = new PSDataCollection<PSObject>();
                    IAsyncResult result = powerShellInstance.BeginInvoke<PSObject, PSObject>(null, psOutput);

                    int allowedIteration = (int)TimeSpan.FromMinutes(20).TotalSeconds / 5;
                    var noOfIteration = 0;
                    do
                    {
                        noOfIteration++;
                        Thread.Sleep(TimeSpan.FromSeconds(5));
                    } while (noOfIteration <= allowedIteration && !result.IsCompleted);

                    if (!result.IsCompleted)
                    {
                        throw new Exception(string.Format("Timed out. Killing powershell instance... Username: {0}",
                            userName));
                    }

                    powerShellInstance.EndInvoke(result);

                    return psOutput;
                }
            }
        }
jazb
  • 5,498
  • 6
  • 37
  • 44
Prakash
  • 422
  • 1
  • 12
  • 31
  • what was the full exception? – jazb Nov 23 '18 at 07:27
  • [2018-11-21 12:12:10] - [ERROR] – System.Management.Automation.RemoteException: Value cannot be null. Parameter name: source at System.Management.Automation.Runspaces.AsyncResult.EndInvoke() at System.Management.Automation.PowerShell.EndInvoke(IAsyncResult asyncResult) at O365Management.PowershellWrapper.RunPowerShellScript(String userName, String plainPassword, PowerShellScriptType powerShellScriptType, String groupId) at O365Management.PowershellWrapper.GetLyncUsers(String userName, String plainPassword) – Prakash Nov 23 '18 at 08:30
  • looks like `EndInvoke` is being called with `null` to me... – jazb Nov 23 '18 at 09:37
  • Thanks John. When we ran the same script to get lync users from powershell and got same error so I think, the issue is not in the C# code. It looks there is some issue on the power shell script but our admin told me that it was working before so it looks like there is some configuration change or windows updates on the server. – Prakash Nov 23 '18 at 10:47

0 Answers0