【问题标题】:How To output two defferent Hashtables in PowerShell [duplicate]如何在PowerShell中输出两个不同的哈希表[重复]
【发布时间】:2018-07-20 10:55:20
【问题描述】:

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

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)
}

【问题讨论】:

    标签: powershell hashtable


    【解决方案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)}}
    }
    

    【讨论】:

    • 但我只想要禁用继承的文件夹的名称
    • @Morenkashi 如果您只想要继承中断的文件夹的名称 - 执行此操作 ls -path c:\temp -Recurse| Get-Acl |%{$_|where {$_.access.isinherited -eq $false}} 这对我有用
    • 是的,但我想将其导出为 CSV 文件
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-26
    • 1970-01-01
    • 2019-01-02
    • 2013-06-06
    • 2021-10-15
    • 2016-04-29
    相关资源
    最近更新 更多