我对PowerShell中的GenericLists有一些疑问。 下面的脚本为特定用户及其组在文件共享上打印所有访问权限。现在,我想在GenericList中添加一个新行,以显示该权限从何处(用户/组)继承。
$User = "Testumgebung\cbruehwiler"
$UserOhneDomain = "cbruehwiler"
$Path = "T:\"
$List = New-Object System.Collections.Generic.List[System.Object]
$Groups = Get-ADPrincipalGroupMembership $UserOhneDomain
$GroupArrayList = New-Object System.Collections.ArrayList
foreach ($Group in $Groups) {
$GroupArrayList.Add($Group.Name) | Out-Null
}
# Fields we want in list, an array of calculated properties.
$OutputFields = @(
@{name="Item" ; expression={$_.Path.split(':',3)[-1]}}
@{name="Rights" ; expression={$Right.FileSystemRights}}
@{name="AccessType" ; expression={$Right.AccessControlType}}
@{name="From" ; expression={$User}}
)
$FileSystemObjects = Get-ChildItem $Path -Recurse | ForEach-Object {Get-Acl $_.FullName}
foreach ($Item in $FileSystemObjects) {
foreach ($Right in $Item.Access) {
if ($Right.IdentityReference -eq $User) {
$List.Add(($Item | Select-Object $OutputFields))
}
}
}
foreach ($Item in $FileSystemObjects) {
foreach ($Right in $Item.Access) {
foreach ($GroupArrayItem in $GroupArrayList){
if ($Right.IdentityReference -eq ("TESTUMGEBUNG\" + $GroupArrayItem)) {
$List.Add(($Item | Select-Object $OutputFields))
}
}
}
}
$List | Out-File C:\Users\cbruehwiler\Desktop\PermissionCheck.txt
结果如下:
Item Rights AccessType From ---- ------ ---------- ---- T:\TestFolder FullControl Allow Testumgebung\cbruehwiler T:\TestFolder Read, Synchronize Allow Testumgebung\cbruehwiler T:\TestFolder Write, ReadAndExecute, Synchronize Allow Testumgebung\cbruehwiler
现在,最后一行仅打印我的用户。但是,它应该显示用户或组。
答案 0 :(得分:1)
您甚至可以将两个循环合并为一个,就像这样:
foreach ($Item in $FileSystemObjects) {
foreach ($Right in $Item.Access) {
foreach ($GroupArrayItem in $GroupArrayList) {
# not needed; just for convenience
[string]$id = $Right.IdentityReference
# test if the $Right.IdentityReference corresponds with the user name
if ($id -eq $User) {
$List.Add(($Item | Select-Object $OutputFields))
}
# test if the $Right.IdentityReference without the 'Domain\' part can be found in the list of groups
elseif (($id.Split("\", 2)[-1]) -in $GroupArrayList) {
# set the $User variable to the value of $Right.IdentityReference
$User = "Group: $id"
$List.Add(($Item | Select-Object $OutputFields))
}
}
}
}