将 Get-ACL 结果合并为 1 个对象



请原谅我的初学者级别的Powershell。 我希望能够将 Get-ACL 的不同结果合并到一个对象中,稍后将导出该对象

在最基本的层面上,我想要的只是为下面的代码组合不同文件夹的不同结果:

$test = (get-acl $path).Access | select -ExpandProperty IdentityReference

这给了我一个结果:

Value                                         
-----                                         
NT AUTHORITYSYSTEM                           
BUILTINAdministrators                        
Etc
Etc

我想要一个像这样的东西的对象(加上更多的列,总共大约 4-5 列(:


Folder1                      Folder2                                    
-----                        -------                           
NT AUTHORITYSYSTEM          NT AUTHORITYSYSTEM                  
BUILTINAdministrators       BUILTINAdministrators                 
Etc                          Etc
Etc                          Etc 

我尝试探索构建自定义对象,但找不到一种方法来正确列出对象值,就像我的第一个结果一样

$Custom = New-Object PSObject
$Custom | Add-Member -type NoteProperty -name Folder1 -value $test.value

给我:

Folder1                                                                        
-------                                                                        
{NT AUTHORITYSYSTEM, BUILTINAdministrators, etc, etc ...}

我如何处理这个问题以给我一个像第一个对象这样的结果,然后反过来向自定义对象添加更多?

提前感谢, 楼

根据您的描述,我认为您需要的只是对象的集合,即$aclObjectList

此脚本捕获一个集合,其中每个对象都是get-acl返回的对象类型。我这样做只是为了向您展示每个对象的 path 属性,以证明每个对象都用于所涉及的三个文件夹之一

然后,脚本循环遍历get-acl对象的数组,并输出每个对象的pathIdentityReference

如果要导出单个对象,请导出$aclObjectList

cls
#define and declare an array. A System.Collections.ArrayList can be big and is fast
$aclObjectList = New-Object System.Collections.ArrayList
$aclObjectList.clear()
$path = "C:TemptopFolderFolder 1"
$aclObject  = (get-acl $path)
$aclObjectList.Add($aclObject) | Out-Null
$path = "C:TemptopFolderFolder 2"
$aclObject  = (get-acl $path)
$aclObjectList.Add($aclObject) | Out-Null
$path = "C:TemptopFolderFolder 3"
$aclObject  = (get-acl $path)
$aclObjectList.Add($aclObject) | Out-Null
foreach ($aclObject in $aclObjectList)
{
write-host ($aclObject.Path)
$aclAccessObject = $aclObject.Access | select -ExpandProperty IdentityReference
foreach ($aclAccessItem in $aclAccessObject)
{
write-host ("item=" + $aclAccessItem.Value)
}
write-host
}

输出为:

Microsoft.PowerShell.CoreFileSystem::C:TemptopFolderFolder 1
item=BUILTINAdministrators
item=NT AUTHORITYSYSTEM
item=BUILTINUsers
item=NT AUTHORITYAuthenticated Users
item=NT AUTHORITYAuthenticated Users
Microsoft.PowerShell.CoreFileSystem::C:TemptopFolderFolder 2
item=BUILTINAdministrators
item=NT AUTHORITYSYSTEM
item=BUILTINUsers
item=NT AUTHORITYAuthenticated Users
item=NT AUTHORITYAuthenticated Users
Microsoft.PowerShell.CoreFileSystem::C:TemptopFolderFolder 3
item=BUILTINAdministrators
item=NT AUTHORITYSYSTEM
item=BUILTINUsers
item=NT AUTHORITYAuthenticated Users
item=NT AUTHORITYAuthenticated Users

顺便说一下,get-acl 返回的对象数据类型是 System.Security.AccessControl.DirectorySecurity。例如,您可以通过将$aclObject变量之一管道连接到 Get-Member 来查看这一点:

$aclObject | Get-Member
TypeName: System.Security.AccessControl.DirectorySecurity
Name                            MemberType     Definition                                                                                                                                                   
----                            ----------     ----------                                                                                                                                                   
Access                          CodeProperty   System.Security.AccessControl.AuthorizationRuleCollection Access{get=GetAccess;}                                                                             
CentralAccessPolicyId           CodeProperty   System.Security.Principal.SecurityIdentifier CentralAccessPolicyId{get=GetCentralAccessPolicyId;}                                                            
CentralAccessPolicyName         CodeProperty   System.String CentralAccessPolicyName{get=GetCentralAccessPolicyName;}                                                                                       
Group                           CodeProperty   System.String Group{get=GetGroup;}                                                                                                                           
Owner                           CodeProperty   System.String Owner{get=GetOwner;}                                                                                                                           
Path                            CodeProperty   System.String Path{get=GetPath;}                                                                                                                             
Sddl                            CodeProperty   System.String Sddl{get=GetSddl;}                                                                                                                             
AccessRuleFactory               Method         System.Security.AccessControl.AccessRule AccessRuleFactory(System.Security.Principal.IdentityReference identityReference, int accessMask, bool isInherited...
AddAccessRule                   Method         void AddAccessRule(System.Security.AccessControl.FileSystemAccessRule rule)                                                                                  
AddAuditRule                    Method         void AddAuditRule(System.Security.AccessControl.FileSystemAuditRule rule)                                                                                    
AuditRuleFactory                Method         System.Security.AccessControl.AuditRule AuditRuleFactory(System.Security.Principal.IdentityReference identityReference, int accessMask, bool isInherited, ...
Equals                          Method         bool Equals(System.Object obj)                                                                                                                               
GetAccessRules                  Method         System.Security.AccessControl.AuthorizationRuleCollection GetAccessRules(bool includeExplicit, bool includeInherited, type targetType)                       
GetAuditRules                   Method         System.Security.AccessControl.AuthorizationRuleCollection GetAuditRules(bool includeExplicit, bool includeInherited, type targetType)                        
GetGroup                        Method         System.Security.Principal.IdentityReference GetGroup(type targetType)                                                                                        
GetHashCode                     Method         int GetHashCode()                                                                                                                                            
GetOwner                        Method         System.Security.Principal.IdentityReference GetOwner(type targetType)                                                                                        
GetSecurityDescriptorBinaryForm Method         byte[] GetSecurityDescriptorBinaryForm()                                                                                                                     
GetSecurityDescriptorSddlForm   Method         string GetSecurityDescriptorSddlForm(System.Security.AccessControl.AccessControlSections includeSections)                                                    
GetType                         Method         type GetType()                                                                                                                                               
ModifyAccessRule                Method         bool ModifyAccessRule(System.Security.AccessControl.AccessControlModification modification, System.Security.AccessControl.AccessRule rule, [ref] bool modi...
ModifyAuditRule                 Method         bool ModifyAuditRule(System.Security.AccessControl.AccessControlModification modification, System.Security.AccessControl.AuditRule rule, [ref] bool modified)
PurgeAccessRules                Method         void PurgeAccessRules(System.Security.Principal.IdentityReference identity)                                                                                  
PurgeAuditRules                 Method         void PurgeAuditRules(System.Security.Principal.IdentityReference identity)                                                                                   
RemoveAccessRule                Method         bool RemoveAccessRule(System.Security.AccessControl.FileSystemAccessRule rule)                                                                               
RemoveAccessRuleAll             Method         void RemoveAccessRuleAll(System.Security.AccessControl.FileSystemAccessRule rule)                                                                            
RemoveAccessRuleSpecific        Method         void RemoveAccessRuleSpecific(System.Security.AccessControl.FileSystemAccessRule rule)                                                                       
RemoveAuditRule                 Method         bool RemoveAuditRule(System.Security.AccessControl.FileSystemAuditRule rule)                                                                                 
RemoveAuditRuleAll              Method         void RemoveAuditRuleAll(System.Security.AccessControl.FileSystemAuditRule rule)                                                                              
RemoveAuditRuleSpecific         Method         void RemoveAuditRuleSpecific(System.Security.AccessControl.FileSystemAuditRule rule)                                                                         
ResetAccessRule                 Method         void ResetAccessRule(System.Security.AccessControl.FileSystemAccessRule rule)                                                                                
SetAccessRule                   Method         void SetAccessRule(System.Security.AccessControl.FileSystemAccessRule rule)                                                                                  
SetAccessRuleProtection         Method         void SetAccessRuleProtection(bool isProtected, bool preserveInheritance)                                                                                     
SetAuditRule                    Method         void SetAuditRule(System.Security.AccessControl.FileSystemAuditRule rule)                                                                                    
SetAuditRuleProtection          Method         void SetAuditRuleProtection(bool isProtected, bool preserveInheritance)                                                                                      
SetGroup                        Method         void SetGroup(System.Security.Principal.IdentityReference identity)                                                                                          
SetOwner                        Method         void SetOwner(System.Security.Principal.IdentityReference identity)                                                                                          
SetSecurityDescriptorBinaryForm Method         void SetSecurityDescriptorBinaryForm(byte[] binaryForm), void SetSecurityDescriptorBinaryForm(byte[] binaryForm, System.Security.AccessControl.AccessContr...
SetSecurityDescriptorSddlForm   Method         void SetSecurityDescriptorSddlForm(string sddlForm), void SetSecurityDescriptorSddlForm(string sddlForm, System.Security.AccessControl.AccessControlSectio...
ToString                        Method         string ToString()                                                                                                                                            
PSChildName                     NoteProperty   string PSChildName=Folder 1                                                                                                                                  
PSDrive                         NoteProperty   PSDriveInfo PSDrive=C                                                                                                                                        
PSParentPath                    NoteProperty   string PSParentPath=Microsoft.PowerShell.CoreFileSystem::C:TemptopFolder                                                                                  
PSPath                          NoteProperty   string PSPath=Microsoft.PowerShell.CoreFileSystem::C:TemptopFolderFolder 1                                                                               
PSProvider                      NoteProperty   ProviderInfo PSProvider=Microsoft.PowerShell.CoreFileSystem                                                                                                 
AccessRightType                 Property       type AccessRightType {get;}                                                                                                                                  
AccessRuleType                  Property       type AccessRuleType {get;}                                                                                                                                   
AreAccessRulesCanonical         Property       bool AreAccessRulesCanonical {get;}                                                                                                                          
AreAccessRulesProtected         Property       bool AreAccessRulesProtected {get;}                                                                                                                          
AreAuditRulesCanonical          Property       bool AreAuditRulesCanonical {get;}                                                                                                                           
AreAuditRulesProtected          Property       bool AreAuditRulesProtected {get;}                                                                                                                           
AuditRuleType                   Property       type AuditRuleType {get;}                                                                                                                                    
AccessToString                  ScriptProperty System.Object AccessToString {get=$toString = "";...                                                                                                         
AuditToString                   ScriptProperty System.Object AuditToString {get=$toString = "";... 

最新更新