Ejemplo n.º 1
0
 private static void WriteErrorOrWarning(bool writeErrorOnException, Cmdlet cmdlet, Exception exception, string identifier, JobSourceAdapter sourceAdapter)
 {
     try
     {
         if (writeErrorOnException)
         {
             cmdlet.WriteError(new ErrorRecord(exception, identifier, ErrorCategory.OpenError, sourceAdapter));
         }
         else
         {
             // Write a warning
             string message = string.Format(CultureInfo.CurrentCulture,
                                            RemotingErrorIdStrings.JobSourceAdapterError,
                                            exception.Message,
                                            sourceAdapter.Name);
             cmdlet.WriteWarning(message);
         }
     }
     catch (Exception)
     {
         // if this call is not made from a cmdlet thread or if
         // the cmdlet is closed this will thrown an exception
         // it is fine to eat that exception
     }
 }
Ejemplo n.º 2
0
 private static void HandleErrorFromPipeline(Cmdlet cmdlet, ErrorRecord errorRecord, PowerShell powerShell)
 {
     if (!cmdlet.MyInvocation.ExpectingInput && (((powerShell.Runspace != null) && (powerShell.Runspace.RunspaceStateInfo.State != RunspaceState.Opened)) || ((powerShell.RunspacePool != null) && (powerShell.RunspacePool.RunspacePoolStateInfo.State != RunspacePoolState.Opened))))
     {
         cmdlet.ThrowTerminatingError(errorRecord);
     }
     cmdlet.WriteError(errorRecord);
 }
Ejemplo n.º 3
0
 private void WriteError(Cmdlet cmdlet, ErrorRecord errorRecord)
 {
     if (this.PropagateThrows)
     {
         Exception exceptionFromErrorRecord = GetExceptionFromErrorRecord(errorRecord);
         if (exceptionFromErrorRecord != null)
         {
             throw exceptionFromErrorRecord;
         }
     }
     errorRecord.PreserveInvocationInfoOnce = true;
     cmdlet.WriteError(errorRecord);
 }
        } // WriteObject

        /// <summary>
        /// Writes the error to the pipeline or accumulates the error in an internal
        /// buffer.
        /// </summary>
        /// <param name="errorRecord">
        /// The error record to write to the pipeline or the internal buffer.
        /// </param>
        /// <exception cref="InvalidOperationException">
        /// The CmdletProvider could not stream the error because no
        /// cmdlet was specified to stream the output through.
        /// </exception>
        /// <exception cref="PipelineStoppedException">
        /// If the pipeline has been signaled for stopping but
        /// the provider calls this method.
        /// </exception>
        internal void WriteError(ErrorRecord errorRecord)
        {
            // Making sure to obey the StopProcessing by
            // throwing an exception anytime a provider tries
            // to WriteError

            if (Stopping)
            {
                PipelineStoppedException stopPipeline =
                    new PipelineStoppedException();

                throw stopPipeline;
            }

            if (_streamErrors)
            {
                if (_command != null)
                {
                    s_tracer.WriteLine("Writing error package to command error pipe");

                    _command.WriteError(errorRecord);
                }
                else
                {
                    InvalidOperationException e =
                        PSTraceSource.NewInvalidOperationException(
                            SessionStateStrings.ErrorStreamingNotEnabled);
                    throw e;
                }
            }
            else
            {
                // Since we are not streaming, just add the object to the accumulatedErrorObjects
                _accumulatedErrorObjects.Add(errorRecord);

                if (errorRecord.ErrorDetails != null &&
                    errorRecord.ErrorDetails.TextLookupError != null)
                {
                    Exception textLookupError = errorRecord.ErrorDetails.TextLookupError;
                    errorRecord.ErrorDetails.TextLookupError = null;
                    MshLog.LogProviderHealthEvent(
                        this.ExecutionContext,
                        this.ProviderInstance.ProviderInfo.Name,
                        textLookupError,
                        Severity.Warning);
                }
            }
        } // WriteError
Ejemplo n.º 5
0
 private static void WriteErrorOrWarning(bool writeErrorOnException, Cmdlet cmdlet, Exception exception, string identifier, JobSourceAdapter sourceAdapter)
 {
     try
     {
         if (writeErrorOnException)
         {
             cmdlet.WriteError(new ErrorRecord(exception, identifier, ErrorCategory.OpenError, sourceAdapter));
         }
         else
         {
             cmdlet.WriteWarning(ResourceManagerCache.FormatResourceString("RemotingErrorIdStrings", "JobSourceAdapterError", new object[] { exception.Message, sourceAdapter.Name }));
         }
     }
     catch (Exception)
     {
     }
 }
