コード例 #1
0
        // lock is on this instance only

        internal void ReportErrors(PsRequest request, IEnumerable <ErrorRecord> errors)
        {
            foreach (var error in errors)
            {
                request.Error(error.FullyQualifiedErrorId, error.CategoryInfo.Category.ToString(), error.TargetObject == null ? null : error.TargetObject.ToString(), error.ErrorDetails == null ? error.Exception.Message : error.ErrorDetails.Message);
                if (!string.IsNullOrWhiteSpace(error.ScriptStackTrace))
                {
                    // give a debug hint if we have a script stack trace. How nice of us.
                    request.Debug(Constants.ScriptStackTrace, error.ScriptStackTrace);
                }
            }
        }
コード例 #2
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 PM, 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.SetVariable("request", request);
                    _powershell.Streams.ClearStreams();

                    // request.Debug("INVOKING PowerShell Fn {0} in {1}", request.CommandInfo.Name, _module.Name);
                    // make sure we don't pass the request to the function.
                    var result = _powershell.InvokeFunction<object>(request.CommandInfo.Name, 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 (_powershell.Streams.Error.Any()) {
                            ReportErrors(request, _powershell.Streams.Error);
                            _powershell.Streams.Error.Clear();
                        }

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

                    if (_powershell.Streams.Error.Any()) {
                        ReportErrors(request, _powershell.Streams.Error);
                        _powershell.Streams.Error.Clear();
                    }

                    return finalValue;
                } catch (CmdletInvocationException cie) {
                    var error = cie.ErrorRecord;
                    request.Error(error.FullyQualifiedErrorId, error.CategoryInfo.Category.ToString(), error.TargetObject == null ? null : error.TargetObject.ToString(), error.ErrorDetails == null ? error.Exception.Message : error.ErrorDetails.Message);
                }catch (Exception e) {
                    e.Dump();
                } finally {
                    lock (_stopLock) {
                        if (_stopResult != null){
                            _powershell.EndStop(_stopResult);
                            _stopResult = null;
                        }
                    }
                    _powershell.Clear();
                    _powershell.SetVariable("request", null);
                    // it's ok if someone else calls into this module now.
                    _reentrancyLock.Set();
                }

                return null;
            }
        }
コード例 #3
0
 // lock is on this instance only
 internal void ReportErrors(PsRequest request, IEnumerable<ErrorRecord> errors)
 {
     foreach (var error in errors) {
         request.Error(error.FullyQualifiedErrorId, error.CategoryInfo.Category.ToString(), error.TargetObject == null ? null : error.TargetObject.ToString(), error.ErrorDetails == null ? error.Exception.Message : error.ErrorDetails.Message);
         if (!string.IsNullOrWhiteSpace(error.ScriptStackTrace)) {
             // give a debug hint if we have a script stack trace. How nice of us.
             request.Debug(Constants.ScriptStackTrace, error.ScriptStackTrace);
         }
     }
 }
コード例 #4
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 PM, 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.SetVariable("request", request);
                    _powershell.Streams.ClearStreams();

                    // request.Debug("INVOKING PowerShell Fn {0} in {1}", request.CommandInfo.Name, _module.Name);
                    // make sure we don't pass the request to the function.
                    var result = _powershell.InvokeFunction <object>(request.CommandInfo.Name, 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 (_powershell.Streams.Error.Any())
                        {
                            ReportErrors(request, _powershell.Streams.Error);
                            _powershell.Streams.Error.Clear();
                        }

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

                    if (_powershell.Streams.Error.Any())
                    {
                        ReportErrors(request, _powershell.Streams.Error);
                        _powershell.Streams.Error.Clear();
                    }

                    return(finalValue);
                } catch (CmdletInvocationException cie) {
                    var error = cie.ErrorRecord;
                    request.Error(error.FullyQualifiedErrorId, error.CategoryInfo.Category.ToString(), error.TargetObject == null ? null : error.TargetObject.ToString(), error.ErrorDetails == null ? error.Exception.Message : error.ErrorDetails.Message);
                }catch (Exception e) {
                    e.Dump();
                } finally {
                    lock (_stopLock) {
                        if (_stopResult != null)
                        {
                            _powershell.EndStop(_stopResult);
                            _stopResult = null;
                        }
                    }
                    _powershell.Clear();
                    _powershell.SetVariable("request", null);
                    // it's ok if someone else calls into this module now.
                    _reentrancyLock.Set();
                }

                return(null);
            }
        }