Объединение результатов Get-ACL в 1 объект

Прошу прощения за мой 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 ...}

Как я могу справиться с этим, чтобы получить результат, подобный первому объекту, а затем, в свою очередь, добавить больше к настраиваемому объекту?

Заранее спасибо, Лу

1 ответ

Исходя из вашего описания, я думаю, что вам нужен просто набор объектов, т.е. $aclObjectList

Этот сценарий захватывает коллекцию, где каждый объект является типом объекта, возвращаемого get-acl. Я делаю это только для того, чтобы показать вам свойство пути каждого объекта, чтобы продемонстрировать, что каждый объект относится к одной из трех задействованных папок.

Затем скрипт перебирает массив get-acl объекты и выводит path а также IdentityReference каждого

Если вы хотите экспортировать один объект, то экспортируйте $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 = "";... 
Другие вопросы по тегам