Пример #1
0
        internal CmdletProvider CreateInstance()
        {
            object    obj2           = null;
            Exception innerException = null;

            try
            {
                obj2 = Activator.CreateInstance(this.ImplementingType);
            }
            catch (TargetInvocationException exception2)
            {
                innerException = exception2.InnerException;
            }
            catch (MissingMethodException)
            {
            }
            catch (MemberAccessException)
            {
            }
            catch (ArgumentException)
            {
            }
            if (obj2 == null)
            {
                ProviderNotFoundException exception3 = null;
                if (innerException != null)
                {
                    exception3 = new ProviderNotFoundException(this.Name, SessionStateCategory.CmdletProvider, "ProviderCtorException", SessionStateStrings.ProviderCtorException, new object[] { innerException.Message });
                }
                else
                {
                    exception3 = new ProviderNotFoundException(this.Name, SessionStateCategory.CmdletProvider, "ProviderNotFoundInAssembly", SessionStateStrings.ProviderNotFoundInAssembly, new object[0]);
                }
                throw exception3;
            }
            CmdletProvider provider = obj2 as CmdletProvider;

            provider.SetProviderInformation(this);
            return(provider);
        }
Пример #2
0
        /// <summary>
        /// Creates an instance of the provider.
        /// </summary>
        /// <returns>
        /// An instance of the provider or null if one could not be created.
        /// </returns>
        /// <exception cref="ProviderNotFoundException">
        /// If an instance of the provider could not be created because the
        /// type could not be found in the assembly.
        /// </exception>
        internal Provider.CmdletProvider CreateInstance()
        {
            // It doesn't really seem that using thread local storage to store an
            // instance of the provider is really much of a performance gain and it
            // still causes problems with the CmdletProviderContext when piping two
            // commands together that use the same provider.
            // get-child -filter a*.txt | get-content
            // This pipeline causes problems when using a cached provider instance because
            // the CmdletProviderContext gets changed when get-content gets called.
            // When get-content finishes writing content from the first output of get-child
            // get-child gets control back and writes out a FileInfo but the WriteObject
            // from get-content gets used because the CmdletProviderContext is still from
            // that cmdlet.
            // Possible solutions are to not cache the provider instance, or to maintain
            // a CmdletProviderContext stack in ProviderBase.  Each method invocation pushes
            // the current context and the last action of the method pops back to the
            // previous context.
#if USE_TLS
            // Next see if we already have an instance in thread local storage

            object providerInstance = Thread.GetData(instance);

            if (providerInstance == null)
            {
#else
            object providerInstance = null;
#endif
            // Finally create an instance of the class
            Exception invocationException = null;

            try
            {
                providerInstance =
                    Activator.CreateInstance(this.ImplementingType);
            }
            catch (TargetInvocationException targetException)
            {
                invocationException = targetException.InnerException;
            }
            catch (MissingMethodException)
            {
            }
            catch (MemberAccessException)
            {
            }
            catch (ArgumentException)
            {
            }
#if USE_TLS
                // cache the instance in thread local storage

                Thread.SetData(instance, providerInstance);
            }
#endif

            if (providerInstance == null)
            {
                ProviderNotFoundException e = null;

                if (invocationException != null)
                {
                    e =
                        new ProviderNotFoundException(
                            this.Name,
                            SessionStateCategory.CmdletProvider,
                            "ProviderCtorException",
                            SessionStateStrings.ProviderCtorException,
                            invocationException.Message);
                }
                else
                {
                    e =
                        new ProviderNotFoundException(
                            this.Name,
                            SessionStateCategory.CmdletProvider,
                            "ProviderNotFoundInAssembly",
                            SessionStateStrings.ProviderNotFoundInAssembly);
                }

                throw e;
            }

            Provider.CmdletProvider result = providerInstance as Provider.CmdletProvider;

            Dbg.Diagnostics.Assert(
                result != null,
                "DiscoverProvider should verify that the class is derived from CmdletProvider so this is just validation of that");

            result.SetProviderInformation(this);
            return result;
        }
