示例#1
0
        /// <summary>
        /// Gets the dynamic parameters for the clear-item cmdlet.
        /// </summary>
        /// <param name="path">
        /// The path to the item if it was specified on the command line.
        /// </param>
        /// <param name="context">
        /// The context which the core command is running.
        /// </param>
        /// <returns>
        /// An object that has properties and fields decorated with
        /// parsing attributes similar to a cmdlet class.
        /// </returns>
        /// <exception cref="ProviderNotFoundException">
        /// If the <paramref name="path"/> refers to a provider that could not be found.
        /// </exception>
        /// <exception cref="DriveNotFoundException">
        /// If the <paramref name="path"/> refers to a drive that could not be found.
        /// </exception>
        /// <exception cref="NotSupportedException">
        /// If the provider that the <paramref name="path"/> refers to does
        /// not support this operation.
        /// </exception>
        /// <exception cref="ProviderInvocationException">
        /// If the provider threw an exception.
        /// </exception>
        /// <exception cref="ItemNotFoundException">
        /// If <paramref name="path"/> does not contain glob characters and
        /// could not be found.
        /// </exception>
        internal object ClearItemDynamicParameters(string path, CmdletProviderContext context)
        {
            if (path == null)
            {
                return(null);
            }

            ProviderInfo   provider         = null;
            CmdletProvider providerInstance = null;

            CmdletProviderContext newContext =
                new CmdletProviderContext(context);

            newContext.SetFilters(
                new Collection <string>(),
                new Collection <string>(),
                null);

            Collection <string> providerPaths =
                Globber.GetGlobbedProviderPathsFromMonadPath(
                    path,
                    true,
                    newContext,
                    out provider,
                    out providerInstance);

            if (providerPaths.Count > 0)
            {
                // Get the dynamic parameters for the first resolved path

                return(ClearItemDynamicParameters(providerInstance, providerPaths[0], newContext));
            }

            return(null);
        }
        /// <summary>
        /// Gets the dynamic parameters for the get-content cmdlet.
        /// </summary>
        /// <param name="path">
        /// The path to the item if it was specified on the command line.
        /// </param>
        /// <param name="context">
        /// The context which the core command is running.
        /// </param>
        /// <returns>
        /// An object that has properties and fields decorated with
        /// parsing attributes similar to a cmdlet class.
        /// </returns>
        /// <exception cref="ProviderNotFoundException">
        /// If the <paramref name="path"/> refers to a provider that could not be found.
        /// </exception>
        /// <exception cref="DriveNotFoundException">
        /// If the <paramref name="path"/> refers to a drive that could not be found.
        /// </exception>
        /// <exception cref="NotSupportedException">
        /// If the provider that the <paramref name="path"/> refers to does
        /// not support this operation.
        /// </exception>
        /// <exception cref="ProviderInvocationException">
        /// If the provider threw an exception.
        /// </exception>
        /// <exception cref="ItemNotFoundException">
        /// If <paramref name="path"/> does not contain glob characters and
        /// could not be found.
        /// </exception>
        internal object GetContentReaderDynamicParameters(
            string path,
            CmdletProviderContext context)
        {
            if (path == null)
            {
                return(null);
            }

            ProviderInfo   provider         = null;
            CmdletProvider providerInstance = null;

            CmdletProviderContext newContext =
                new CmdletProviderContext(context);

            newContext.SetFilters(
                new Collection <string>(),
                new Collection <string>(),
                null);

            Collection <string> providerPaths =
                Globber.GetGlobbedProviderPathsFromMonadPath(
                    path,
                    true,
                    newContext,
                    out provider,
                    out providerInstance);

            return(GetContentReaderDynamicParameters(providerInstance, path, newContext));
        }
示例#3
0
 internal void CopyProperty(string[] sourcePaths, string sourceProperty, string destinationPath, string destinationProperty, CmdletProviderContext context)
 {
     if (sourcePaths == null)
     {
         throw PSTraceSource.NewArgumentNullException("sourcePaths");
     }
     if (sourceProperty == null)
     {
         throw PSTraceSource.NewArgumentNullException("sourceProperty");
     }
     if (destinationPath == null)
     {
         throw PSTraceSource.NewArgumentNullException("destinationPath");
     }
     if (destinationProperty == null)
     {
         throw PSTraceSource.NewArgumentNullException("destinationProperty");
     }
     foreach (string str in sourcePaths)
     {
         if (str == null)
         {
             throw PSTraceSource.NewArgumentNullException("sourcePaths");
         }
         ProviderInfo info = null;
         CmdletProvider providerInstance = null;
         Collection<string> collection = this.Globber.GetGlobbedProviderPathsFromMonadPath(str, false, context, out info, out providerInstance);
         if (collection.Count > 0)
         {
             Collection<string> include = context.Include;
             Collection<string> exclude = context.Exclude;
             string filter = context.Filter;
             context.SetFilters(new Collection<string>(), new Collection<string>(), null);
             Collection<string> collection4 = this.Globber.GetGlobbedProviderPathsFromMonadPath(destinationPath, false, context, out info, out providerInstance);
             context.SetFilters(include, exclude, filter);
             foreach (string str3 in collection)
             {
                 foreach (string str4 in collection4)
                 {
                     this.CopyProperty(providerInstance, str3, sourceProperty, str4, destinationProperty, context);
                 }
             }
         }
     }
 }
示例#4
0
 internal object CopyItemDynamicParameters(string path, string destination, bool recurse, CmdletProviderContext context)
 {
     if (path != null)
     {
         ProviderInfo info = null;
         CmdletProvider providerInstance = null;
         CmdletProviderContext context2 = new CmdletProviderContext(context);
         context2.SetFilters(new Collection<string>(), new Collection<string>(), null);
         Collection<string> collection = this.Globber.GetGlobbedProviderPathsFromMonadPath(path, true, context2, out info, out providerInstance);
         if (collection.Count > 0)
         {
             return this.CopyItemDynamicParameters(providerInstance, collection[0], destination, recurse, context2);
         }
     }
     return null;
 }
示例#5
0
 internal object SetPropertyDynamicParameters(string path, PSObject propertyValue, CmdletProviderContext context)
 {
     if (path != null)
     {
         ProviderInfo info = null;
         CmdletProvider providerInstance = null;
         CmdletProviderContext context2 = new CmdletProviderContext(context);
         context2.SetFilters(new Collection<string>(), new Collection<string>(), null);
         Collection<string> collection = this.Globber.GetGlobbedProviderPathsFromMonadPath(path, true, context2, out info, out providerInstance);
         if (collection.Count > 0)
         {
             return this.SetPropertyDynamicParameters(providerInstance, collection[0], propertyValue, context2);
         }
     }
     return null;
 }
