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

如何在PowerShell GUI的标签页间添加密码权限控制

PowerShell GUI标签页权限控制实现方案

要实现基于密码的标签页访问控制(Team1仅访问Tab1,Team2访问Tab1+Tab2),可以通过登录验证+动态调整标签页可见性的方式实现,以下是完整的实现代码和关键说明:

完整实现代码

# 加载Windows Forms程序集
Add-Type -AssemblyName System.Windows.Forms

# 创建主窗体
$MainForm = New-Object System.Windows.Forms.Form
$MainForm.Text = "权限控制GUI"
$MainForm.Size = New-Object System.Drawing.Size(600,400)
$MainForm.StartPosition = "CenterScreen"

# 创建TabControl容器
$FormTabControl = New-Object System.Windows.Forms.TabControl
$FormTabControl.Dock = "Fill"
$MainForm.Controls.Add($FormTabControl)

#------------[TAB1]------------
$Tab1 = New-object System.Windows.Forms.Tabpage
$Tab1.DataBindings.DefaultDataSourceUpdateMode = 0 
$Tab1.UseVisualStyleBackColor = $True 
$Tab1.Name = "Team1" 
$Tab1.Text = "TAB1"
$FormTabControl.Controls.Add($Tab1)

# 示例:给Tab1添加操作按钮
$Tab1Btn = New-Object System.Windows.Forms.Button
$Tab1Btn.Text = "Tab1专属操作"
$Tab1Btn.Location = New-Object System.Drawing.Point(20,20)
$Tab1.Controls.Add($Tab1Btn)

#------------[TAB2]------------
$Tab2 = New-object System.Windows.Forms.Tabpage
$Tab2.DataBindings.DefaultDataSourceUpdateMode = 0 
$Tab2.UseVisualStyleBackColor = $True 
$Tab2.Name = "Team2" 
$Tab2.Text = "TAB2"
$FormTabControl.Controls.Add($Tab2)

# 示例:给Tab2添加操作按钮
$Tab2Btn = New-Object System.Windows.Forms.Button
$Tab2Btn.Text = "Tab2专属操作"
$Tab2Btn.Location = New-Object System.Drawing.Point(20,20)
$Tab2.Controls.Add($Tab2Btn)

# 创建权限验证登录窗口
$LoginForm = New-Object System.Windows.Forms.Form
$LoginForm.Text = "权限验证"
$LoginForm.Size = New-Object System.Drawing.Size(300,150)
$LoginForm.StartPosition = "CenterScreen"
$LoginForm.FormBorderStyle = "FixedDialog"
$LoginForm.MaximizeBox = $false
$LoginForm.MinimizeBox = $false

# 密码输入控件
$PasswordLabel = New-Object System.Windows.Forms.Label
$PasswordLabel.Text = "请输入密码:"
$PasswordLabel.Location = New-Object System.Drawing.Point(20,20)
$PasswordLabel.Size = New-Object System.Drawing.Size(80,20)
$LoginForm.Controls.Add($PasswordLabel)

$PasswordTextBox = New-Object System.Windows.Forms.TextBox
$PasswordTextBox.Location = New-Object System.Drawing.Point(100,20)
$PasswordTextBox.Size = New-Object System.Drawing.Size(160,20)
$PasswordTextBox.PasswordChar = "*" # 隐藏输入的密码字符
$LoginForm.Controls.Add($PasswordTextBox)

# 确认按钮及验证逻辑
$ConfirmButton = New-Object System.Windows.Forms.Button
$ConfirmButton.Text = "确认"
$ConfirmButton.Location = New-Object System.Drawing.Point(100,60)
$ConfirmButton.Size = New-Object System.Drawing.Size(80,30)
$ConfirmButton.Add_Click({
    $inputPassword = $PasswordTextBox.Text
    # 预设权限密码,可根据实际需求修改
    $team1Password = "Team1@2024"
    $team2Password = "Team2@2024"

    if ($inputPassword -eq $team1Password) {
        # Team1权限:移除Tab2(彻底禁用访问)
        $FormTabControl.TabPages.Remove($Tab2)
        $LoginForm.Close()
    } elseif ($inputPassword -eq $team2Password) {
        # Team2权限:保留所有标签页
        $LoginForm.Close()
    } else {
        # 密码错误提示
        [System.Windows.Forms.MessageBox]::Show("密码错误,请重新输入!", "验证失败", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Error)
        $PasswordTextBox.Clear()
        $PasswordTextBox.Focus()
    }
})
$LoginForm.Controls.Add($ConfirmButton)

# 先弹出登录窗口,验证通过后再显示主窗体
$LoginForm.ShowDialog() | Out-Null

# 启动主窗体
$MainForm.ShowDialog() | Out-Null

关键说明

  • 权限验证时机:登录窗口会在主窗体显示前强制弹出,确保只有验证通过的用户才能进入主界面
  • 权限控制方式:
    • 对Team1用户,直接从TabControl中移除Tab2,彻底禁止访问(也可改用$Tab2.Visible = $false隐藏,但移除的方式更安全)
    • 对Team2用户,保留所有标签页的访问权限
  • 密码安全性:示例中使用硬编码密码,实际使用时建议从安全存储(如Windows凭据管理器)读取,避免明文泄露

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.01 15:51:02