Esempio n. 1
0
        protected override void ProcessRecord()
        {
            IEnumerable <FileSystemAccessRule2> acl = null;
            FileSystemInfo item = null;

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

                try
                {
                    acl = FileSystemAccessRule2.GetFileSystemAccessRules(item, !ExcludeExplicit, !ExcludeInherited, getInheritedFrom);
                }
                catch (UnauthorizedAccessException)
                {
                    try
                    {
                        var ownerInfo     = FileSystemOwner.GetOwner(item);
                        var previousOwner = ownerInfo.Owner;

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

                        acl = FileSystemAccessRule2.GetFileSystemAccessRules(item, !ExcludeExplicit, !ExcludeInherited, getInheritedFrom);

                        FileSystemOwner.SetOwner(item, previousOwner);
                    }
                    catch (Exception ex2)
                    {
                        this.WriteError(new ErrorRecord(ex2, "AddAceError", ErrorCategory.WriteError, path));
                    }
                }
                catch (Exception ex)
                {
                    this.WriteWarning(string.Format("Could not read item {0}. The error was: {1}", path, ex.Message));
                }
                finally
                {
                    if (acl != null)
                    {
                        var orphanedAces = acl.Where(ace => string.IsNullOrEmpty(ace.Account.AccountName));
                        orphanedSidCount += orphanedAces.Count();

                        WriteVerbose(string.Format("Item {0} knows about {1} orphaned SIDs in its ACL", path, orphanedAces.Count()));

                        orphanedAces.ForEach(ace => WriteObject(ace));
                    }
                }
            }
        }
Esempio n. 2
0
        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
                    {
                        WriteObject(FileSystemOwner.GetOwner(item));
                    }
                    catch (UnauthorizedAccessException)
                    {
                        try
                        {
                            var ownerInfo     = FileSystemOwner.GetOwner(item);
                            var previousOwner = ownerInfo.Owner;

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

                            WriteObject(FileSystemOwner.GetOwner(item));

                            FileSystemOwner.SetOwner(item, previousOwner);
                        }
                        catch (Exception ex2)
                        {
                            WriteError(new ErrorRecord(ex2, "ReadSecurityError", ErrorCategory.WriteError, path));
                            continue;
                        }
                    }
                    catch (Exception ex)
                    {
                        WriteError(new ErrorRecord(ex, "ReadSecurityError", ErrorCategory.OpenError, path));
                        continue;
                    }
                }
            }
            else
            {
                foreach (var sd in securityDescriptors)
                {
                    WriteObject(FileSystemOwner.GetOwner(sd));
                }
            }
        }
Esempio n. 3
0
        protected override void ProcessRecord()
        {
            string         hash = string.Empty;
            FileSystemInfo item = null;

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

                try
                {
                    hash = ((FileInfo)item).GetHash(algorithm);
                }
                catch (UnauthorizedAccessException)
                {
                    try
                    {
                        var ownerInfo     = FileSystemOwner.GetOwner(item);
                        var previousOwner = ownerInfo.Owner;

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

                        hash = ((FileInfo)item).GetHash(algorithm);

                        FileSystemOwner.SetOwner(item, previousOwner);
                    }
                    catch (Exception ex2)
                    {
                        WriteError(new ErrorRecord(ex2, "GetHashError", ErrorCategory.WriteError, path));
                    }
                }
                catch (Exception ex)
                {
                    WriteError(new ErrorRecord(ex, "GetHashError", ErrorCategory.WriteError, path));
                }

                var result = new PSObject(item);
                result.Properties.Add(new PSNoteProperty("Hash", hash));
                result.Properties.Add(new PSNoteProperty("Algorithm", algorithm.ToString()));
                result.TypeNames.Insert(0, "Alphaleonis.Win32.Filesystem.FileInfo+Hash");
                WriteObject(result);
            }
        }