Пример #3
0
 internal CmdletProvider CreateInstance()
 {
     object obj2 = null;
     Exception innerException = null;
     try
     {
         obj2 = Activator.CreateInstance(this.ImplementingType);
     }
     catch (TargetInvocationException exception2)
     {
         innerException = exception2.InnerException;
     }
     catch (MissingMethodException)
     {
     }
     catch (MemberAccessException)
     {
     }
     catch (ArgumentException)
     {
     }
     if (obj2 == null)
     {
         ProviderNotFoundException exception3 = null;
         if (innerException != null)
         {
             exception3 = new ProviderNotFoundException(this.Name, SessionStateCategory.CmdletProvider, "ProviderCtorException", SessionStateStrings.ProviderCtorException, new object[] { innerException.Message });
         }
         else
         {
             exception3 = new ProviderNotFoundException(this.Name, SessionStateCategory.CmdletProvider, "ProviderNotFoundInAssembly", SessionStateStrings.ProviderNotFoundInAssembly, new object[0]);
         }
         throw exception3;
     }
     CmdletProvider provider = obj2 as CmdletProvider;
     provider.SetProviderInformation(this);
     return provider;
 }
Пример #4
0
        internal Collection<ProviderInfo> GetProvider(PSSnapinQualifiedName providerName)
        {
            Collection<ProviderInfo> result = new Collection<ProviderInfo>();

            if (providerName == null)
            {
                ProviderNotFoundException e =
                    new ProviderNotFoundException(
                        providerName.ToString(),
                        SessionStateCategory.CmdletProvider,
                        "ProviderNotFound",
                        SessionStateStrings.ProviderNotFound);

                throw e;
            }

            // Get the provider from the providers container

            List<ProviderInfo> matchingProviders = null;

            if (!Providers.TryGetValue(providerName.ShortName, out matchingProviders))
            {
                // If the provider was not found, we may need to auto-mount it.
                SessionStateInternal.MountDefaultDrive(providerName.ShortName, ExecutionContext);

                if (!Providers.TryGetValue(providerName.ShortName, out matchingProviders))
                {
                    ProviderNotFoundException e =
                        new ProviderNotFoundException(
                            providerName.ToString(),
                            SessionStateCategory.CmdletProvider,
                            "ProviderNotFound",
                            SessionStateStrings.ProviderNotFound);

                    throw e;
                }
            }

            if (ExecutionContext.IsSingleShell && !String.IsNullOrEmpty(providerName.PSSnapInName))
            {
                // Be sure the PSSnapin/Module name matches

                foreach (ProviderInfo provider in matchingProviders)
                {
                    if (String.Equals(
                            provider.PSSnapInName,
                            providerName.PSSnapInName,
                           StringComparison.OrdinalIgnoreCase) ||
                        String.Equals(
                            provider.ModuleName,
                            providerName.PSSnapInName,
                            StringComparison.OrdinalIgnoreCase))
                    {
                        result.Add(provider);
                    }
                }
            }
            else
            {
                foreach (ProviderInfo provider in matchingProviders)
                {
                    result.Add(provider);
                }
            }
            return result;
        } // GetProvider