Ejemplo n.º 6
0
        private static IEnumerable <CimModule> GetCimModules(CimSession cimSession, Uri resourceUri, string cimNamespace, string moduleNamePattern, bool onlyManifests, Cmdlet cmdlet, CancellationToken cancellationToken)
        {
            Func <CimModule, CimModule> selector        = null;
            WildcardPattern             wildcardPattern = new WildcardPattern(moduleNamePattern, WildcardOptions.CultureInvariant | WildcardOptions.IgnoreCase);
            string optionValue          = WildcardPatternToDosWildcardParser.Parse(wildcardPattern);
            CimOperationOptions options = new CimOperationOptions {
                CancellationToken = new CancellationToken?(cancellationToken)
            };

            options.SetCustomOption("PS_ModuleNamePattern", optionValue, false);
            if (resourceUri != null)
            {
                options.ResourceUri = resourceUri;
            }
            if (string.IsNullOrEmpty(cimNamespace) && (resourceUri == null))
            {
                cimNamespace = "root/Microsoft/Windows/Powershellv3";
            }
            IEnumerable <CimModule> source = from cimInstance in cimSession.EnumerateInstances(cimNamespace, "PS_Module", options)
                                             select new CimModule(cimInstance) into cimModule
                                             where wildcardPattern.IsMatch(cimModule.ModuleName)
                                             select cimModule;

            if (!onlyManifests)
            {
                if (selector == null)
                {
                    selector = delegate(CimModule cimModule) {
                        cimModule.FetchAllModuleFiles(cimSession, cimNamespace, options);
                        return(cimModule);
                    };
                }
                source = source.Select <CimModule, CimModule>(selector);
            }
            return(EnumerateWithCatch <CimModule>(source, delegate(Exception exception) {
                ErrorRecord errorRecord = GetErrorRecordForRemoteDiscoveryProvider(exception);
                if (!cmdlet.MyInvocation.ExpectingInput && (((-1 != errorRecord.FullyQualifiedErrorId.IndexOf("DiscoveryProviderNotFound", StringComparison.OrdinalIgnoreCase)) || cancellationToken.IsCancellationRequested) || ((exception is OperationCanceledException) || !cimSession.TestConnection())))
                {
                    cmdlet.ThrowTerminatingError(errorRecord);
                }
                cmdlet.WriteError(errorRecord);
            }));
        }
Ejemplo n.º 7
0
        private static IEnumerable <CimModule> GetCimModules(
            CimSession cimSession,
            Uri resourceUri,
            string cimNamespace,
            string moduleNamePattern,
            bool onlyManifests,
            Cmdlet cmdlet,
            CancellationToken cancellationToken)
        {
            Dbg.Assert(cimSession != null, "Caller should verify cimSession != null");
            Dbg.Assert(moduleNamePattern != null, "Caller should verify that moduleNamePattern != null");

            const WildcardOptions wildcardOptions = WildcardOptions.IgnoreCase | WildcardOptions.CultureInvariant;
            var    wildcardPattern = WildcardPattern.Get(moduleNamePattern, wildcardOptions);
            string dosWildcard     = WildcardPatternToDosWildcardParser.Parse(wildcardPattern);

            var options = new CimOperationOptions {
                CancellationToken = cancellationToken
            };

            options.SetCustomOption("PS_ModuleNamePattern", dosWildcard, mustComply: false);
            if (resourceUri != null)
            {
                options.ResourceUri = resourceUri;
            }

            if (string.IsNullOrEmpty(cimNamespace) && (resourceUri == null))
            {
                cimNamespace = DiscoveryProviderNamespace;
            }

            // TODO/FIXME: ETW for method invocation
            IEnumerable <CimInstance> syncResults = cimSession.EnumerateInstances(
                cimNamespace,
                DiscoveryProviderModuleClass,
                options);
            // TODO/FIXME: ETW for method results
            IEnumerable <CimModule> cimModules = syncResults
                                                 .Select(cimInstance => new CimModule(cimInstance))
                                                 .Where(cimModule => wildcardPattern.IsMatch(cimModule.ModuleName));

            if (!onlyManifests)
            {
                cimModules = cimModules.Select(
                    delegate(CimModule cimModule)
                {
                    cimModule.FetchAllModuleFiles(cimSession, cimNamespace, options);
                    return(cimModule);
                });
            }

            return(EnumerateWithCatch(
                       cimModules,
                       delegate(Exception exception)
            {
                ErrorRecord errorRecord = GetErrorRecordForRemoteDiscoveryProvider(exception);
                if (!cmdlet.MyInvocation.ExpectingInput)
                {
                    if (((-1) != errorRecord.FullyQualifiedErrorId.IndexOf(DiscoveryProviderNotFoundErrorId, StringComparison.OrdinalIgnoreCase)) ||
                        (cancellationToken.IsCancellationRequested || (exception is OperationCanceledException)) ||
                        (!cimSession.TestConnection()))
                    {
                        cmdlet.ThrowTerminatingError(errorRecord);
                    }
                }
                cmdlet.WriteError(errorRecord);
            }));
        }