Ejemplo n.º 1
0
        internal object CallPowerShell(PsRequest request, params object[] args)
        {
            // the lock ensures that we're not re-entrant into the same powershell runspace
            lock (_lock) {
                if (!_reentrancyLock.WaitOne(0))
                {
                    // this lock is set to false, meaning we're still in a call **ON THIS THREAD**
                    // this is bad karma -- powershell won't let us call into the runspace again
                    // we're going to throw an error here because this indicates that the currently
                    // running powershell call is calling back into OneGet, and it has called back
                    // into this provider. That's just bad bad bad.
                    throw new Exception("Re-entrancy Violation in powershell module");
                }

                try {
                    // otherwise, this is the first time we've been here during this call.
                    _reentrancyLock.Reset();

                    _powershell["request"] = request;

                    DynamicPowershellResult result = null;

                    // request.Debug("INVOKING PowerShell Fn {0} in {1}", request.CommandInfo.Name, _module.Name);
                    // make sure we don't pass the request to the function.
                    result = _powershell.NewTryInvokeMemberEx(request.CommandInfo.Name, new string[0], args);

                    // instead, loop thru results and get
                    if (result == null)
                    {
                        // failure!
                        throw new Exception(Messages.PowershellScriptFunctionFailed.format(_module.Name, request.CommandInfo.Name));
                    }

                    object finalValue = null;

                    foreach (var value in result)
                    {
                        if (result.ContainsErrors)
                        {
                            ReportErrors(request, result.Errors);
                            return(null);
                        }

                        var y = value as Yieldable;
                        if (y != null)
                        {
                            y.YieldResult(request);
                        }
                        else
                        {
                            finalValue = value;
                        }
                    }

                    if (result.ContainsErrors)
                    {
                        ReportErrors(request, result.Errors);
                        return(null);
                    }

                    return(finalValue);
                } catch (Exception e) {
                    e.Dump();
                } finally {
                    _powershell.WaitForAvailable();
                    _powershell["request"] = null;

                    // it's ok if someone else calls into this module now.
                    _reentrancyLock.Set();
                }



                return(null);
            }
        }