You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

创建带GUI的PowerShell脚本:实现名称检索与文件夹授权

带GUI的PowerShell文件夹权限配置脚本实现

我帮你梳理并完善了这款带GUI的PowerShell脚本,完全贴合你需求的功能逻辑,下面是具体实现:

核心功能说明

  • 提供简洁的Windows GUI界面,方便用户输入目标名称和文件夹路径
  • 支持从指定数据源(比如AD用户/组、数据库列表)检索完全匹配的条目
  • 匹配成功后自动将名称复制到剪贴板,并为目标文件夹配置对应权限

完整实现代码

function Select-SharePermission {
    # 加载Windows Forms程序集(推荐用Add-Type替代过时的LoadWithPartialName)
    Add-Type -AssemblyName System.Windows.Forms
    Add-Type -AssemblyName System.Drawing

    # 创建主窗口
    $mainForm = New-Object System.Windows.Forms.Form
    $mainForm.Text = "文件夹权限配置工具"
    $mainForm.Size = New-Object System.Drawing.Size(400,250)
    $mainForm.StartPosition = "CenterScreen"

    # 目标名称输入区
    $nameLabel = New-Object System.Windows.Forms.Label
    $nameLabel.Location = New-Object System.Drawing.Point(20,20)
    $nameLabel.Size = New-Object System.Drawing.Size(100,20)
    $nameLabel.Text = "输入目标名称:"
    $mainForm.Controls.Add($nameLabel)

    $nameInput = New-Object System.Windows.Forms.TextBox
    $nameInput.Location = New-Object System.Drawing.Point(120,20)
    $nameInput.Size = New-Object System.Drawing.Size(240,20)
    $mainForm.Controls.Add($nameInput)

    # 文件夹路径输入区
    $pathLabel = New-Object System.Windows.Forms.Label
    $pathLabel.Location = New-Object System.Drawing.Point(20,60)
    $pathLabel.Size = New-Object System.Drawing.Size(100,20)
    $pathLabel.Text = "文件夹路径:"
    $mainForm.Controls.Add($pathLabel)

    $pathInput = New-Object System.Windows.Forms.TextBox
    $pathInput.Location = New-Object System.Drawing.Point(120,60)
    $pathInput.Size = New-Object System.Drawing.Size(240,20)
    $mainForm.Controls.Add($pathInput)

    # 检索并配置按钮
    $searchBtn = New-Object System.Windows.Forms.Button
    $searchBtn.Location = New-Object System.Drawing.Point(20,100)
    $searchBtn.Size = New-Object System.Drawing.Size(100,30)
    $searchBtn.Text = "检索并配置"
    $searchBtn.Add_Click({
        $targetName = $nameInput.Text.Trim()
        $folderPath = $pathInput.Text.Trim()

        # 校验输入完整性
        if (-not $targetName -or -not $folderPath) {
            [System.Windows.Forms.MessageBox]::Show("请填写完整信息!","提示",0,48)
            return
        }

        # 替换为你的数据源检索逻辑:示例用AD用户查询
        try {
            # 完全匹配查询AD用户(如果是组就换成Get-ADGroup)
            $matchedItem = Get-ADUser -Filter {Name -eq $targetName} -ErrorAction Stop

            if ($matchedItem) {
                # 复制匹配名称到剪贴板
                [System.Windows.Forms.Clipboard]::SetText($matchedItem.Name)
                [System.Windows.Forms.MessageBox]::Show("已找到匹配项,名称已复制到剪贴板!","成功",0,64)

                # 配置文件夹权限(这里用了完全控制,可根据需求调整)
                $acl = Get-Acl -Path $folderPath
                $permissionRule = $matchedItem.SamAccountName,"FullControl","ContainerInherit,ObjectInherit","None","Allow"
                $accessRule = New-Object System.Security.AccessControl.FileSystemAccessRule $permissionRule
                $acl.AddAccessRule($accessRule)
                Set-Acl -Path $folderPath -AclObject $acl

                [System.Windows.Forms.MessageBox]::Show("文件夹权限配置完成!","成功",0,64)
            } else {
                [System.Windows.Forms.MessageBox]::Show("未找到完全匹配的名称!","提示",0,48)
            }
        } catch {
            [System.Windows.Forms.MessageBox]::Show("检索或配置出错: $_","错误",0,16)
        }
    })
    $mainForm.Controls.Add($searchBtn)

    # 显示窗口
    $mainForm.ShowDialog() | Out-Null
}

# 启动工具
Select-SharePermission

自定义调整提示

  • 数据源替换:如果要从数据库检索,把AD查询部分换成数据库连接逻辑,比如用Invoke-SqlCmd执行查询语句,筛选完全匹配的条目
  • 权限调整:修改FullControl为你需要的权限,比如ReadAndExecute、Modify等
  • 路径校验:可以添加Test-Path $folderPath判断路径是否存在,提升用户体验
  • 过时API替换:你原来代码里的[System.Reflection.Assembly]::LoadWithPartialName已经被标记为过时,我换成了更推荐的Add-Type -AssemblyName

内容的提问来源于stack exchange,提问作者itisshan Shan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 10:27:20