Пример #5
0
        /// <summary>
        /// Gets the provider of the specified name
        /// </summary>
        /// 
        /// <param name="name">
        /// The name of the provider to retrieve
        /// </param>
        /// 
        /// <returns>
        /// The provider of the given name
        /// </returns>
        /// 
        /// <exception cref="ArgumentException">
        /// If <paramref name="name"/> is null or empty.
        /// </exception>
        /// 
        /// <exception cref="ProviderNotFoundException">
        /// The provider with the specified <paramref name="name"/> 
        /// could not be found or the name was ambiguous.
        /// If the name is ambiguous then the PSSnapin qualified name must
        /// be specified.
        /// </exception>
        /// 
        internal ProviderInfo GetSingleProvider(string name)
        {
            Collection<ProviderInfo> matchingProviders = GetProvider(name);

            if (matchingProviders.Count != 1)
            {
                if (matchingProviders.Count == 0)
                {
                    ProviderNotFoundException e =
                        new ProviderNotFoundException(
                            name,
                            SessionStateCategory.CmdletProvider,
                            "ProviderNotFound",
                            SessionStateStrings.ProviderNotFound);

                    throw e;
                }
                else
                {
                    throw NewAmbiguousProviderName(name, matchingProviders);
                }
            }
            return matchingProviders[0];
        }
Пример #6
0
        } // IsProviderLoaded

        /// <summary>
        /// Gets the provider of the specified name
        /// </summary>
        /// 
        /// <param name="name">
        /// The name of the provider to retrieve
        /// </param>
        /// 
        /// <returns>
        /// The provider of the given name
        /// </returns>
        /// 
        /// <exception cref="ArgumentException">
        /// If <paramref name="name"/> is null or empty.
        /// </exception>
        /// 
        /// <exception cref="ProviderNotFoundException">
        /// The provider with the specified <paramref name="name"/> 
        /// could not be found.
        /// </exception>
        /// 
        internal Collection<ProviderInfo> GetProvider(string name)
        {
            if (String.IsNullOrEmpty(name))
            {
                throw PSTraceSource.NewArgumentException("name");
            }

            PSSnapinQualifiedName providerName = PSSnapinQualifiedName.GetInstance(name);

            if (providerName == null)
            {
                ProviderNotFoundException e =
                 new ProviderNotFoundException(
                     name,
                     SessionStateCategory.CmdletProvider,
                     "ProviderNotFoundBadFormat",
                     SessionStateStrings.ProviderNotFoundBadFormat);

                throw e;
            }
            return GetProvider(providerName);
        }
