【问题标题】:Exclude specific users when listing Windows folder permissions in PowerShell在 PowerShell 中列出 Windows 文件夹权限时排除特定用户
【发布时间】:2021-09-16 04:34:32
【问题描述】:

我正在尝试列出有权访问特定目录的所有用户以及该目录中的子文件夹。

我发现了这个website,它向我展示了如何很好地做到这一点。但是我想稍微修改一下这个脚本,这样我就可以从输出中排除某些内置的 Windows 用户。

所以我在StackOverflow 上找到了另一个链接,该链接显示了如何从结果中排除用户列表。但是当我将-notmatch 添加到现有的 PS 脚本中时,组/用户由于某种原因从实际用户名更改为 True 或 False。

如何让此脚本过滤掉 $ignore 变量中的用户并让组/用户显示用户名?

$ignore = @('BUILTIN\Administrators','CREATOR OWNER')
$ExcludeUsersRegex = ($ignore | % { [regex]::Escape($_) }) -join '|'

$FolderPath = Get-ChildItem -Directory -Path "D:\MSSQL" -Recurse -Force
$Output = @()
ForEach ($Folder in $FolderPath) {
    $Acl = Get-Acl -Path $Folder.FullName
    ForEach ($Access in $Acl.Access) {
$Properties = [ordered]@{'Folder Name'=$Folder.FullName;'Group/User'=$Access.IdentityReference -notmatch $ExcludeUsersRegex;'Permissions'=$Access.FileSystemRights;'Inherited'=$Access.IsInherited}
#$Properties = [ordered]@{'Folder Name'=$Folder.FullName;'Group/User'=$Access.IdentityReference;'Permissions'=$Access.FileSystemRights;'Inherited'=$Access.IsInherited}
$Output += New-Object -TypeName PSObject -Property $Properties            
}
}
$Output | Out-GridView

【问题讨论】:

  • ForEach($Access in ($Acl.Access|Where{$_.IdentityReference -notmatch $ExcludeUsersRegex})) { 过滤掉匹配特定用户名的访问。
  • 谢谢 - 如果您作为答案发布,我很乐意接受并标记为已回答。

标签: powershell


【解决方案1】:

您可以在循环级别进行过滤,因此不受欢迎的用户不会通过循环进行迭代。

ForEach($Access in ($Acl.Access|Where{$_.IdentityReference -notmatch $ExcludeUsersRegex})) {

过滤掉与特定用户名匹配的访问。

【讨论】:

    猜你喜欢
    • 2019-09-07
    • 2012-11-10
    • 2016-06-04
    • 1970-01-01
    • 1970-01-01
    • 2020-06-16
    • 1970-01-01
    • 2012-08-29
    • 2022-12-06
    相关资源
    最近更新 更多