コード例 #1
0
        public object GetDynamicParameters()
        {
            var profileNames = UserProfileDirectory.GetUserProfiles(ShowHidden).Select(p => p.Name).ToArray();

            var attributeCollection = new Collection <Attribute>
            {
                new ParameterAttribute {
                    Position = 0, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true
                },
                new ValidateSetAttribute(profileNames)
            };

            var runtimeDictionary = new RuntimeDefinedParameterDictionary
            {
                { "UserName", new RuntimeDefinedParameter("UserName", typeof(string), attributeCollection) }
            };

            return(runtimeDictionary);
        }
コード例 #2
0
        protected override void ProcessRecord()
        {
            base.ProcessRecord();

            MyInvocation.BoundParameters.TryGetValue("UserName", out var boundParameter);
            var userNames = boundParameter is string str ? new[] { str } : (string[])boundParameter;

            if (userNames == null)
            {
                var profiles = UserProfileDirectory.GetUserProfiles(ShowHidden);

                WriteObject(profiles.ToArray().ToPSObject());
            }
            else
            {
                WriteObject(userNames
                            .Select(u => new DirectoryInfo(Path.Combine(UserProfileDirectory.DirectoryInfo.FullName, u)))
                            .ToArray()
                            .ToPSObject());
            }
        }
コード例 #3
0
        protected override void ProcessRecord()
        {
            base.ProcessRecord();

            switch (ParameterSetName)
            {
            case "Scope":
                if ((Scope & TempFileScope.AllUsers) > 0)
                {
                    foreach (var userProfile in UserProfileDirectory.GetUserProfiles(false))
                    {
                        var userTemp = new DirectoryInfo(Path.Combine(userProfile.FullName, "AppData\\Local\\Temp"));
                        if (!userTemp.Exists)
                        {
                            continue;
                        }
                        try
                        {
                            userTemp.TryRecursiveDelete(false, out var inaccessible);

                            if (inaccessible > 0)
                            {
                                WriteWarning($"{inaccessible} file system objects could not be deleted because they were in use or could not be accessed.");
                            }
                        }
                        catch (Exception e)
                        {
                            WriteError(e.ToErrorRecord());
                        }
                    }
                }
                else if ((Scope & TempFileScope.System) > 0)
                {
                    var windowsPath = Path.GetDirectoryName(Environment.GetFolderPath(Environment.SpecialFolder.System));
                    var windowsTemp = new DirectoryInfo(Path.Combine(windowsPath, "Temp"));

                    try
                    {
                        windowsTemp.TryRecursiveDelete(false, out var inaccessible);

                        if (inaccessible > 0)
                        {
                            WriteWarning($"{inaccessible} file system objects could not be deleted because they were in use or could not be accessed.");
                        }
                    }
                    catch (Exception e)
                    {
                        WriteError(e.ToErrorRecord());
                    }
                }
                break;

            case "Pipeline":

                foreach (var item in InputObject)
                {
                    if (item.Parent == null || !item.Parent.Name.Equals("users", StringComparison.CurrentCultureIgnoreCase))
                    {
                        throw new ArgumentException("The input value is invalid because the parent directory does not match the user profiles directory name.");
                    }

                    var usertemp = new DirectoryInfo(Path.Combine(item.FullName, "AppData\\Local\\Temp"));
                    if (!usertemp.Exists)
                    {
                        continue;
                    }
                    try
                    {
                        usertemp.TryRecursiveDelete(false, out var inaccessible);

                        WriteVerbose($"Deleted all files not in use from \"{usertemp.FullName}\".");

                        if (inaccessible > 0)
                        {
                            WriteWarning($"{inaccessible} file system objects could not be deleted because they were in use or could not be accessed.");
                        }
                    }
                    catch (Exception e)
                    {
                        WriteError(e.ToErrorRecord());
                    }
                }
                break;
            }
        }