Пример #7
0
        /// <summary>
        /// Removes the provider of the given name.
        /// </summary>
        /// 
        /// <param name="providerName">
        /// The name of the provider to remove.
        /// </param>
        /// 
        /// <param name="force">
        /// Determines if the provider should be removed forcefully even if there were
        /// drives present or errors.
        /// </param>
        ///
        /// <param name="context">
        /// The context under which the command is being run.
        /// </param>
        /// 
        /// <error cref="ArgumentNullException">
        /// If <paramref name="providerName"/> is null.
        /// </error>
        /// 
        /// <error cref="SessionStateException">
        /// There are still drives associated with this provider,
        /// and the "force" option was not specified.
        /// </error>
        /// 
        /// <error cref="ProviderNotFoundException">
        /// A provider with name <paramref name="providerName"/> could not be found.
        /// </error>
        /// 
        /// <error>
        /// If a provider throws an exception it gets written to the <paramref name="context"/>.
        /// </error>
        /// 
        /// <exception cref="ArgumentException">
        /// If <paramref name="providerName"/> is null or empty.
        /// </exception>
        /// 
        /// <exception cref="ArgumentNullException">
        /// If <paramref name="context"/> is null.
        /// </exception>
        /// 
        /// <remarks>
        /// All drives associated with the provider must be removed before the provider
        /// can be removed. Call SessionState.GetDrivesForProvider() to determine if there
        /// are any drives associated with the provider. A SessionStateException
        /// will be written to the context if any such drives do exist.
        /// </remarks>
        /// 
        internal void RemoveProvider(
            string providerName,
            bool force,
            CmdletProviderContext context)
        {
            if (context == null)
            {
                throw PSTraceSource.NewArgumentNullException("context");
            }

            if (String.IsNullOrEmpty(providerName))
            {
                throw PSTraceSource.NewArgumentException("providerName");
            }

            bool errors = false;

            ProviderInfo provider = null;

            try
            {
                provider = GetSingleProvider(providerName);
            }
            catch (ProviderNotFoundException)
            {
                return;
            }

            try
            {
                // First get an instance of the provider to make sure it exists
                Provider.CmdletProvider providerBase = GetProviderInstance(provider);

                if (providerBase == null)
                {
                    ProviderNotFoundException e = new ProviderNotFoundException(
                        providerName,
                        SessionStateCategory.CmdletProvider,
                        "ProviderNotFound",
                        SessionStateStrings.ProviderNotFound);
                    context.WriteError(new ErrorRecord(e.ErrorRecord, e));

                    errors = true;
                }
                else
                {
                    // See if there are any drives present for the provider

                    int driveCount = 0;
                    foreach (PSDriveInfo drive in GetDrivesForProvider(providerName))
                    {
                        if (drive != null)
                        {
                            ++driveCount;
                            break;
                        }
                    }

                    if (driveCount > 0)
                    {
                        if (force)
                        {
                            // Forcefully remove all the drives

                            foreach (PSDriveInfo drive in GetDrivesForProvider(providerName))
                            {
                                if (drive != null)
                                {
                                    RemoveDrive(drive, true, null);
                                }
                            }
                        }
                        else
                        {
                            errors = true;

                            // Since there are still drives associated with the provider
                            // the provider cannot be removed

                            SessionStateException e = new SessionStateException(
                                providerName,
                                SessionStateCategory.CmdletProvider,
                                "RemoveDrivesBeforeRemovingProvider",
                                SessionStateStrings.RemoveDrivesBeforeRemovingProvider,
                                ErrorCategory.InvalidOperation);
                            context.WriteError(new ErrorRecord(e.ErrorRecord, e));

                            return;
                        }
                    }

                    // Now tell the provider that they are going to be removed by
                    // calling the Stop method

                    try
                    {
                        providerBase.Stop(context);
                    }
                    catch (LoopFlowException)
                    {
                        throw;
                    }
                    catch (PipelineStoppedException)
                    {
                        throw;
                    }
                    catch (ActionPreferenceStopException)
                    {
                        throw;
                    }
                }
            }
            catch (LoopFlowException)
            {
                throw;
            }
            catch (PipelineStoppedException)
            {
                throw;
            }
            catch (ActionPreferenceStopException)
            {
                throw;
            }
            catch (Exception e)
            {
                CommandProcessorBase.CheckForSevereException(e);
                errors = true;
                context.WriteError(
                    new ErrorRecord(
                        e,
                        "RemoveProviderUnexpectedException",
                        ErrorCategory.InvalidArgument,
                        providerName));
            }
            finally
            {
                if (force || !errors)
                {
                    // Log the provider stopped event

                    MshLog.LogProviderLifecycleEvent(
                        this.ExecutionContext,
                        providerName,
                        ProviderState.Stopped);

                    RemoveProviderFromCollection(provider);
                    ProvidersCurrentWorkingDrive.Remove(provider);

#if RELATIONSHIP_SUPPORTED
    // 2004/11/24-JeffJon - Relationships have been removed from the Exchange release

                   // Now make sure no relationship reference this provider
                    relationships.ProcessRelationshipsOnCmdletProviderRemoval (providerName);
#endif
                }
            }
        } // RemoveProvider