示例#6
0
 internal void MoveProperty(string[] sourcePaths, string sourceProperty, string destinationPath, string destinationProperty, CmdletProviderContext context)
 {
     if (sourcePaths == null)
     {
         throw PSTraceSource.NewArgumentNullException("sourcePaths");
     }
     if (sourceProperty == null)
     {
         throw PSTraceSource.NewArgumentNullException("sourceProperty");
     }
     if (destinationPath == null)
     {
         throw PSTraceSource.NewArgumentNullException("destinationPath");
     }
     if (destinationProperty == null)
     {
         throw PSTraceSource.NewArgumentNullException("destinationProperty");
     }
     ProviderInfo info = null;
     CmdletProvider providerInstance = null;
     CmdletProviderContext context2 = new CmdletProviderContext(context);
     context2.SetFilters(new Collection<string>(), new Collection<string>(), null);
     Collection<string> targetObject = this.Globber.GetGlobbedProviderPathsFromMonadPath(destinationPath, false, context2, out info, out providerInstance);
     if (targetObject.Count > 1)
     {
         ArgumentException exception = PSTraceSource.NewArgumentException("destinationPath", "SessionStateStrings", "MovePropertyDestinationResolveToSingle", new object[0]);
         context.WriteError(new ErrorRecord(exception, exception.GetType().FullName, ErrorCategory.InvalidArgument, targetObject));
     }
     else
     {
         foreach (string str in sourcePaths)
         {
             if (str == null)
             {
                 throw PSTraceSource.NewArgumentNullException("sourcePaths");
             }
             foreach (string str2 in this.Globber.GetGlobbedProviderPathsFromMonadPath(str, false, context, out info, out providerInstance))
             {
                 this.MoveProperty(providerInstance, str2, sourceProperty, targetObject[0], destinationProperty, context);
             }
         }
     }
 }
示例#7
0
 internal object ItemExistsDynamicParameters(string path, CmdletProviderContext context)
 {
     if (path == null)
     {
         throw PSTraceSource.NewArgumentNullException("path");
     }
     ProviderInfo info = null;
     CmdletProvider providerInstance = null;
     CmdletProviderContext context2 = new CmdletProviderContext(context);
     context2.SetFilters(new Collection<string>(), new Collection<string>(), null);
     Collection<string> collection = this.Globber.GetGlobbedProviderPathsFromMonadPath(path, true, context2, out info, out providerInstance);
     if (collection.Count > 0)
     {
         return this.ItemExistsDynamicParameters(providerInstance, collection[0], context2);
     }
     return null;
 }
        } // GetChildNames

        /// <summary>
        /// Gets names of the children of the specified path.
        /// </summary>
        /// 
        /// <param name="path">
        /// The path to the item from which to retrieve the child names.
        /// </param>
        /// 
        /// <param name="returnContainers">
        /// Determines if all containers should be returned or only those containers that match the
        /// filter(s).
        /// </param>
        /// 
        /// <param name="recurse">
        /// If true, gets all the relative paths of all the children 
        /// in all the sub-containers of the specified
        /// container. If false, only gets the immediate child names of the specified
        /// container.
        /// </param>
        ///
        /// <param name="depth">
        /// Limits the depth of recursion; uint.MaxValue performs full recursion.
        /// </param>
        /// 
        /// <param name="context">
        /// The context which the core command is running.
        /// </param>
        /// 
        /// <returns>
        /// Nothing is returned, but all names should be written to the context object.
        /// </returns>
        /// 
        /// <remarks>
        /// The child names are the leaf portion of the path. Example, for the file system
        /// the name for the path c:\windows\system32\foo.dll would be foo.dll or for
        /// the directory c:\windows\system32 would be system32. For Active Directory the
        /// child names would be RDN values of the child objects of the container.
        /// </remarks>
        /// 
        /// <exception cref="ArgumentNullException">
        /// If <paramref name="path"/> or <paramref name="propertyToClear"/> is null.
        /// </exception>
        /// 
        /// <exception cref="ProviderNotFoundException">
        /// If the <paramref name="path"/> refers to a provider that could not be found.
        /// </exception>
        /// 
        /// <exception cref="DriveNotFoundException">
        /// If the <paramref name="path"/> refers to a drive that could not be found.
        /// </exception>
        /// 
        /// <exception cref="NotSupportedException">
        /// If the provider that the <paramref name="path"/> refers to does
        /// not support this operation.
        /// </exception>
        /// 
        /// <exception cref="ProviderInvocationException">
        /// If the provider threw an exception.
        /// </exception>
        /// 
        /// <exception cref="ItemNotFoundException">
        /// If <paramref name="path"/> does not contain glob characters and
        /// could not be found.
        /// </exception>
        /// 
        internal void GetChildNames(
            string path,
            ReturnContainers returnContainers,
            bool recurse,
            uint depth,
            CmdletProviderContext context)
        {
            if (path == null)
            {
                throw PSTraceSource.NewArgumentNullException("path");
            }

            // Construct the include filter

            Collection<WildcardPattern> includeMatcher =
                SessionStateUtilities.CreateWildcardsFromStrings(
                    context.Include,
                    WildcardOptions.IgnoreCase);


            // Construct the exclude filter

            Collection<WildcardPattern> excludeMatcher =
                SessionStateUtilities.CreateWildcardsFromStrings(
                    context.Exclude,
                    WildcardOptions.IgnoreCase);

            if (LocationGlobber.ShouldPerformGlobbing(path, context))
            {
                ProviderInfo provider = null;
                CmdletProvider providerInstance = null;

                // We don't want to process include/exclude filters
                // when globbing the targets of the operation, so
                // copy the context without the filters.

                CmdletProviderContext resolvePathContext =
                    new CmdletProviderContext(context);
                resolvePathContext.SetFilters(
                    new Collection<string>(),
                    new Collection<string>(),
                    null);

                // Resolve the path

                Collection<string> providerPaths =
                    Globber.GetGlobbedProviderPathsFromMonadPath(
                        path,
                        false,
                        resolvePathContext,
                        out provider,
                        out providerInstance);

                if (resolvePathContext.Drive != null)
                {
                    context.Drive = resolvePathContext.Drive;
                }

                bool pathContainsGlobCharacters = LocationGlobber.StringContainsGlobCharacters(path);
                foreach (string providerPath in providerPaths)
                {
                    // Making sure to obey the StopProcessing.
                    if (context.Stopping)
                    {
                        return;
                    }


                    if ((!pathContainsGlobCharacters || recurse) && IsItemContainer(providerInstance, providerPath, context))
                    {
                        // Since the path contained glob characters or we are recursing and the
                        // path is a container, do the name enumeration manually

                        DoGetChildNamesManually(
                            providerInstance,
                            providerPath,
                            String.Empty,
                            returnContainers,
                            includeMatcher,
                            excludeMatcher,
                            context,
                            recurse,
                            depth);
                    }
                    else
                    {
                        // Since the original path did not contain glob characters, 
                        // if the provider is a NavigationCmdletProvider, write
                        // out the child name, else write out the name as it
                        // was resolved.

                        if (providerInstance is NavigationCmdletProvider)
                        {
                            string childName =
                                GetChildName(
                                    providerInstance,
                                    providerPath,
                                    context, false);

                            bool isIncludeMatch =
                                SessionStateUtilities.MatchesAnyWildcardPattern(
                                    childName,
                                    includeMatcher,
                                    true);

                            bool isExcludeMatch =
                                SessionStateUtilities.MatchesAnyWildcardPattern(
                                    childName,
                                    excludeMatcher,
                                    false);

                            if (isIncludeMatch && !isExcludeMatch)
                            {
                                context.WriteObject(childName);
                            }
                        }
                        else
                        {
                            context.WriteObject(providerPath);
                        }
                    }
                }
            }
            else
            {
                // Figure out which provider to use

                ProviderInfo provider = null;
                PSDriveInfo drive = null;

                string providerPath =
                    Globber.GetProviderPath(
                        path,
                        context,
                        out provider,
                        out drive);

                ContainerCmdletProvider providerInstance = GetContainerProviderInstance(provider);

                if (drive != null)
                {
                    context.Drive = drive;
                }

                if (!providerInstance.ItemExists(providerPath, context))
                {
                    ItemNotFoundException pathNotFound =
                        new ItemNotFoundException(
                            providerPath,
                            "PathNotFound",
                            SessionStateStrings.PathNotFound);
                    throw pathNotFound;
                }

                if (recurse)
                {
                    // The path did not contain glob characters but recurse was specified
                    // so do the enumeration manually

                    DoGetChildNamesManually(
                        providerInstance,
                        providerPath,
                        String.Empty,
                        returnContainers,
                        includeMatcher,
                        excludeMatcher,
                        context,
                        recurse,
                        depth);
                }
                else
                {
                    // Since the path did not contain glob characters and recurse wasn't
                    // specified, we can have the provider write out the child names directly

                    GetChildNames(
                        providerInstance,
                        providerPath,
                        returnContainers,
                        context);
                }
            }
        } // GetChildNames
