|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238 |
- using Alphaleonis.Win32.Filesystem;
- using Security2;
- using System;
- using System.Management.Automation;
-
- namespace NTFSSecurity
- {
- [Cmdlet(VerbsCommon.Set, "NTFSInheritance", DefaultParameterSetName = "Path")]
- public class SetInheritance : BaseCmdletWithPrivControl
- {
- private bool? accessInheritanceEnabled;
- private bool? auditInheritanceEnabled;
- private bool passThru;
-
- [Parameter(Position = 1, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true, ParameterSetName = "Path")]
- [ValidateNotNullOrEmpty]
- [Alias("FullName")]
- public string[] Path
- {
- get { return paths.ToArray(); }
- set
- {
- paths.Clear();
- paths.AddRange(value);
- }
- }
-
- [Parameter(Mandatory = true, Position = 1, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true, ParameterSetName = "SecurityDescriptor")]
- [ValidateNotNullOrEmpty]
- public FileSystemSecurity2[] SecurityDescriptor
- {
- get { return securityDescriptors.ToArray(); }
- set
- {
- securityDescriptors.Clear();
- securityDescriptors.AddRange(value);
- }
- }
-
- [Parameter(ValueFromPipelineByPropertyName = true)]
- public bool? AccessInheritanceEnabled
- {
- get { return accessInheritanceEnabled; }
- set { accessInheritanceEnabled = value; }
- }
-
- [Parameter(ValueFromPipelineByPropertyName = true)]
- public bool? AuditInheritanceEnabled
- {
- get { return auditInheritanceEnabled; }
- set { auditInheritanceEnabled = value; }
- }
-
- [Parameter]
- public SwitchParameter PassThru
- {
- get { return passThru; }
- set { passThru = value; }
- }
-
- protected override void BeginProcessing()
- {
- base.BeginProcessing();
- EnableFileSystemPrivileges(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));
- }
- }
- }
- }
- }
- }
|