Esempio n. 4
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
                    {
                        FileSystemOwner.SetOwner(item, account);
                        WriteDebug("Owner set on item {0}", item.FullName);

                        if (passThru)
                        {
                            WriteObject(FileSystemOwner.GetOwner(item));
                        }
                    }
                    catch (Exception ex)
                    {
                        WriteError(new ErrorRecord(ex, "SetOwnerError", ErrorCategory.WriteError, path));
                        continue;
                    }
                }
            }
            else
            {
                foreach (var sd in securityDescriptors)
                {
                    FileSystemOwner.SetOwner(sd, account);

                    if (passThru)
                    {
                        WriteObject(FileSystemOwner.GetOwner(sd));
                    }
                }
            }
        }
        protected override void ProcessRecord()
        {
            foreach (var sd in securityDescriptors)
            {
                try
                {
                    sd.Write();

                    if (passThru)
                    {
                        WriteObject(new FileSystemSecurity2(sd.Item));
                    }
                }
                catch (UnauthorizedAccessException)
                {
                    try
                    {
                        var ownerInfo     = FileSystemOwner.GetOwner(sd.Item);
                        var previousOwner = ownerInfo.Owner;

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

                        sd.Write();

                        FileSystemOwner.SetOwner(sd.Item, previousOwner);
                    }
                    catch (Exception ex2)
                    {
                        WriteError(new ErrorRecord(ex2, "WriteSdError", ErrorCategory.WriteError, sd.Item));
                        continue;
                    }
                }
                catch (Exception ex)
                {
                    WriteError(new ErrorRecord(ex, "WriteSdError", ErrorCategory.WriteError, sd.Item));
                }
            }
        }
Esempio n. 6
0
        protected override void ProcessRecord()
        {
            if (ParameterSetName.EndsWith("Simple"))
            {
                FileSystemSecurity2.ConvertToFileSystemFlags(appliesTo, out inheritanceFlags, out propagationFlags);
            }

            if (ParameterSetName.StartsWith("Path"))
            {
                foreach (var path in paths)
                {
                    FileSystemInfo item = null;

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

                    if (ParameterSetName == "PathSimple")
                    {
                        FileSystemSecurity2.ConvertToFileSystemFlags(appliesTo, out inheritanceFlags, out propagationFlags);
                    }

                    try
                    {
                        FileSystemAccessRule2.RemoveFileSystemAccessRule(item, account.ToList(), accessRights, accessType, inheritanceFlags, propagationFlags);
                    }
                    catch (UnauthorizedAccessException)
                    {
                        try
                        {
                            var ownerInfo     = FileSystemOwner.GetOwner(item);
                            var previousOwner = ownerInfo.Owner;

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

                            FileSystemAccessRule2.RemoveFileSystemAccessRule(item, account.ToList(), accessRights, accessType, inheritanceFlags, propagationFlags);

                            FileSystemOwner.SetOwner(item, previousOwner);
                        }
                        catch (Exception ex2)
                        {
                            WriteError(new ErrorRecord(ex2, "RemoveAceError", ErrorCategory.WriteError, path));
                        }
                    }
                    catch (Exception ex)
                    {
                        WriteError(new ErrorRecord(ex, "RemoveAceError", ErrorCategory.WriteError, path));
                    }

                    if (passThru == true)
                    {
                        FileSystemAccessRule2.GetFileSystemAccessRules(item, true, true).ForEach(ace => WriteObject(ace));
                    }
                }
            }
            else
            {
                foreach (var sd in securityDescriptors)
                {
                    FileSystemAccessRule2.RemoveFileSystemAccessRule(sd, account.ToList(), accessRights, accessType, inheritanceFlags, propagationFlags);

                    if (passThru == true)
                    {
                        FileSystemAccessRule2.GetFileSystemAccessRules(sd, true, true).ForEach(ace => WriteObject(ace));
                    }
                }
            }
        }
        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.DisableAccessInheritance(item, removeInheritedAccessRules);
                    }
                    catch (UnauthorizedAccessException)
                    {
                        try
                        {
                            var ownerInfo     = FileSystemOwner.GetOwner(item);
                            var previousOwner = ownerInfo.Owner;

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

                            FileSystemInheritanceInfo.DisableAccessInheritance(item, removeInheritedAccessRules);

                            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.DisableAccessInheritance(sd, removeInheritedAccessRules);

                    if (passThru)
                    {
                        FileSystemInheritanceInfo.GetFileSystemInheritanceInfo(sd);
                    }
                }
            }
        }