示例#9
0
 internal void GetChildItems(string path, bool recurse, CmdletProviderContext context)
 {
     if (path == null)
     {
         throw PSTraceSource.NewArgumentNullException("path");
     }
     if (context == null)
     {
         throw PSTraceSource.NewArgumentNullException("context");
     }
     ProviderInfo info = null;
     if (recurse || LocationGlobber.ShouldPerformGlobbing(path, context))
     {
         bool flag = false;
         try
         {
             if ((recurse && ((context.Include == null) || (context.Include.Count == 0))) && (!string.IsNullOrEmpty(path) && !this.IsItemContainer(path)))
             {
                 string childName = this.GetChildName(path, context);
                 if (!string.Equals(childName, "*", StringComparison.OrdinalIgnoreCase) && (context.Include != null))
                 {
                     context.Include.Add(childName);
                     flag = true;
                 }
                 path = path.Substring(0, path.Length - childName.Length);
             }
             Collection<string> include = context.Include;
             Collection<string> exclude = context.Exclude;
             string filter = context.Filter;
             if (recurse)
             {
                 context.SetFilters(new Collection<string>(), new Collection<string>(), null);
             }
             CmdletProvider providerInstance = null;
             Collection<string> collection3 = null;
             try
             {
                 collection3 = this.Globber.GetGlobbedProviderPathsFromMonadPath(path, false, context, out info, out providerInstance);
             }
             finally
             {
                 context.SetFilters(include, exclude, filter);
             }
             if (recurse)
             {
                 this.GetContainerProviderInstance(info);
             }
             bool flag2 = !LocationGlobber.StringContainsGlobCharacters(path);
             if ((((recurse && !flag2) && ((include != null) && (include.Count == 0))) || ((include != null) && (include.Count > 0))) || ((exclude != null) && (exclude.Count > 0)))
             {
                 foreach (string str4 in collection3)
                 {
                     if (context.Stopping)
                     {
                         return;
                     }
                     this.DoManualGetChildItems(providerInstance, str4, recurse, context, false);
                 }
             }
             else
             {
                 foreach (string str5 in collection3)
                 {
                     if (context.Stopping)
                     {
                         return;
                     }
                     if ((flag2 || recurse) && this.IsItemContainer(providerInstance, str5, context))
                     {
                         this.GetChildItems(providerInstance, str5, recurse, context);
                     }
                     else
                     {
                         this.GetItemPrivate(providerInstance, str5, context);
                     }
                 }
             }
         }
         finally
         {
             if (flag)
             {
                 context.Include.Clear();
             }
         }
     }
     else
     {
         PSDriveInfo drive = null;
         path = this.Globber.GetProviderPath(path, context, out info, out drive);
         if (drive != null)
         {
             context.Drive = drive;
         }
         ContainerCmdletProvider containerProviderInstance = this.GetContainerProviderInstance(info);
         if ((path != null) && this.ItemExists(containerProviderInstance, path, context))
         {
             if (this.IsItemContainer(containerProviderInstance, path, context))
             {
                 this.GetChildItems(containerProviderInstance, path, recurse, context);
             }
             else
             {
                 this.GetItemPrivate(containerProviderInstance, path, context);
             }
         }
         else
         {
             ItemNotFoundException exception = new ItemNotFoundException(path, "PathNotFound", SessionStateStrings.PathNotFound);
             throw exception;
         }
     }
 }
        } // RemoveProperty

        /// <summary>
        /// Gets the dynamic parameters for the remove-itemproperty cmdlet.
        /// </summary>
        /// 
        /// <param name="path">
        /// The path to the item if it was specified on the command line.
        /// </param>
        /// 
        /// <param name="propertyName">
        /// The name of the property that should be created.
        /// </param>
        ///
        /// <param name="context">
        /// The context which the core command is running.
        /// </param>
        /// 
        /// <returns>
        /// An object that has properties and fields decorated with
        /// parsing attributes similar to a cmdlet class.
        /// </returns>
        /// 
        /// <exception cref="ProviderNotFoundException">
        /// If the <paramref name="path"/> refers to a provider that could not be found.
        /// </exception>
        /// 
        /// <exception cref="DriveNotFoundException">
        /// If the <paramref name="path"/> refers to a drive that could not be found.
        /// </exception>
        /// 
        /// <exception cref="NotSupportedException">
        /// If the provider that the <paramref name="path"/> refers to does
        /// not support this operation.
        /// </exception>
        /// 
        /// <exception cref="ProviderInvocationException">
        /// If the provider threw an exception.
        /// </exception>
        /// 
        /// <exception cref="ItemNotFoundException">
        /// If <paramref name="path"/> does not contain glob characters and
        /// could not be found.
        /// </exception>
        /// 
        internal object RemovePropertyDynamicParameters(
             string path,
            string propertyName,
            CmdletProviderContext context)
        {
            if (path == null)
            {
                return null;
            }

            ProviderInfo provider = null;
            CmdletProvider providerInstance = null;

            CmdletProviderContext newContext =
               new CmdletProviderContext(context);
            newContext.SetFilters(
                new Collection<string>(),
                new Collection<string>(),
                null);

            Collection<string> providerPaths =
                 Globber.GetGlobbedProviderPathsFromMonadPath(
                    path,
                    true,
                    newContext,
                    out provider,
                    out providerInstance);

            if (providerPaths.Count > 0)
            {
                // Get the dynamic parameters for the first resolved path

                return RemovePropertyDynamicParameters(providerInstance, providerPaths[0], propertyName, newContext);
            }
            return null;
        } // RemovePropertyDynamicParameters
        } // MoveProperty

        /// <summary>
        /// Moves the specified property on the specified item to the specified property
        /// on the destination item.  The source and destination items can be the same item.
        /// </summary>
        /// 
        /// <param name="sourcePaths">
        /// The path(s) to the item(s) to move the property from.
        /// </param>
        /// 
        /// <param name="sourceProperty">
        /// The name of the property to be moved.
        /// </param>
        ///
        /// <param name="destinationPath">
        /// The path to the item to move the property to.
        /// </param> 
        /// 
        /// <param name="destinationProperty">
        /// The name of the property to move the property to.
        /// </param>
        ///
        /// <param name="context">
        /// The context which the core command is running.
        /// </param>
        /// 
        /// <exception cref="ArgumentNullException">
        /// If <paramref name="sourcePath"/>, <paramref name="sourceProperty"/>,
        /// <paramref name="destinationPath"/>, or <paramref name="destinationProperty"/>
        ///  is null.
        /// </exception>
        /// 
        /// <exception cref="ArgumentException">
        /// If <paramref name="destinationPath"/> resolves to more than one item.
        /// </exception>
        /// 
        /// <exception cref="ProviderNotFoundException">
        /// If the <paramref name="sourcePath"/> refers to a provider that could not be found.
        /// </exception>
        /// 
        /// <exception cref="DriveNotFoundException">
        /// If the <paramref name="sourcePath"/> refers to a drive that could not be found.
        /// </exception>
        /// 
        /// <exception cref="NotSupportedException">
        /// If the provider that the <paramref name="sourcePath"/> refers to does
        /// not support this operation.
        /// </exception>
        /// 
        /// <exception cref="ProviderInvocationException">
        /// If the provider threw an exception.
        /// </exception>
        /// 
        /// <exception cref="ItemNotFoundException">
        /// If <paramref name="path"/> does not contain glob characters and
        /// could not be found.
        /// </exception>
        /// 
        internal void MoveProperty(
            string[] sourcePaths,
            string sourceProperty,
            string destinationPath,
            string destinationProperty,
            CmdletProviderContext context)
        {
            if (sourcePaths == null)
            {
                throw PSTraceSource.NewArgumentNullException("sourcePaths");
            }

            if (sourceProperty == null)
            {
                throw PSTraceSource.NewArgumentNullException("sourceProperty");
            }

            if (destinationPath == null)
            {
                throw PSTraceSource.NewArgumentNullException("destinationPath");
            }

            if (destinationProperty == null)
            {
                throw PSTraceSource.NewArgumentNullException("destinationProperty");
            }

            ProviderInfo provider = null;
            CmdletProvider providerInstance = null;

            // We shouldn't be filtering the destination path

            CmdletProviderContext destinationContext = new CmdletProviderContext(context);

            destinationContext.SetFilters(
                new Collection<string>(),
                new Collection<string>(),
                null);

            Collection<string> destinationProviderPaths =
                Globber.GetGlobbedProviderPathsFromMonadPath(
                    destinationPath,
                    false,
                    destinationContext,
                    out provider,
                    out providerInstance);

            if (destinationProviderPaths.Count > 1)
            {
                ArgumentException argException =
                    PSTraceSource.NewArgumentException(
                        "destinationPath",
                        SessionStateStrings.MovePropertyDestinationResolveToSingle);

                context.WriteError(new ErrorRecord(argException, argException.GetType().FullName, ErrorCategory.InvalidArgument, destinationProviderPaths));
            }
            else
            {
                foreach (string sourcePath in sourcePaths)
                {
                    if (sourcePath == null)
                    {
                        throw PSTraceSource.NewArgumentNullException("sourcePaths");
                    }

                    Collection<string> providerPaths =
                        Globber.GetGlobbedProviderPathsFromMonadPath(
                            sourcePath,
                            false,
                            context,
                            out provider,
                            out providerInstance);

                    foreach (string providerPath in providerPaths)
                    {
                        MoveProperty(providerInstance, providerPath, sourceProperty, destinationProviderPaths[0], destinationProperty, context);
                    }
                }
            }
        } // MoveProperty
        } // CopyProperty

        /// <summary>
        /// Copies the specified property on the specified item to the specified property
        /// on the destination item.  The source and destination items can be the same item.
        /// </summary>
        /// 
        /// <param name="sourcePaths">
        /// The path(s) to the item(s) to copy the property from.
        /// </param>
        /// 
        /// <param name="sourceProperty">
        /// The name of the property to be copied.
        /// </param>
        ///
        /// <param name="destinationPath">
        /// The path to the item to copy the property to.
        /// </param> 
        /// 
        /// <param name="destinationProperty">
        /// The name of the property to copy the property to.
        /// </param>
        ///
        /// <param name="context">
        /// The context which the core command is running.
        /// </param>
        /// 
        /// <exception cref="ArgumentNullException">
        /// If <paramref name="sourcePath"/>, <paramref name="sourceProperty"/>,
        /// <paramref name="destinationPath"/>, or <paramref name="destinationProperty"/>
        ///  is null.
        /// </exception>
        /// 
        /// <exception cref="ProviderNotFoundException">
        /// If the <paramref name="sourcePath"/> refers to a provider that could not be found.
        /// </exception>
        /// 
        /// <exception cref="DriveNotFoundException">
        /// If the <paramref name="sourcePath"/> refers to a drive that could not be found.
        /// </exception>
        /// 
        /// <exception cref="NotSupportedException">
        /// If the provider that the <paramref name="sourcePath"/> refers to does
        /// not support this operation.
        /// </exception>
        /// 
        /// <exception cref="ProviderInvocationException">
        /// If the provider threw an exception.
        /// </exception>
        /// 
        /// <exception cref="ItemNotFoundException">
        /// If <paramref name="path"/> does not contain glob characters and
        /// could not be found.
        /// </exception>
        /// 
        internal void CopyProperty(
            string[] sourcePaths,
            string sourceProperty,
            string destinationPath,
            string destinationProperty,
            CmdletProviderContext context)
        {
            if (sourcePaths == null)
            {
                throw PSTraceSource.NewArgumentNullException("sourcePaths");
            }

            if (sourceProperty == null)
            {
                throw PSTraceSource.NewArgumentNullException("sourceProperty");
            }

            if (destinationPath == null)
            {
                throw PSTraceSource.NewArgumentNullException("destinationPath");
            }

            if (destinationProperty == null)
            {
                throw PSTraceSource.NewArgumentNullException("destinationProperty");
            }

            foreach (string sourcePath in sourcePaths)
            {
                if (sourcePath == null)
                {
                    throw PSTraceSource.NewArgumentNullException("sourcePaths");
                }

                ProviderInfo provider = null;
                CmdletProvider providerInstance = null;

                Collection<string> providerPaths =
                    Globber.GetGlobbedProviderPathsFromMonadPath(
                        sourcePath,
                        false,
                        context,
                        out provider,
                        out providerInstance);

                if (providerPaths.Count > 0)
                {
                    // Save off the original filters
                    Collection<string> includeFilters = context.Include;
                    Collection<string> excludeFilters = context.Exclude;
                    string filterString = context.Filter;

                    // now modify the filters so that the destination isn't filtered

                    context.SetFilters(
                        new Collection<string>(),
                        new Collection<string>(),
                        null);

                    Collection<string> providerDestinationPaths =
                        Globber.GetGlobbedProviderPathsFromMonadPath(
                            destinationPath,
                            false,
                            context,
                            out provider,
                            out providerInstance);

                    // Now reapply the filters

                    context.SetFilters(
                        includeFilters,
                        excludeFilters,
                        filterString);

                    foreach (string providerPath in providerPaths)
                    {
                        foreach (string providerDestinationPath in providerDestinationPaths)
                        {
                            CopyProperty(providerInstance, providerPath, sourceProperty, providerDestinationPath, destinationProperty, context);
                        }
                    }
                }
            }
        } // CopyProperty
