如何在PowerShell NTFS权限分析脚本中集成Active Directory选择器替换手动输入组名
AD组可视化选择器嵌入脚本实现方案
实现逻辑说明
替换原有手动键入组名的交互环节,调用Windows系统原生的AD对象选择对话框组件,实现和域环境通用的AD对象选择一致的操作体验,支持域内组浏览、搜索功能,无需自行开发复杂的列表框、搜索框交互逻辑。
可直接嵌入的代码
1. 新增AD组选择对话框函数
移除原有脚本第一行的手动输入命令$gruppe = read-Host "group name",在脚本最开头加载所需系统程序集,并新增选择器函数:
# 加载依赖系统程序集 Add-Type -AssemblyName System.Windows.Forms Add-Type -AssemblyName System.DirectoryServices Add-Type -AssemblyName System.DirectoryServices.AccountManagement function Select-ADGroupFromDialog { # 实例化系统原生AD对象选择组件 $pickerType = [Type]::GetTypeFromProgID("DSObjectPicker.DsObjectPicker.1") $picker = [Activator]::CreateInstance($pickerType) # 配置选择规则:仅展示组类对象,隐藏用户、计算机类对象 $currentDomain = [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain() $picker.Initialize( 0x40000, # 过滤位:仅加载组对象 $currentDomain.DirectoryEntry.Path, $null, $null ) # 配置对话框基础布局 $dialog = New-Object System.Windows.Forms.Form $dialog.Width = 480 $dialog.Height = 520 $dialog.Text = "选择目标AD组" $dialog.StartPosition = "CenterScreen" $dialog.Controls.Add($picker) # 添加确认、取消按钮 $btnOk = New-Object System.Windows.Forms.Button $btnOk.Text = "确定" $btnOk.Location = New-Object System.Drawing.Point(340, 450) $btnOk.Width = 80 $btnOk.Add_Click({ $dialog.DialogResult = [System.Windows.Forms.DialogResult]::OK $dialog.Close() }) $btnCancel = New-Object System.Windows.Forms.Button $btnCancel.Text = "取消" $btnCancel.Location = New-Object System.Drawing.Point(340, 480) $btnCancel.Width = 80 $btnCancel.Add_Click({ $dialog.DialogResult = [System.Windows.Forms.DialogResult]::Cancel $dialog.Close() }) $dialog.Controls.AddRange(@($btnOk, $btnCancel)) # 处理选择结果 if ($dialog.ShowDialog() -eq [System.Windows.Forms.DialogResult]::OK) { $selectedItems = $picker.Invoke("InvokeGetSelectedItems") if ($selectedItems.Count -gt 0) { # 返回选中组的DistinguishedName,去掉前缀的LDAP标识 return $selectedItems[0].ADsPath -replace "^LDAP://", "" } } return $null }
2. 替换原手动输入逻辑
在原有文件夹选择函数的调用之前,新增选择器调用逻辑,替代原手动输组名的环节:
# 弹出AD组选择对话框 $selectedGroupDN = Select-ADGroupFromDialog # 用户点取消则直接退出脚本 if (-not $selectedGroupDN) { Write-Host "未选择有效AD组,脚本退出" exit }
3. 修正原有脚本的权限匹配缺陷
原脚本在收集匹配SID列表时,错误将组名文本直接加入匹配数组,会导致权限匹配失效,对应修正后的权限扫描函数:
function Get-FolderRightsForAccount([string]$dn, [string]$rootfolder, [switch]$includeInheritedRights){ $sids = @() # 收集目标组本身的SID $targetGroupSid = (Get-ADObject $dn -Properties objectSid).objectSid.Value $sids += $targetGroupSid # 递归收集目标组嵌套的所有子组SID(修正原逻辑中直接加入组名文本的错误) $nestedGroupSids = Get-ADGroupMember $dn -Recursive | Where-Object ObjectClass -eq "group" | Get-ADObject -Properties objectSid | Select-Object -ExpandProperty objectSid -ExpandProperty Value $sids += $nestedGroupSids # 后续ACL扫描逻辑保持和原脚本一致 $inherited = @{$true=($true,$false);$false=$false}[$includeInheritedRights.IsPresent] (Get-ACL $rootfolder).Access | Where-Object { try { $_.IdentityReference.Translate([System.Security.Principal.SecurityIdentifier]).Value -in $sids -and $_.IsInherited -in $inherited } catch {} } | Select-Object @{n='Folder';e={$rootfolder}}, AccessControlType, @{n='Rights';e={$_.FileSystemRights}} Get-ChildItem $rootfolder -Recurse -Directory -PipelineVariable f | ForEach-Object { (Get-ACL $_.Fullname).Access | Where-Object { try { $_.IdentityReference.Translate([System.Security.Principal.SecurityIdentifier]).Value -in $sids -and $_.IsInherited -in $inherited } catch {} } | Select-Object @{n='Folder';e={$f.Fullname}}, AccessControlType, @{n='Rights';e={$_.FileSystemRights}} } }
4. 调整最终函数调用参数
因为选择器已经直接返回了目标组的DistinguishedName,无需二次查询AD获取组对象,替换原脚本最后一行的调用代码:
Get-FolderRightsForAccount -dn $selectedGroupDN -rootfolder $o -includeInheritedRights | Format-Table -AutoSize
运行注意事项
- 运行脚本的主机需安装RSAT域管理组件,且当前登录账号具备当前域的基础读取权限,才能正常加载域内AD组列表。
- 选择器支持跨域选择对象,可直接在弹窗中切换域节点浏览其他信任域的组对象。
- 可根据实际需求调整选择器的过滤规则,比如限定仅展示特定作用域的组对象,只需修改选择器初始化方法中的过滤位、路径参数即可。
内容的提问来源于stack exchange,提问作者chkdsk
相关产品推荐
相关产品推荐