Esempio n. 8
0
        protected override void ProcessRecord()
        {
            IEnumerable <FileSystemAuditRule2> acl = null;
            FileSystemInfo item = null;

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

                    try
                    {
                        acl = FileSystemAuditRule2.GetFileSystemAuditRules(item, !excludeExplicit, !excludeInherited, getInheritedFrom);
                    }
                    catch (UnauthorizedAccessException)
                    {
                        try
                        {
                            var ownerInfo     = FileSystemOwner.GetOwner(item);
                            var previousOwner = ownerInfo.Owner;

                            FileSystemOwner.SetOwner(item, System.Security.Principal.WindowsIdentity.GetCurrent().User);
                            acl = FileSystemAuditRule2.GetFileSystemAuditRules(item, !excludeExplicit, !excludeInherited, getInheritedFrom);
                            FileSystemOwner.SetOwner(item, previousOwner);
                        }
                        catch (Exception ex2)
                        {
                            WriteError(new ErrorRecord(ex2, "ReadSecurityError", ErrorCategory.WriteError, path));
                            continue;
                        }
                    }
                    catch (Exception ex)
                    {
                        WriteError(new ErrorRecord(ex, "ReadSecurityError", ErrorCategory.OpenError, path));
                        continue;
                    }
                    finally
                    {
                        if (acl != null)
                        {
                            if (account != null)
                            {
                                acl = acl.Where(ace => ace.Account == account);
                            }

                            acl.ForEach(ace => WriteObject(ace));
                        }
                    }
                }
            }
            else
            {
                foreach (var sd in securityDescriptors)
                {
                    acl = FileSystemAuditRule2.GetFileSystemAuditRules(sd, !excludeExplicit, !excludeInherited, getInheritedFrom);

                    if (account != null)
                    {
                        acl = acl.Where(ace => ace.Account == account);
                    }

                    acl.ForEach(ace => WriteObject(ace));
                }
            }
        }
        protected override void ProcessRecord()
        {
            FileSystemInfo item = null;

            foreach (var path in paths)
            {
                EffectiveAccessInfo result = null;

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

                try
                {
                    result = EffectiveAccess.GetEffectiveAccess(item, account, serverName);

                    if (!result.FromRemote)
                    {
                        WriteWarning("The effective rights can only be computed based on group membership on this" +
                                     " computer. For more accurate results, calculate effective access rights on " +
                                     "the target computer");
                    }
                    if (result.OperationFailed && securityPrivilege == null)
                    {
                        var ex = new Exception(string.Format("Could not get effective permissions from machine '{0}' maybe because the 'Security' privilege is not enabled which might be required. Enable the priviliges using 'Enable-Privileges'. The error was '{1}'", serverName, result.AuthzException.Message), result.AuthzException);
                        WriteError(new ErrorRecord(ex, "GetEffectiveAccessError", ErrorCategory.ReadError, item));
                        continue;
                    }
                    else if (result.OperationFailed)
                    {
                        var ex = new Exception(string.Format("Could not get effective permissions from machine '{0}'. The error is '{1}'", serverName, result.AuthzException.Message), result.AuthzException);
                        WriteError(new ErrorRecord(ex, "GetEffectiveAccessError", ErrorCategory.ReadError, item));
                        continue;
                    }

                    if (excludeNoneAccessEntries && result.Ace.AccessRights == FileSystemRights2.None)
                    {
                        continue;
                    }
                }
                //not sure if the following catch block willb be invoked, testing needed.
                catch (UnauthorizedAccessException)
                {
                    try
                    {
                        var ownerInfo     = FileSystemOwner.GetOwner(item);
                        var previousOwner = ownerInfo.Owner;

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

                        //--------------------

                        result = EffectiveAccess.GetEffectiveAccess(item, account, serverName);

                        if (!result.FromRemote)
                        {
                            WriteWarning("The effective rights can only be computed based on group membership on this" +
                                         " computer. For more accurate results, calculate effective access rights on " +
                                         "the target computer");
                        }
                        if (result.OperationFailed && securityPrivilege == null)
                        {
                            var ex = new Exception(string.Format("Could not get effective permissions from machine '{0}' maybe because the 'Security' privilege is not enabled which might be required. Enable the priviliges using 'Enable-Privileges'. The error was '{1}'", serverName, result.AuthzException.Message), result.AuthzException);
                            WriteError(new ErrorRecord(ex, "GetEffectiveAccessError", ErrorCategory.ReadError, item));
                            continue;
                        }
                        else if (result.OperationFailed)
                        {
                            var ex = new Exception(string.Format("Could not get effective permissions from machine '{0}'. The error is '{1}'", serverName, result.AuthzException.Message), result.AuthzException);
                            WriteError(new ErrorRecord(ex, "GetEffectiveAccessError", ErrorCategory.ReadError, item));
                            continue;
                        }

                        if (excludeNoneAccessEntries && result.Ace.AccessRights == FileSystemRights2.None)
                        {
                            continue;
                        }

                        //--------------------

                        FileSystemOwner.SetOwner(item, previousOwner);
                    }
                    catch (Exception ex2)
                    {
                        this.WriteError(new ErrorRecord(ex2, "ReadSecurityError", ErrorCategory.WriteError, path));
                    }
                }
                catch (Exception ex)
                {
                    WriteError(new ErrorRecord(ex, "ReadEffectivePermissionError", ErrorCategory.ReadError, path));
                }
                finally
                {
                    if (result != null)
                    {
                        WriteObject(result.Ace);
                    }
                }
            }
        }
Esempio n. 10
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
                    {
                        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));
                    }
                }
            }
        }