示例#13
0
        } // CopyRecurseToSingleContainer

        /// <summary>
        /// Gets the dynamic parameters for the copy-item cmdlet.
        /// </summary>
        /// 
        /// <param name="path">
        /// The path to the item if it was specified on the command line.
        /// </param>
        /// 
        /// <param name="destination">
        /// The path of the item to copy to.
        /// </param>
        /// 
        /// <param name="recurse">
        /// Tells the provider to recurse sub-containers when copying.
        /// </param>
        /// 
        /// <param name="context">
        /// The context which the core command is running.
        /// </param>
        /// 
        /// <returns>
        /// An object that has properties and fields decorated with
        /// parsing attributes similar to a cmdlet class.
        /// </returns>
        /// 
        /// <exception cref="ProviderNotFoundException">
        /// If the <paramref name="path"/> refers to a provider that could not be found.
        /// </exception>
        /// 
        /// <exception cref="DriveNotFoundException">
        /// If the <paramref name="path"/> refers to a drive that could not be found.
        /// </exception>
        /// 
        /// <exception cref="NotSupportedException">
        /// If the provider that the <paramref name="path"/> refers to does
        /// not support this operation.
        /// </exception>
        /// 
        /// <exception cref="ProviderInvocationException">
        /// If the provider threw an exception.
        /// </exception>
        /// 
        /// <exception cref="ItemNotFoundException">
        /// If <paramref name="path"/> does not contain glob characters and
        /// could not be found.
        /// </exception>
        /// 
        internal object CopyItemDynamicParameters(
            string path,
            string destination,
            bool recurse,
            CmdletProviderContext context)
        {
            if (path == null)
            {
                return null;
            }

            ProviderInfo provider = null;
            CmdletProvider providerInstance = null;

            CmdletProviderContext newContext =
                new CmdletProviderContext(context);
            newContext.SetFilters(
                new Collection<string>(),
                new Collection<string>(),
                null);

            string providerPath = null;
            bool pathNotFoundOnClient = false;
            try
            {
                Collection<string> providerPaths =
                    Globber.GetGlobbedProviderPathsFromMonadPath(
                        path,
                        true,
                        newContext,
                        out provider,
                        out providerInstance);

                if (providerPaths.Count > 0)
                    providerPath = providerPaths[0];
            }

            catch (DriveNotFoundException)
            {
                // This exception is expected for remote sessions where drives exist in a remote session but not
                // on the client.
                pathNotFoundOnClient = true;
            }
            catch (ItemNotFoundException)
            {
                // This exception is expected for remote sessions where item exist in a remote session but not
                // on the client.
                pathNotFoundOnClient = true;
            }

            if (pathNotFoundOnClient)
            {
                // At this point, we don't know if this is a remote use of copy-item because the FromSession
                // and ToSession dynamic parameters have not been retrieved yet.
                // Ignore these exceptions and use the FileSystem provider as default.  If this is a real drive
                // issue, or if the item does not exist, it will be caught later when the drive or item path is used.
                var fileSystemProviders = Providers["FileSystem"];
                if (fileSystemProviders.Count > 0)
                {
                    providerPath = path;
                    providerInstance = ExecutionContext.EngineSessionState.GetProviderInstance(
                        fileSystemProviders[0]);
                }
            }

            if (providerPath != null)
            {
                // Get the dynamic parameters for the first resolved path
                return CopyItemDynamicParameters(providerInstance, providerPath, destination, recurse, newContext);
            }

            return null;
        } // CopyItemDynamicParameters
