Exemplo n.º 1
0
        protected override void ProcessRecord()
        {
            // name support wildcards.
            // the default is { "*" }, which will match and return all special folders defined in SpecialFolderEnumNames
            // ps will take care of validating against empty collections and empty/null string

            // loop through each name specified
            foreach (string nameItem in _name)
            {
                // create wildcard pattern for the name item
                WildcardPattern nameItemPattern = new WildcardPattern(
                    nameItem,
                    WildcardOptions.IgnoreCase | WildcardOptions.Compiled | WildcardOptions.CultureInvariant
                    );

                // holds things that match nameItem
                // first elem is the enum name that will be parsed, second is specified if it comes from an alias
                List <string[]> foundMatch = new List <string[]>();

                // deal with alias first
                foreach (string altName in SpecialFolderAltNames.Keys)
                {
                    if (nameItemPattern.IsMatch(altName))
                    {
                        foundMatch.Add(new string[] { SpecialFolderAltNames[altName], altName });
                    }
                }

                // try match with each enum name
                foreach (string folderName in SpecialFolderEnumNames)
                {
                    if (nameItemPattern.IsMatch(folderName))
                    {
                        foundMatch.Add(new string[] { folderName, null });
                    }
                }

                // loop through each enum name specified in SpecialFolderEnumNames
                foreach (string[] matchItem in foundMatch)
                {
                    string enumName    = matchItem[0];
                    string altName     = matchItem[1];
                    bool   isAltName   = (altName != null);
                    string displayName = isAltName ? altName : enumName;

                    SpecialFolder specialFolder;
                    if (Enum.TryParse(enumName, true, out specialFolder))
                    {
                        PSObject responseObj = new PSObject();
                        responseObj.Members.Add(new PSNoteProperty("Name", displayName));
                        responseObj.Members.Add(new PSNoteProperty("Path",
                                                                   WindowsUtility.GetSpecialFolder(specialFolder)
                                                                   ));
                        if (isAltName)
                        {
                            responseObj.Members.Add(new PSNoteProperty("IsAlias", true));
                            responseObj.Members.Add(new PSNoteProperty("Alias", enumName));
                        }

                        base.WriteObject(responseObj);
                    }
                    else
                    {
                        // this shouldn't happen. Keep here for debugging purposes.
                        throw new InvalidOperationException(string.Format(RS.InvalidEnumName, enumName));
                    }
                }
            }
        }