System.Management.Automation.RemoteException возникла при создании сценария powerhshell для получения пользователей lync - PullRequest
0 голосов
/ 23 ноября 2018

У меня есть следующий код c #, который будет вызывать скрипт PowerShell для получения пользователей Lync.

Но строка ниже выдает исключение:

powerShellInstance.EndInvoke(result); 

Исключение:

[2018-11-21 12:12:10] - [ОШИБКА] - System.Management.Automation.RemoteException: значение не может быть нулевым.Имя параметра: источник в System.Management.Automation.Runspaces.AsyncResult.EndInvoke () в System.Management.Automation.PowerShell.EndInvoke (IAsyncResult asyncResult) в O365Management.PowershellWrapper.RunPowerShellPlayShellPlayShellPlayShellShellShellShellShellShellShellShellScripting) в O365Management.PowershellWrapper.GetLyncUsers (String userName, String plainPassword)

Любое предложение?

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