示例#14
0
        } // GetChildNames

        /// <summary>
        /// Gets the dynamic parameters for the get-childitem -name cmdlet.
        /// </summary>
        /// 
        /// <param name="path">
        /// The path to the item if it was specified on the command line.
        /// </param>
        /// 
        /// <param name="context">
        /// The context which the core command is running.
        /// </param>
        /// 
        /// <returns>
        /// An object that has properties and fields decorated with
        /// parsing attributes similar to a cmdlet class.
        /// </returns>
        /// 
        /// <exception cref="ProviderNotFoundException">
        /// If the <paramref name="path"/> refers to a provider that could not be found.
        /// </exception>
        /// 
        /// <exception cref="DriveNotFoundException">
        /// If the <paramref name="path"/> refers to a drive that could not be found.
        /// </exception>
        /// 
        /// <exception cref="NotSupportedException">
        /// If the provider that the <paramref name="path"/> refers to does
        /// not support this operation.
        /// </exception>
        /// 
        /// <exception cref="ProviderInvocationException">
        /// If the provider threw an exception.
        /// </exception>
        /// 
        /// <exception cref="ItemNotFoundException">
        /// If <paramref name="path"/> does not contain glob characters and
        /// could not be found.
        /// </exception>
        /// 
        internal object GetChildNamesDynamicParameters(
            string path,
            CmdletProviderContext context)
        {
            if (path == null)
            {
                return null;
            }

            ProviderInfo provider = null;
            CmdletProvider providerInstance = null;

            CmdletProviderContext newContext =
                new CmdletProviderContext(context);
            newContext.SetFilters(
                new Collection<string>(),
                new Collection<string>(),
                null);

            Collection<string> providerPaths = null;

            try
            {
                providerPaths =
                    Globber.GetGlobbedProviderPathsFromMonadPath(
                        path,
                        true,
                        newContext,
                        out provider,
                        out providerInstance);
            }
            catch (ItemNotFoundException)
            {
                // If the provided path is like c:\fakepath\aa*, since we cannot resolve c:\fakepath, an
                // ItemNotFoundException will be thrown out. In this case, we catch the exception 
                // and check if the "providerInstance" is identified. If providerInstance is not null, 
                // we can carry on with the get-dynamic-parameters method.
                if (providerInstance == null)
                {
                    throw;
                }
            }

            object result = null;
            if (providerPaths != null && providerPaths.Count > 0)
            {
                // Get the dynamic parameters for the first resolved path
                result = GetChildNamesDynamicParameters(providerInstance, providerPaths[0], newContext);
            }
            else
            {
                if (providerInstance != null)
                {
                    PSDriveInfo drive = null;
                    // If we get here, the GetProviderPath should always succeed. This method was already invoked 
                    // in the call to GetGlobbedProviderPathsFromMonadPath, and since "providerInstance" is not null,
                    // the invocation in method GetGlobbedProviderPathsFromMonadPath should succeed.
                    string providerPath = Globber.GetProviderPath(path, context, out provider, out drive);
                    if (providerPath != null)
                    {
                        result = GetChildNamesDynamicParameters(providerInstance, providerPath, newContext);
                    }
                }
            }
            return result;
        } // GetChildNamesDynamicParameters
示例#15
0
        } // Exists

        /// <summary>
        /// Gets the dynamic parameters for the test-path cmdlet.
        /// </summary>
        /// 
        /// <param name="path">
        /// The path to the item if it was specified on the command line.
        /// </param>
        /// 
        /// <param name="context">
        /// The context which the core command is running.
        /// </param>
        /// 
        /// <returns>
        /// An object that has properties and fields decorated with
        /// parsing attributes similar to a cmdlet class.
        /// </returns>
        /// 
        /// <exception cref="ArgumentNullException">
        /// If <paramref name="path"/> is null.
        /// </exception>
        /// 
        /// <exception cref="ProviderNotFoundException">
        /// If the <paramref name="path"/> refers to a provider that could not be found.
        /// </exception>
        /// 
        /// <exception cref="DriveNotFoundException">
        /// If the <paramref name="path"/> refers to a drive that could not be found.
        /// </exception>
        /// 
        /// <exception cref="NotSupportedException">
        /// If the provider that the <paramref name="path"/> refers to does
        /// not support this operation.
        /// </exception>
        /// 
        /// <exception cref="ProviderInvocationException">
        /// If the provider threw an exception.
        /// </exception>
        /// 
        /// <exception cref="ItemNotFoundException">
        /// If <paramref name="path"/> does not contain glob characters and
        /// could not be found.
        /// </exception>
        /// 
        internal object ItemExistsDynamicParameters(string path, CmdletProviderContext context)
        {
            if (path == null)
            {
                throw PSTraceSource.NewArgumentNullException("path");
            }

            ProviderInfo provider = null;
            CmdletProvider providerInstance = null;

            CmdletProviderContext newContext =
                new CmdletProviderContext(context);
            newContext.SetFilters(
                new Collection<string>(),
                new Collection<string>(),
                null);

            Collection<string> providerPaths =
                Globber.GetGlobbedProviderPathsFromMonadPath(
                    path,
                    true,
                    newContext,
                    out provider,
                    out providerInstance);

            if (providerPaths.Count > 0)
            {
                // Get the dynamic parameters for the first resolved path

                return ItemExistsDynamicParameters(providerInstance, providerPaths[0], newContext);
            }
            return null;
        } // ItemExistsDynamicParameters
