如何在PowerShell中输出两个不同的哈希表

时间:2018-07-20 10:55:20

标签: powershell hashtable

我正在尝试使用第一个功能来获取目录的所有权限,并使用第二个功能来获取继承中断的文件夹。然后,我想使用第三个函数输出它们两者,但是我得到的只是第一个结果“权限”,而没有第二个结果!

function Get-Permissions3($folder) {
    $Paths = Get-ChildItem $folder -Recurse 
    foreach ($p in $Paths) {
        $Permissions = (Get-Acl $p.FullName).Access
        $Permissiontable = $Permissions |
                           Select-Object @{name="FullName";expression={$p.FullName}},
                               @{name="IdentityReference";expression={$_.IdentityReference}},
                               @{name="FileSystemRights";expression={$_.FileSystemRights}},
                               @{name="IsInherited";expression={$_.IsInherited}}
        $Permissiontable
    }
} 

function Get-BrokenInheritance($Directory) {
    $D = Get-ChildItem $Directory -Directory -Recurse |
         Get-Acl |
         where {$_.Access.IsInherited -eq $false}
    $BrokenInheritance = $D | Select-Object @{name="Without Inheritance";expression={$_.Path}}
    $BrokenInheritance
}

function Get-FolderAnalysis($Path) {
    Write-Output "Output Permissions"
    Get-Permissions3($Path)

    Write-Output "Output Broken Inheritance"
    Get-BrokenInheritance($Path)
}

1 个答案:

答案 0 :(得分:1)

在第三个函数(即Get-FolderAnalysis)中,您实际上并没有合并前两个函数的结果。您只是在单独的行上调用它们,仅此而已。因此,您看不到两个输出。您可以像下面一样使用Calculated properties来克服它-

Function Get-FolderAnalysis($Path)
{  
    Write-Output "Output Permissions"
    $Permissions = Get-Permissions3($Path)

    Write-Output "Output Broken Inheritance"
    $BrokenInheritance = Get-BrokenInheritance($Path)

    $Permissions | Select-Object *, @{ name="Output Broken Inheritance"; expression={Get-BrokenInheritance($Path)}}
}