0

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

在非常基本的级别上,我想要的只是将不同文件夹的不同结果组合起来以获取以下代码:

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

这给了我一个结果:

Value                                         
-----                                         
NT AUTHORITY\SYSTEM                           
BUILTIN\Administrators                        
Etc
Etc

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


Folder1                      Folder2                                    
-----                        -------                           
NT AUTHORITY\SYSTEM          NT AUTHORITY\SYSTEM                  
BUILTIN\Administrators       BUILTIN\Administrators                 
Etc                          Etc
Etc                          Etc 

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

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

给我:

Folder1                                                                        
-------                                                                        
{NT AUTHORITY\SYSTEM, BUILTIN\Administrators, etc, etc ...}

我怎样才能处理这个给我一个像第一个对象一样的结果,然后又向自定义对象添加更多?

在此先感谢,娄

4

1 回答 1

0

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

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

然后,脚本循环遍历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:\Temp\topFolder\Folder 1"
$aclObject  = (get-acl $path)
$aclObjectList.Add($aclObject) | Out-Null

$path = "C:\Temp\topFolder\Folder 2"
$aclObject  = (get-acl $path)
$aclObjectList.Add($aclObject) | Out-Null

$path = "C:\Temp\topFolder\Folder 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.Core\FileSystem::C:\Temp\topFolder\Folder 1
item=BUILTIN\Administrators
item=NT AUTHORITY\SYSTEM
item=BUILTIN\Users
item=NT AUTHORITY\Authenticated Users
item=NT AUTHORITY\Authenticated Users

Microsoft.PowerShell.Core\FileSystem::C:\Temp\topFolder\Folder 2
item=BUILTIN\Administrators
item=NT AUTHORITY\SYSTEM
item=BUILTIN\Users
item=NT AUTHORITY\Authenticated Users
item=NT AUTHORITY\Authenticated Users

Microsoft.PowerShell.Core\FileSystem::C:\Temp\topFolder\Folder 3
item=BUILTIN\Administrators
item=NT AUTHORITY\SYSTEM
item=BUILTIN\Users
item=NT AUTHORITY\Authenticated Users
item=NT AUTHORITY\Authenticated 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.Core\FileSystem::C:\Temp\topFolder                                                                                  
PSPath                          NoteProperty   string PSPath=Microsoft.PowerShell.Core\FileSystem::C:\Temp\topFolder\Folder 1                                                                               
PSProvider                      NoteProperty   ProviderInfo PSProvider=Microsoft.PowerShell.Core\FileSystem                                                                                                 
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 = "";... 
于 2019-09-15T02:56:44.447 回答