示例#16
0
        } // GenerateNewPathsWithGlobLeaf

        /// <summary>
        /// Gets the child names in the specified path by using the provider
        /// </summary>
        /// 
        /// <param name="dir">
        /// The path of the directory to get the child names from. If this is an Msh Path,
        /// dirIsProviderPath must be false, If this is a provider-internal path,
        /// dirIsProviderPath must be true.
        /// </param>
        /// 
        /// <param name="leafElement">
        /// The element that we are ultimately looking for. Used to set filters on the context
        /// if desired.
        /// </param>
        /// 
        /// <param name="getAllContainers">
        /// Determines if the GetChildNames call should get all containers even if they don't
        /// match the filter.
        /// </param>
        /// 
        /// <param name="context">
        /// The context to be used for the command. The context is copied to a new context, the
        /// results are accumulated and then returned.
        /// </param>
        /// 
        /// <param name="dirIsProviderPath">
        /// Specifies whether the dir parameter is a provider-internal path (true) or Msh Path (false).
        /// </param>
        /// 
        /// <param name="drive">
        /// The drive to use to qualify the Msh path if dirIsProviderPath is false.
        /// </param>
        /// 
        /// <param name="provider">
        /// The provider to use to get the child names.
        /// </param>
        /// 
        /// <param name="modifiedDirPath">
        /// Returns the modified dir path. If dirIsProviderPath is true, this is the unescaped dir path.
        /// If dirIsProviderPath is false, this is the unescaped resolved provider path.
        /// </param>
        /// 
        /// <returns>
        /// A collection of PSObjects whose BaseObject is a string that contains the name of the child.
        /// </returns>
        /// 
        /// <exception cref="ArgumentNullException">
        /// If <paramref name="dir"/> or <paramref name="drive"/> is null.
        /// </exception>
        /// 
        /// <exception cref="ProviderNotFoundException">
        /// If the path is a provider-qualified path for a provider that is
        /// not loaded into the system.
        /// </exception>
        /// 
        /// <exception cref="DriveNotFoundException">
        /// If the <paramref name="path"/> refers to a drive that could not be found.
        /// </exception>
        /// 
        /// <exception cref="ProviderInvocationException">
        /// If the provider used to build the path threw an exception.
        /// </exception>
        /// 
        /// <exception cref="NotSupportedException">
        /// If the provider that the <paramref name="path"/> represents is not a NavigationCmdletProvider
        /// or ContainerCmdletProvider.
        /// </exception>
        /// 
        /// <exception cref="InvalidOperationException">
        /// If the <paramref name="path"/> starts with "~" and the home location is not set for 
        /// the provider.
        /// </exception>
        /// 
        /// <exception cref="ProviderInvocationException">
        /// If the provider associated with the <paramref name="path"/> threw an
        /// exception when its GetParentPath or MakePath was called while
        /// processing the <paramref name="path"/>.
        /// </exception>
        /// 
        /// <exception cref="PipelineStoppedException">
        /// If <paramref name="context"/> has been signaled for
        /// StopProcessing.
        /// </exception>
        /// 
        /// <exception>
        /// Any exception can be thrown by the provider that is called to build
        /// the provider path.
        /// </exception>
        /// 
        private Collection<PSObject> GetChildNamesInDir(
            string dir,
            string leafElement,
            bool getAllContainers,
            CmdletProviderContext context,
            bool dirIsProviderPath,
            PSDriveInfo drive,
            ContainerCmdletProvider provider,
            out string modifiedDirPath)
        {
            // See if the provider wants to convert the path and filter
            string convertedPath = null;
            string convertedFilter = null;
            string originalFilter = context.Filter;
            bool changedPathOrFilter = provider.ConvertPath(leafElement, context.Filter, ref convertedPath, ref convertedFilter, context);

            if (changedPathOrFilter)
            {
                if (s_tracer.IsEnabled)
                {
                    s_tracer.WriteLine("Provider converted path and filter.");
                    s_tracer.WriteLine("Original path: {0}", leafElement);
                    s_tracer.WriteLine("Converted path: {0}", convertedPath);
                    s_tracer.WriteLine("Original filter: {0}", context.Filter);
                    s_tracer.WriteLine("Converted filter: {0}", convertedFilter);
                }

                leafElement = convertedPath;
                context.Filter = convertedFilter;
            }

            ReturnContainers returnContainers = ReturnContainers.ReturnAllContainers;
            if (!getAllContainers)
            {
                returnContainers = ReturnContainers.ReturnMatchingContainers;
            }

            CmdletProviderContext getChildNamesContext =
                new CmdletProviderContext(context);

            // Remove the include/exclude filters from the new context
            getChildNamesContext.SetFilters(
                new Collection<string>(),
                new Collection<string>(),
                context.Filter);

            try
            {
                // Use the provider to get the children
                string unescapedDir = null;
                modifiedDirPath = null;

                if (dirIsProviderPath)
                {
                    modifiedDirPath = unescapedDir = context.SuppressWildcardExpansion ? dir : RemoveGlobEscaping(dir);
                }
                else
                {
                    Dbg.Diagnostics.Assert(
                        drive != null,
                        "Caller should verify that drive is not null when dirIsProviderPath is false");

                    // If the directory is an MSH path we must resolve it before calling GetChildNames()
                    // -- If the path is passed in by LiteralPath (context.SuppressWildcardExpansion == false), we surely should use 'dir' unchanged.
                    // -- If the path is passed in by Path (context.SuppressWildcardExpansion == true), we still should use 'dir' unchanged, in case that the special character
                    //    in 'dir' is escaped
                    modifiedDirPath = GetMshQualifiedPath(dir, drive);

                    ProviderInfo providerIgnored = null;
                    CmdletProvider providerInstanceIgnored = null;
                    Collection<string> resolvedPaths =
                        GetGlobbedProviderPathsFromMonadPath(
                            modifiedDirPath,
                            false,
                            getChildNamesContext,
                            out providerIgnored,
                            out providerInstanceIgnored);

                    // After resolving the path, we unescape the modifiedDirPath if necessary.
                    modifiedDirPath = context.SuppressWildcardExpansion
                                          ? modifiedDirPath
                                          : RemoveGlobEscaping(modifiedDirPath);
                    if (resolvedPaths.Count > 0)
                    {
                        unescapedDir = resolvedPaths[0];
                    }
                    else
                    {
                        // If there were no results from globbing but no
                        // exception was thrown, that means there was filtering.
                        // So return an empty collection and let the caller deal
                        // with it.

                        if (changedPathOrFilter)
                        {
                            context.Filter = originalFilter;
                        }

                        return new Collection<PSObject>();
                    }
                }

                if (provider.HasChildItems(unescapedDir, getChildNamesContext))
                {
                    provider.GetChildNames(
                        unescapedDir,
                        returnContainers,
                        getChildNamesContext);
                }

                // First check to see if there were any errors, and write them
                // to the real context if there are.

                if (getChildNamesContext.HasErrors())
                {
                    Collection<ErrorRecord> errors = getChildNamesContext.GetAccumulatedErrorObjects();

                    if (errors != null &&
                        errors.Count > 0)
                    {
                        foreach (ErrorRecord errorRecord in errors)
                        {
                            context.WriteError(errorRecord);
                        }
                    }
                }

                Collection<PSObject> childNamesObjectArray = getChildNamesContext.GetAccumulatedObjects();

                if (changedPathOrFilter)
                {
                    context.Filter = originalFilter;
                }

                return childNamesObjectArray;
            }
            finally
            {
                getChildNamesContext.RemoveStopReferral();
            }
        } // GetChildNamesInDir
