Beispiel #1
0
        protected override void ProcessRecord()
        {
            if (ParameterSetName == "Path")
            {
                foreach (var path in paths)
                {
                    FileSystemInfo item = null;

                    try
                    {
                        item = GetFileSystemInfo2(path);
                    }
                    catch (Exception ex)
                    {
                        WriteError(new ErrorRecord(ex, "ReadFileError", ErrorCategory.OpenError, path));
                        continue;
                    }

                    try
                    {
                        FileSystemInheritanceInfo.EnableAuditInheritance(item, removeExplicitAccessRules);
                    }
                    catch (UnauthorizedAccessException)
                    {
                        try
                        {
                            var ownerInfo     = FileSystemOwner.GetOwner(item);
                            var previousOwner = ownerInfo.Owner;

                            FileSystemOwner.SetOwner(item, System.Security.Principal.WindowsIdentity.GetCurrent().User);

                            FileSystemInheritanceInfo.EnableAuditInheritance(item, removeExplicitAccessRules);

                            FileSystemOwner.SetOwner(item, previousOwner);
                        }
                        catch (Exception ex2)
                        {
                            WriteError(new ErrorRecord(ex2, "ModifySdError", ErrorCategory.WriteError, path));
                            continue;
                        }
                    }
                    catch (Exception ex)
                    {
                        WriteError(new ErrorRecord(ex, "ModifySdError", ErrorCategory.WriteError, path));
                        continue;
                    }
                    finally
                    {
                        if (passThru)
                        {
                            FileSystemInheritanceInfo.GetFileSystemInheritanceInfo(item);
                        }
                    }
                }
            }
            else
            {
                foreach (var sd in securityDescriptors)
                {
                    FileSystemInheritanceInfo.EnableAuditInheritance(sd, removeExplicitAccessRules);

                    if (passThru)
                    {
                        FileSystemInheritanceInfo.GetFileSystemInheritanceInfo(sd);
                    }
                }
            }
        }
        protected override void ProcessRecord()
        {
            if (ParameterSetName == "Path")
            {
                FileSystemInfo item = null;

                foreach (var path in paths)
                {
                    try
                    {
                        item = GetFileSystemInfo2(path);
                    }
                    catch (Exception ex)
                    {
                        WriteError(new ErrorRecord(ex, "ReadFileError", ErrorCategory.OpenError, path));
                        continue;
                    }

                    try
                    {
                        FileSystemAccessRule2.RemoveFileSystemAccessRuleAll(item);
                        if (disableInheritance)
                        {
                            FileSystemInheritanceInfo.DisableAccessInheritance(item, true);
                        }
                    }
                    catch (UnauthorizedAccessException)
                    {
                        try
                        {
                            var ownerInfo     = FileSystemOwner.GetOwner(item);
                            var previousOwner = ownerInfo.Owner;

                            FileSystemOwner.SetOwner(item, System.Security.Principal.WindowsIdentity.GetCurrent().User);

                            FileSystemAccessRule2.RemoveFileSystemAccessRuleAll(item);
                            if (disableInheritance)
                            {
                                FileSystemInheritanceInfo.DisableAccessInheritance(item, true);
                            }

                            FileSystemOwner.SetOwner(item, previousOwner);
                        }
                        catch (Exception ex2)
                        {
                            WriteError(new ErrorRecord(ex2, "ClearAclError", ErrorCategory.WriteError, path));
                        }
                    }
                    catch (Exception ex)
                    {
                        WriteError(new ErrorRecord(ex, "ClearAclError", ErrorCategory.WriteError, path));
                    }
                }
            }
            else
            {
                foreach (var sd in securityDescriptors)
                {
                    FileSystemAccessRule2.RemoveFileSystemAccessRuleAll(sd);
                    if (disableInheritance)
                    {
                        FileSystemInheritanceInfo.DisableAccessInheritance(sd, true);
                    }
                }
            }
        }
        protected override void ProcessRecord()
        {
            if (ParameterSetName == "Path")
            {
                foreach (var path in paths)
                {
                    FileSystemInfo item = null;

                    try
                    {
                        item = GetFileSystemInfo2(path);
                    }
                    catch (Exception ex)
                    {
                        WriteError(new ErrorRecord(ex, "ReadFileError", ErrorCategory.OpenError, path));
                        continue;
                    }

                    try
                    {
                        var currentState = FileSystemInheritanceInfo.GetFileSystemInheritanceInfo(item);

                        if (currentState.AccessInheritanceEnabled != accessInheritanceEnabled)
                        {
                            WriteVerbose("AccessInheritanceEnabled not equal");
                            if (accessInheritanceEnabled.Value)
                            {
                                WriteVerbose("Calling EnableAccessInheritance");
                                FileSystemInheritanceInfo.EnableAccessInheritance(item, false);
                            }
                            else
                            {
                                WriteVerbose("Calling DisableAccessInheritance");
                                FileSystemInheritanceInfo.DisableAccessInheritance(item, true);
                            }
                        }
                        else
                        {
                            WriteVerbose("AccessInheritanceEnabled is equal - no change was done");
                        }

                        if (currentState.AuditInheritanceEnabled != auditInheritanceEnabled)
                        {
                            WriteVerbose("AuditInheritanceEnabled not equal");
                            if (auditInheritanceEnabled.Value)
                            {
                                WriteVerbose("Calling EnableAuditInheritance");
                                FileSystemInheritanceInfo.EnableAuditInheritance(item, true);
                            }
                            else
                            {
                                WriteVerbose("Calling DisableAuditInheritance");
                                FileSystemInheritanceInfo.DisableAuditInheritance(item, false);
                            }
                        }
                        else
                        {
                            WriteVerbose("AuditInheritanceEnabled is equal - no change was done");
                        }
                    }
                    catch (UnauthorizedAccessException)
                    {
                        try
                        {
                            var ownerInfo     = FileSystemOwner.GetOwner(item);
                            var previousOwner = ownerInfo.Owner;

                            FileSystemOwner.SetOwner(item, System.Security.Principal.WindowsIdentity.GetCurrent().User);

                            var currentState = FileSystemInheritanceInfo.GetFileSystemInheritanceInfo(item);

                            if (currentState.AccessInheritanceEnabled != accessInheritanceEnabled)
                            {
                                WriteVerbose("AccessInheritanceEnabled not equal");
                                if (accessInheritanceEnabled.Value)
                                {
                                    WriteVerbose("Calling EnableAccessInheritance");
                                    FileSystemInheritanceInfo.EnableAccessInheritance(item, false);
                                }
                                else
                                {
                                    WriteVerbose("Calling DisableAccessInheritance");
                                    FileSystemInheritanceInfo.DisableAccessInheritance(item, true);
                                }
                            }
                            else
                            {
                                WriteVerbose("AccessInheritanceEnabled is equal - no change was done");
                            }

                            if (currentState.AuditInheritanceEnabled != auditInheritanceEnabled)
                            {
                                WriteVerbose("AuditInheritanceEnabled not equal");
                                if (auditInheritanceEnabled.Value)
                                {
                                    WriteVerbose("Calling EnableAuditInheritance");
                                    FileSystemInheritanceInfo.EnableAuditInheritance(item, true);
                                }
                                else
                                {
                                    WriteVerbose("Calling DisableAuditInheritance");
                                    FileSystemInheritanceInfo.DisableAuditInheritance(item, false);
                                }
                            }
                            else
                            {
                                WriteVerbose("AuditInheritanceEnabled is equal - no change was done");
                            }

                            FileSystemOwner.SetOwner(item, previousOwner);
                        }
                        catch (Exception ex2)
                        {
                            WriteError(new ErrorRecord(ex2, "ModifySdError", ErrorCategory.WriteError, path));
                            continue;
                        }
                    }
                    catch (Exception ex)
                    {
                        WriteError(new ErrorRecord(ex, "ModifySdError", ErrorCategory.WriteError, path));
                        continue;
                    }
                    finally
                    {
                        if (passThru)
                        {
                            WriteObject(FileSystemInheritanceInfo.GetFileSystemInheritanceInfo(item));
                        }
                    }
                }
            }
            else
            {
                foreach (var sd in securityDescriptors)
                {
                    var currentState = FileSystemInheritanceInfo.GetFileSystemInheritanceInfo(sd);

                    if (currentState.AccessInheritanceEnabled != accessInheritanceEnabled)
                    {
                        WriteVerbose("AccessInheritanceEnabled not equal");
                        if (accessInheritanceEnabled.Value)
                        {
                            WriteVerbose("Calling EnableAccessInheritance");
                            FileSystemInheritanceInfo.EnableAccessInheritance(sd, false);
                        }
                        else
                        {
                            WriteVerbose("Calling DisableAccessInheritance");
                            FileSystemInheritanceInfo.DisableAccessInheritance(sd, true);
                        }
                    }
                    else
                    {
                        WriteVerbose("AccessInheritanceEnabled is equal - no change was done");
                    }

                    if (currentState.AuditInheritanceEnabled != auditInheritanceEnabled)
                    {
                        WriteVerbose("AuditInheritanceEnabled not equal");
                        if (auditInheritanceEnabled.Value)
                        {
                            WriteVerbose("Calling EnableAuditInheritance");
                            FileSystemInheritanceInfo.EnableAuditInheritance(sd, true);
                        }
                        else
                        {
                            WriteVerbose("Calling DisableAuditInheritance");
                            FileSystemInheritanceInfo.DisableAuditInheritance(sd, false);
                        }
                    }
                    else
                    {
                        WriteVerbose("AuditInheritanceEnabled is equal - no change was done");
                    }

                    if (passThru)
                    {
                        WriteObject(FileSystemInheritanceInfo.GetFileSystemInheritanceInfo(sd));
                    }
                }
            }
        }