Пример #1
0
        public IEnumerable <object> LoadAvailableProvider(PsRequest request, string modulePath, Version requiredVersion, bool force)
        {
            if (request == null)
            {
                throw new ArgumentNullException("request");
            }

            if (string.IsNullOrEmpty(modulePath))
            {
                throw new ArgumentNullException("modulePath");
            }

            EnsurePowerShellInitialized();

            //Check if it is already in the cache table
            var providersAlreadyImported = FindMatchedProvidersFromInternalCacheTable(request, modulePath).ToArray();

            if (providersAlreadyImported.Any() && !force)
            {
                return(providersAlreadyImported);
            }

            //Trying to load it from the path directly
            var pkgProvider = AnalyzeModule(request, modulePath, requiredVersion ?? new Version(0, 0), force);

            if (pkgProvider != null)
            {
                return(new[] { pkgProvider }.WhereNotNull());
            }

            request.Error(PackageManagement.Internal.Constants.Messages.FailedToImportProvider,
                          ErrorCategory.InvalidOperation.ToString(), PowerShellGet, string.Format(CultureInfo.CurrentCulture, Resources.Messages.FailedToImportProvider, modulePath));
            return(Enumerable.Empty <PackageProvider>());
        }
        // 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.Exception.StackTrace))
                {
                    // give a debug hint if we have a script stack trace. How nice of us.
                    // the exception stack trace gives better stack than the script stack trace
                    request.Debug(Constants.ScriptStackTrace, error.Exception.StackTrace);
                }
            }
        }
Пример #3
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("Reentrancy 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();

                    object finalValue = null;
                    ConcurrentBag<ErrorRecord> errors = new ConcurrentBag<ErrorRecord>();

                    request.Debug("INVOKING PowerShell Fn {0} with args {1} that has length {2}", request.CommandInfo.Name, String.Join(", ", args), args.Length);

                    var result = _powershell.InvokeFunction<object>(request.CommandInfo.Name,
                        (sender, e) => output_DataAdded(sender, e, request, ref finalValue),
                        (sender, e) => error_DataAdded(sender, e, request, errors),
                        args);

                    if (result == null)
                    {
                        // result is null but it does not mean that the call fails because the command may return nothing
                        request.Debug(Messages.PowershellScriptFunctionReturnsNull.format(_module.Name, request.CommandInfo.Name));
                    }

                    if (errors.Count > 0)
                    {
                        // report the error if there are any
                        ReportErrors(request, errors);
                        _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);
                } 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.
                    request.Debug("Done calling powershell", request.CommandInfo.Name, _module.Name);
                    _reentrancyLock.Set();
                }

                return null;
            }
        }
Пример #4
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.Exception.StackTrace)) {
                    // give a debug hint if we have a script stack trace. How nice of us.
                    // the exception stack trace gives better stack than the script stack trace
                    request.Debug(Constants.ScriptStackTrace, error.Exception.StackTrace);
                }
            }
        }
Пример #5
0
        public IEnumerable<object> LoadAvailableProvider(PsRequest request, string modulePath, Version requiredVersion, bool force) {
            if (request == null) {
                throw new ArgumentNullException("request");
            }

            if (string.IsNullOrEmpty(modulePath)) {
                throw new ArgumentNullException("modulePath");
            }

            EnsurePowerShellInitialized();

            //Check if it is already in the cache table
            var providersAlreadyImported = FindMatchedProvidersFromInternalCacheTable(request, modulePath).ToArray();

            if (providersAlreadyImported.Any() && !force) {
                return providersAlreadyImported;
            }

            //Trying to load it from the path directly
            var pkgProvider = AnalyzeModule(request, modulePath, requiredVersion ?? new Version(0, 0), force);
            if (pkgProvider != null) {
                return new[] { pkgProvider }.WhereNotNull();
            }

            request.Error(PackageManagement.Internal.Constants.Messages.FailedToImportProvider,
                ErrorCategory.InvalidOperation.ToString(), PowerShellGet, string.Format(CultureInfo.CurrentCulture, Resources.Messages.FailedToImportProvider, modulePath));
            return Enumerable.Empty<PackageProvider>();
        }
        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("Reentrancy 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();

                    object finalValue = null;
                    ConcurrentBag <ErrorRecord> errors = new ConcurrentBag <ErrorRecord>();

                    request.Debug("INVOKING PowerShell Fn {0} with args {1} that has length {2}", request.CommandInfo.Name, String.Join(", ", args), args.Length);

                    var result = _powershell.InvokeFunction <object>(request.CommandInfo.Name,
                                                                     (sender, e) => output_DataAdded(sender, e, request, ref finalValue),
                                                                     (sender, e) => error_DataAdded(sender, e, request, errors),
                                                                     args);

                    if (result == null)
                    {
                        // result is null but it does not mean that the call fails because the command may return nothing
                        request.Debug(Messages.PowershellScriptFunctionReturnsNull.format(_module.Name, request.CommandInfo.Name));
                    }

                    if (errors.Count > 0)
                    {
                        // report the error if there are any
                        ReportErrors(request, errors);
                        _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);
                } 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.
                    request.Debug("Done calling powershell", request.CommandInfo.Name, _module.Name);
                    _reentrancyLock.Set();
                }

                return(null);
            }
        }