示例#17
0
 private Collection<PSObject> GetChildNamesInDir(string dir, string leafElement, bool getAllContainers, CmdletProviderContext context, bool dirIsProviderPath, PSDriveInfo drive, ContainerCmdletProvider provider, out string modifiedDirPath)
 {
     string updatedPath = null;
     string updatedFilter = null;
     Collection<PSObject> collection4;
     string filter = context.Filter;
     bool flag = provider.ConvertPath(leafElement, context.Filter, ref updatedPath, ref updatedFilter, context);
     if (flag)
     {
         tracer.WriteLine("Provider converted path and filter.", new object[0]);
         tracer.WriteLine("Original path: " + leafElement, new object[0]);
         tracer.WriteLine("Converted path: " + updatedPath, new object[0]);
         tracer.WriteLine("Original filter: " + context.Filter, new object[0]);
         tracer.WriteLine("Converted filter: " + updatedFilter, new object[0]);
         leafElement = updatedPath;
         context.Filter = updatedFilter;
     }
     ReturnContainers returnAllContainers = ReturnContainers.ReturnAllContainers;
     if (!getAllContainers)
     {
         returnAllContainers = ReturnContainers.ReturnMatchingContainers;
     }
     CmdletProviderContext context2 = new CmdletProviderContext(context);
     context2.SetFilters(new Collection<string>(), new Collection<string>(), context.Filter);
     try
     {
         string path = null;
         modifiedDirPath = null;
         if (dirIsProviderPath)
         {
             modifiedDirPath = path = context.SuppressWildcardExpansion ? dir : RemoveGlobEscaping(dir);
         }
         else
         {
             modifiedDirPath = GetMshQualifiedPath(dir, drive);
             ProviderInfo info = null;
             CmdletProvider providerInstance = null;
             Collection<string> collection = this.GetGlobbedProviderPathsFromMonadPath(modifiedDirPath, false, context2, out info, out providerInstance);
             modifiedDirPath = context.SuppressWildcardExpansion ? modifiedDirPath : RemoveGlobEscaping(modifiedDirPath);
             if (collection.Count > 0)
             {
                 path = collection[0];
             }
             else
             {
                 if (flag)
                 {
                     context.Filter = filter;
                 }
                 return new Collection<PSObject>();
             }
         }
         if (provider.HasChildItems(path, context2))
         {
             provider.GetChildNames(path, returnAllContainers, context2);
         }
         if (context2.HasErrors())
         {
             Collection<ErrorRecord> accumulatedErrorObjects = context2.GetAccumulatedErrorObjects();
             if ((accumulatedErrorObjects != null) && (accumulatedErrorObjects.Count > 0))
             {
                 foreach (ErrorRecord record in accumulatedErrorObjects)
                 {
                     context.WriteError(record);
                 }
             }
         }
         Collection<PSObject> accumulatedObjects = context2.GetAccumulatedObjects();
         if (flag)
         {
             context.Filter = filter;
         }
         collection4 = accumulatedObjects;
     }
     finally
     {
         context2.RemoveStopReferral();
     }
     return collection4;
 }
示例#18
0
 internal void GetChildNames(string path, ReturnContainers returnContainers, bool recurse, CmdletProviderContext context)
 {
     if (path == null)
     {
         throw PSTraceSource.NewArgumentNullException("path");
     }
     Collection<WildcardPattern> includeMatcher = SessionStateUtilities.CreateWildcardsFromStrings(context.Include, WildcardOptions.IgnoreCase);
     Collection<WildcardPattern> excludeMatcher = SessionStateUtilities.CreateWildcardsFromStrings(context.Exclude, WildcardOptions.IgnoreCase);
     if (LocationGlobber.ShouldPerformGlobbing(path, context))
     {
         ProviderInfo info = null;
         CmdletProvider providerInstance = null;
         CmdletProviderContext context2 = new CmdletProviderContext(context);
         context2.SetFilters(new Collection<string>(), new Collection<string>(), null);
         Collection<string> collection3 = this.Globber.GetGlobbedProviderPathsFromMonadPath(path, false, context2, out info, out providerInstance);
         if (context2.Drive != null)
         {
             context.Drive = context2.Drive;
         }
         bool flag = LocationGlobber.StringContainsGlobCharacters(path);
         foreach (string str in collection3)
         {
             if (context.Stopping)
             {
                 break;
             }
             if ((!flag || recurse) && this.IsItemContainer(providerInstance, str, context))
             {
                 this.DoGetChildNamesManually(providerInstance, str, string.Empty, returnContainers, includeMatcher, excludeMatcher, context, recurse);
             }
             else if (providerInstance is NavigationCmdletProvider)
             {
                 string text = this.GetChildName(providerInstance, str, context, false);
                 bool flag2 = SessionStateUtilities.MatchesAnyWildcardPattern(text, includeMatcher, true);
                 bool flag3 = SessionStateUtilities.MatchesAnyWildcardPattern(text, excludeMatcher, false);
                 if (flag2 && !flag3)
                 {
                     context.WriteObject(text);
                 }
             }
             else
             {
                 context.WriteObject(str);
             }
         }
     }
     else
     {
         ProviderInfo provider = null;
         PSDriveInfo drive = null;
         string str3 = this.Globber.GetProviderPath(path, context, out provider, out drive);
         ContainerCmdletProvider containerProviderInstance = this.GetContainerProviderInstance(provider);
         if (drive != null)
         {
             context.Drive = drive;
         }
         if (!containerProviderInstance.ItemExists(str3, context))
         {
             ItemNotFoundException exception = new ItemNotFoundException(str3, "PathNotFound", SessionStateStrings.PathNotFound);
             throw exception;
         }
         if (recurse)
         {
             this.DoGetChildNamesManually(containerProviderInstance, str3, string.Empty, returnContainers, includeMatcher, excludeMatcher, context, recurse);
         }
         else
         {
             this.GetChildNames(containerProviderInstance, str3, returnContainers, context);
         }
     }
 }
示例#19
0
 internal object GetChildNamesDynamicParameters(string path, CmdletProviderContext context)
 {
     if (path == null)
     {
         return null;
     }
     ProviderInfo info = null;
     CmdletProvider providerInstance = null;
     CmdletProviderContext context2 = new CmdletProviderContext(context);
     context2.SetFilters(new Collection<string>(), new Collection<string>(), null);
     Collection<string> collection = null;
     try
     {
         collection = this.Globber.GetGlobbedProviderPathsFromMonadPath(path, true, context2, out info, out providerInstance);
     }
     catch (ItemNotFoundException)
     {
         if (providerInstance == null)
         {
             throw;
         }
     }
     object obj2 = null;
     if ((collection != null) && (collection.Count > 0))
     {
         return this.GetChildNamesDynamicParameters(providerInstance, collection[0], context2);
     }
     if (providerInstance != null)
     {
         PSDriveInfo drive = null;
         string str = this.Globber.GetProviderPath(path, context, out info, out drive);
         if (str != null)
         {
             obj2 = this.GetChildNamesDynamicParameters(providerInstance, str, context2);
         }
     }
     return obj2;
 }
