如何在一个foreach循环中使用2个条件

时间:2019-06-17 09:47:27

标签: powershell scripting

我有一个脚本片段。这给了我一个具有2个属性的数组:Account和AccessRights。现在,我想构建一个foreach循环,但是我还需要将第二个值存储在变量中以备将来使用。

所以,如果我这样做:

foreach ($id in $ACLFile.Account) {
    # do stuff
}

我只有“帐户”属性保存在$id中。但是,如何获得其AccessRights值?

$ACLFile = GetNTFSAccess | select Account, AccessRights
$ACLGroup = $ACLFile | Group-Object Account
$Singles = $ACLGroup.Where({$_.Count -eq 1}).Group
$Duplicates = $ACLGroup.Where({$_.Count -gt 1})
$ItemizedDuplicates = $Duplicates | foreach {
    [PSCustomObject][ordered]@{
        "Account"=$_.Group.Account[0];
        "AccessRights" = $_.Group.AccessRights -join ", "
    }
}
@($ItemizedDuplicates, $Singles)

1 个答案:

答案 0 :(得分:4)

遍历对象,而不只是一个属性。

foreach ($acl in $ACLFile) {
    $id     = $acl.Account
    $access = $acl.AccessRights
    # ...
}