Пример #8
0
        /// <summary>
        /// Creates an instance of the provider
        /// </summary>
        /// 
        /// <returns>
        /// An instance of the provider or null if one could not be created.
        /// </returns>
        /// 
        /// <exception cref="ProviderNotFoundException">
        /// If an instance of the provider could not be created because the
        /// type could not be found in the assembly.
        /// </exception>
        /// 
        internal Provider.CmdletProvider CreateInstance()
        {
            // It doesn't really seem that using thread local storage to store an
            // instance of the provider is really much of a performance gain and it
            // still causes problems with the CmdletProviderContext when piping two
            // commands together that use the same provider.
            // get-child -filter a*.txt | get-content
            // This pipeline causes problems when using a cached provider instance because
            // the CmdletProviderContext gets changed when get-content gets called. 
            // When get-content finishes writing content from the first output of get-child
            // get-child gets control back and writes out a FileInfo but the WriteObject
            // from get-content gets used because the CmdletProviderContext is still from
            // that cmdlet.
            // Possible solutions are to not cache the provider instance, or to maintain
            // a CmdletProviderContext stack in ProviderBase.  Each method invocation pushes
            // the current context and the last action of the method pops back to the
            // previous context.
#if USE_TLS
            // Next see if we already have an instance in thread local storage

            object providerInstance = Thread.GetData(instance);

            if (providerInstance == null)
            {
#else
            object providerInstance = null;
#endif
            // Finally create an instance of the class
            Exception invocationException = null;

            try
            {
                providerInstance =
                    Activator.CreateInstance(this.ImplementingType);
            }
            catch (TargetInvocationException targetException)
            {
                invocationException = targetException.InnerException;
            }
            catch (MissingMethodException)
            {
            }
            catch (MemberAccessException)
            {
            }
            catch (ArgumentException)
            {
            }
#if USE_TLS
                // cache the instance in thread local storage

                Thread.SetData(instance, providerInstance);
            }
#endif

            if (providerInstance == null)
            {
                ProviderNotFoundException e = null;

                if (invocationException != null)
                {
                    e =
                        new ProviderNotFoundException(
                            this.Name,
                            SessionStateCategory.CmdletProvider,
                            "ProviderCtorException",
                            SessionStateStrings.ProviderCtorException,
                            invocationException.Message);
                }
                else
                {
                    e =
                        new ProviderNotFoundException(
                            this.Name,
                            SessionStateCategory.CmdletProvider,
                            "ProviderNotFoundInAssembly",
                            SessionStateStrings.ProviderNotFoundInAssembly);
                }
                throw e;
            }

            Provider.CmdletProvider result = providerInstance as Provider.CmdletProvider;

            Dbg.Diagnostics.Assert(
                result != null,
                "DiscoverProvider should verify that the class is derived from CmdletProvider so this is just validation of that");

            result.SetProviderInformation(this);
            return result;
        }
Пример #9
0
 internal void RemoveProvider(string providerName, bool force, CmdletProviderContext context)
 {
     if (context == null)
     {
         throw PSTraceSource.NewArgumentNullException("context");
     }
     if (string.IsNullOrEmpty(providerName))
     {
         throw PSTraceSource.NewArgumentException("providerName");
     }
     bool flag = false;
     ProviderInfo singleProvider = null;
     try
     {
         singleProvider = this.GetSingleProvider(providerName);
     }
     catch (ProviderNotFoundException)
     {
         return;
     }
     try
     {
         CmdletProvider providerInstance = this.GetProviderInstance(singleProvider);
         if (providerInstance == null)
         {
             ProviderNotFoundException replaceParentContainsErrorRecordException = new ProviderNotFoundException(providerName, SessionStateCategory.CmdletProvider, "ProviderNotFound", SessionStateStrings.ProviderNotFound, new object[0]);
             context.WriteError(new ErrorRecord(replaceParentContainsErrorRecordException.ErrorRecord, replaceParentContainsErrorRecordException));
             flag = true;
         }
         else
         {
             int num = 0;
             foreach (PSDriveInfo info2 in this.GetDrivesForProvider(providerName))
             {
                 if (info2 != null)
                 {
                     num++;
                     break;
                 }
             }
             if (num > 0)
             {
                 if (force)
                 {
                     foreach (PSDriveInfo info3 in this.GetDrivesForProvider(providerName))
                     {
                         if (info3 != null)
                         {
                             this.RemoveDrive(info3, true, null);
                         }
                     }
                 }
                 else
                 {
                     flag = true;
                     SessionStateException exception2 = new SessionStateException(providerName, SessionStateCategory.CmdletProvider, "RemoveDrivesBeforeRemovingProvider", SessionStateStrings.RemoveDrivesBeforeRemovingProvider, ErrorCategory.InvalidOperation, new object[0]);
                     context.WriteError(new ErrorRecord(exception2.ErrorRecord, exception2));
                     return;
                 }
             }
             try
             {
                 providerInstance.Stop(context);
             }
             catch (LoopFlowException)
             {
                 throw;
             }
             catch (PipelineStoppedException)
             {
                 throw;
             }
             catch (ActionPreferenceStopException)
             {
                 throw;
             }
         }
     }
     catch (LoopFlowException)
     {
         throw;
     }
     catch (PipelineStoppedException)
     {
         throw;
     }
     catch (ActionPreferenceStopException)
     {
         throw;
     }
     catch (Exception exception3)
     {
         CommandProcessorBase.CheckForSevereException(exception3);
         flag = true;
         context.WriteError(new ErrorRecord(exception3, "RemoveProviderUnexpectedException", ErrorCategory.InvalidArgument, providerName));
     }
     finally
     {
         if (force || !flag)
         {
             MshLog.LogProviderLifecycleEvent(this.ExecutionContext, providerName, ProviderState.Stopped);
             this.RemoveProviderFromCollection(singleProvider);
             this.ProvidersCurrentWorkingDrive.Remove(singleProvider);
         }
     }
 }