示例#20
0
        } // GetChildItems

        /// <summary>
        /// Gets the children of the specified item.
        /// </summary>
        /// 
        /// <param name="path">
        /// A relative or absolute path to the object to get the children of.
        /// </param>
        /// 
        /// <param name="recurse">
        /// If true, gets all the children in all the sub-containers of the specified
        /// container. If false, only gets the immediate children of the specified
        /// container.
        /// </param>
        /// 
        /// <param name="depth">
        /// Limits the depth of recursion; uint.MaxValue performs full recursion.
        /// </param>
        /// 
        /// <param name="context">
        /// The context which the core command is running.
        /// </param>
        /// 
        /// <exception cref="ArgumentNullException">
        /// If <paramref name="path"/> or <paramref name="context"/> is null.
        /// </exception>
        /// 
        /// <exception cref="ProviderNotFoundException">
        /// If the <paramref name="path"/> refers to a provider that could not be found.
        /// </exception>
        /// 
        /// <exception cref="DriveNotFoundException">
        /// If the <paramref name="path"/> refers to a drive that could not be found.
        /// </exception>
        /// 
        /// <exception cref="NotSupportedException">
        /// If the provider that the <paramref name="path"/> refers to does
        /// not support this operation.
        /// </exception>
        /// 
        /// <exception cref="ProviderInvocationException">
        /// If the provider threw an exception.
        /// </exception>
        /// 
        /// <exception cref="ItemNotFoundException">
        /// If <paramref name="path"/> does not contain glob characters and
        /// could not be found.
        /// </exception>
        /// 
        internal void GetChildItems(
            string path,
            bool recurse,
            uint depth,
            CmdletProviderContext context)
        {
            if (path == null)
            {
                throw PSTraceSource.NewArgumentNullException("path");
            }

            if (context == null)
            {
                throw PSTraceSource.NewArgumentNullException("context");
            }

            ProviderInfo provider = null;

            if ((recurse && !context.SuppressWildcardExpansion) || LocationGlobber.ShouldPerformGlobbing(path, context))
            {
                bool modifiedInclude = false;

                try
                {
                    // If we're recursing, do some path fixups to match user
                    // expectations:
                    if (recurse)
                    {
                        // dir c:\tem* -include *.ps1 -rec => No change
                        if ((context.Include == null) || (context.Include.Count == 0))
                        {
                            // dir c:\tem* -rec => dir c:\ -include tem* -rec
                            // dir tem* -rec => dir -include tem* -rec
                            // dir temp -rec

                            // Should glob paths and files that match tem*, but then
                            // recurse into all subdirectories and do the same for
                            // those directories.
                            if ((!String.IsNullOrEmpty(path)) && (!IsItemContainer(path)))
                            {
                                string childName = GetChildName(path, context);
                                if (!String.Equals(childName, "*", StringComparison.OrdinalIgnoreCase))
                                {
                                    if (context.Include != null)
                                    {
                                        context.Include.Add(childName);
                                        modifiedInclude = true;
                                    }
                                }

                                string parentName = path.Substring(0, path.Length - childName.Length);
                                path = parentName;
                            }
                        }
                    }

                    // Save the include and exclude filters so that we can ignore
                    // them when doing recursion

                    Collection<string> include = context.Include;
                    Collection<string> exclude = context.Exclude;
                    string filter = context.Filter;

                    if (recurse)
                    {
                        context.SetFilters(
                            new Collection<string>(),
                            new Collection<string>(),
                            null);
                    }

                    CmdletProvider providerInstance = null;
                    Collection<string> providerPaths = null;

                    try
                    {
                        providerPaths = Globber.GetGlobbedProviderPathsFromMonadPath(
                                path,
                                false,
                                context,
                                out provider,
                                out providerInstance);
                    }
                    finally
                    {
                        // Reset the include and exclude filters
                        context.SetFilters(
                            include,
                            exclude,
                            filter);
                    }

                    // Ensure this is a container provider before recursing.
                    // GetContainerProviderInstance throws otherwise (as was done in V2.)
                    if (recurse)
                    {
                        ContainerCmdletProvider unused = GetContainerProviderInstance(provider);
                    }


                    bool getChildrenBecauseNoGlob = !LocationGlobber.StringContainsGlobCharacters(path);
                    // If we are doing recursion and we have include or exclude
                    // filters the recursion must be done manually.
                    // Win8: 279933 Perf degradation: recursive get-childitem is 100% slower than win7
                    // To fix this Perf regression, using getChildrenBecauseNoGlob (+recurse) variables
                    // to decide when to use ManualGetChildItems and when not to.
                    if (
                        (recurse && !getChildrenBecauseNoGlob && (include != null) && (include.Count == 0)) ||
                        (include != null && include.Count > 0) ||
                        (exclude != null && exclude.Count > 0))
                    {
                        // Do the recursion manually so that we can apply the
                        // include and exclude filters
                        foreach (string providerPath in providerPaths)
                        {
                            // Making sure to obey the StopProcessing.
                            if (context.Stopping)
                            {
                                return;
                            }

                            int unUsedChildrenNotMatchingFilterCriteria = 0;
                            ProcessPathItems(providerInstance, providerPath, recurse, context, out unUsedChildrenNotMatchingFilterCriteria, ProcessMode.Enumerate);
                        }
                    }
                    else
                    {
                        // If the path wasn't globbed or we are recursing then we want to get the 
                        // children of the path. If we were globbing and we are not recursing
                        // then we just want to get the item for the resolved paths.

                        foreach (string providerPath in providerPaths)
                        {
                            // Making sure to obey the StopProcessing.
                            if (context.Stopping)
                            {
                                return;
                            }

                            if ((getChildrenBecauseNoGlob || recurse) && IsItemContainer(providerInstance, providerPath, context))
                            {
                                GetChildItems(providerInstance, providerPath, recurse, depth, context);
                            }
                            else
                            {
                                GetItemPrivate(providerInstance, providerPath, context);
                            }
                        }
                    }
                }
                finally
                {
                    if (modifiedInclude)
                    {
                        context.Include.Clear();
                    }
                }
            }
            else
            {
                PSDriveInfo drive = null;

                string originalPath = path;
                path =
                    Globber.GetProviderPath(
                        path,
                        context,
                        out provider,
                        out drive);

                if (drive != null)
                {
                    context.Drive = drive;
                }

                ContainerCmdletProvider providerInstance = GetContainerProviderInstance(provider);

                if (path != null && this.ItemExists(providerInstance, path, context))
                {
                    if (IsItemContainer(providerInstance, path, context))
                    {
                        GetChildItems(providerInstance, path, recurse, depth, context);
                    }
                    else
                    {
                        GetItemPrivate(providerInstance, path, context);
                    }
                }
                else
                {
                    ItemNotFoundException pathNotFound =
                        new ItemNotFoundException(
                            path,
                            "PathNotFound",
                            SessionStateStrings.PathNotFound);
                    throw pathNotFound;
                }
            }
        } // GetChildItems