public static IEnumerable <FileACL> Get_PathAcl(Args_Get_PathAcl args = null) { return(GetPathAcl.Get_PathAcl(args)); }
public static IEnumerable <FileACL> Get_PathAcl(Args_Get_PathAcl args = null) { if (args == null) { args = new Args_Get_PathAcl(); } var ConvertArguments = new Args_ConvertFrom_SID { Credential = args.Credential }; var MappedComputers = new Dictionary <string, bool>(); var FileACLs = new List <FileACL>(); foreach (var TargetPath in args.Path) { try { if (TargetPath.IsRegexMatch(@"\\\\.*\\.*") && args.Credential != null) { var HostComputer = new System.Uri(TargetPath).Host; if (!MappedComputers[HostComputer]) { // map IPC$ to this computer if it's not already AddRemoteConnection.Add_RemoteConnection(new Args_Add_RemoteConnection { ComputerName = new string[] { HostComputer }, Credential = args.Credential }); MappedComputers[HostComputer] = true; } } FileSystemSecurity ACL; var attr = File.GetAttributes(TargetPath); if (attr.HasFlag(FileAttributes.Directory)) { ACL = Directory.GetAccessControl(TargetPath); } else { ACL = File.GetAccessControl(TargetPath); } var arc = ACL.GetAccessRules(true, true, typeof(System.Security.Principal.SecurityIdentifier)); foreach (FileSystemAccessRule ar in arc) { var SID = ar.IdentityReference.Value; ConvertArguments.ObjectSID = new string[] { SID }; var Name = ConvertFromSID.ConvertFrom_SID(ConvertArguments); var Out = new FileACL { Path = TargetPath, FileSystemRights = Convert_FileRight((uint)ar.FileSystemRights), IdentityReference = Name, IdentitySID = SID, AccessControlType = ar.AccessControlType }; FileACLs.Add(Out); } } catch (Exception e) { Logger.Write_Verbose($@"[Get-PathAcl] error: {e}"); } } // remove the IPC$ mappings RemoveRemoteConnection.Remove_RemoteConnection(new Args_Remove_RemoteConnection { ComputerName = MappedComputers.Keys.ToArray() }); return(FileACLs); }