Пример #10
0
 internal ProviderInfo GetSingleProvider(string name)
 {
     Collection<ProviderInfo> provider = this.GetProvider(name);
     if (provider.Count == 1)
     {
         return provider[0];
     }
     if (provider.Count == 0)
     {
         ProviderNotFoundException exception = new ProviderNotFoundException(name, SessionStateCategory.CmdletProvider, "ProviderNotFound", SessionStateStrings.ProviderNotFound, new object[0]);
         throw exception;
     }
     throw NewAmbiguousProviderName(name, provider);
 }
Пример #11
0
        internal Collection<ProviderInfo> GetProvider(PSSnapinQualifiedName providerName)
        {
            Collection<ProviderInfo> collection = new Collection<ProviderInfo>();
            if (providerName == null)
            {
                ProviderNotFoundException exception = new ProviderNotFoundException(providerName.ToString(), SessionStateCategory.CmdletProvider, "ProviderNotFound", SessionStateStrings.ProviderNotFound, new object[0]);
                throw exception;
            }
			string pName = null;
            List<ProviderInfo> list = null;
            if (!this.Providers.TryGetValue(providerName.ShortName, out list))
            {
                MountDefaultDrive(providerName.ShortName, this._context);
				pName = (OSHelper.IsUnix && providerName.ShortName == "/") ? "FileSystem" : providerName.ShortName;
				if (pName == "env") { pName = "Environment"; }
				else if (pName == "cert") { pName = "Certificate"; }
				else if (pName == "reg") { pName = "Registry"; }
                if (!this.Providers.TryGetValue(pName, out list))
                {
                    ProviderNotFoundException exception2 = new ProviderNotFoundException(providerName.ToString(), SessionStateCategory.CmdletProvider, "ProviderNotFound", SessionStateStrings.ProviderNotFound, new object[0]);
                    throw exception2;
                }
            }
			if (string.IsNullOrEmpty (pName)) pName = providerName.PSSnapInName;
            if (this.ExecutionContext.IsSingleShell && !string.IsNullOrEmpty(providerName.PSSnapInName))
            {
                foreach (ProviderInfo info in list)
                {
                    if (string.Equals(info.PSSnapInName, pName, StringComparison.OrdinalIgnoreCase) || string.Equals(info.ModuleName, pName, StringComparison.OrdinalIgnoreCase))
                    {
                        collection.Add(info);
                    }
                }
                return collection;
            }
            foreach (ProviderInfo info2 in list)
            {
                collection.Add(info2);
            }
            return collection;
        }