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

如何将PowerShell脚本转换为带复选框的GUI工具

PowerShell带复选框GUI执行指定函数方案

实现思路

  • 将需执行的函数整理为包含显示名称和函数名的结构化集合,方便动态生成GUI复选框
  • 按原脚本顺序生成对应复选框,保留默认选中状态(比如原注释的函数默认不选中)
  • 点击「运行脚本」按钮时,按顺序遍历复选框,执行选中项对应的函数
  • 函数执行输出直接流向PowerShell控制台,满足真实控制台运行需求

完整示例代码

# --------------------------
# 先定义你的所有业务函数(示例占位,需替换为实际实现)
# --------------------------
function Clear-Host {
    Write-Host "执行Clear-Host" -ForegroundColor Cyan
    # 实际逻辑
}

function StartTranscript {
    $logPath = Join-Path $env:TEMP "AutoScript_$(Get-Date -Format 'yyyyMMddHHmmss').log"
    Write-Host "开始记录日志到: $logPath" -ForegroundColor Cyan
    # 实际逻辑:Start-Transcript -Path $logPath
}

function connectTFS {
    Write-Host "连接TFS服务器" -ForegroundColor Cyan
    # 实际逻辑
}

function ChooseVersion {
    Write-Host "选择目标版本" -ForegroundColor Cyan
    # 实际逻辑
}

function Createtempfolders {
    Write-Host "创建临时文件夹" -ForegroundColor Cyan
    # 实际逻辑
}

function Gathercontent {
    Write-Host "收集新内容" -ForegroundColor Cyan
    # 实际逻辑
}

function Copyfilesforautomation {
    Write-Host "复制自动化所需最新文件" -ForegroundColor Cyan
    # 实际逻辑
}

function RunCI {
    Write-Host "执行CI自动化" -ForegroundColor Cyan
    # 实际逻辑
}

function CreateTFS {
    Write-Host "创建TFS项" -ForegroundColor Cyan
    # 实际逻辑
}

function StartingRelease {
    Write-Host "创建新版本" -ForegroundColor Cyan
    # 实际逻辑
}

function CloseTfs {
    Write-Host "关闭TFS连接" -ForegroundColor Cyan
    # 实际逻辑
}

function FinalizeSheet {
    Write-Host "完成表格处理" -ForegroundColor Cyan
    # 实际逻辑
}

function Createemail {
    Write-Host "生成邮件" -ForegroundColor Cyan
    # 实际逻辑
}

function UndoExcelChanges {
    Write-Host "撤销Excel修改" -ForegroundColor Cyan
    # 实际逻辑
}

function RunWeekendAutomation {
    Write-Host "执行周末自动化构建复制" -ForegroundColor Cyan
    # 实际逻辑
}

# --------------------------
# GUI生成函数
# --------------------------
function GenerateForm {
    Add-Type -AssemblyName System.Windows.Forms
    Add-Type -AssemblyName System.Drawing

    # 定义函数列表:显示名称、函数名、默认是否选中
    $functionList = @(
        @{ DisplayName = "清空控制台"; FunctionName = "Clear-Host"; Checked = $true },
        @{ DisplayName = "启动日志记录"; FunctionName = "StartTranscript"; Checked = $true },
        @{ DisplayName = "连接TFS"; FunctionName = "connectTFS"; Checked = $true },
        @{ DisplayName = "选择版本"; FunctionName = "ChooseVersion"; Checked = $true },
        @{ DisplayName = "创建临时文件夹"; FunctionName = "Createtempfolders"; Checked = $true },
        @{ DisplayName = "收集新内容"; FunctionName = "Gathercontent"; Checked = $true },
        @{ DisplayName = "复制自动化文件"; FunctionName = "Copyfilesforautomation"; Checked = $false }, # 原脚本注释,默认不选中
        @{ DisplayName = "执行CI自动化"; FunctionName = "RunCI"; Checked = $true },
        @{ DisplayName = "创建TFS项"; FunctionName = "CreateTFS"; Checked = $true },
        @{ DisplayName = "启动版本发布"; FunctionName = "StartingRelease"; Checked = $true },
        @{ DisplayName = "关闭TFS连接"; FunctionName = "CloseTfs"; Checked = $true },
        @{ DisplayName = "完成表格处理"; FunctionName = "FinalizeSheet"; Checked = $true },
        @{ DisplayName = "生成邮件"; FunctionName = "Createemail"; Checked = $true },
        @{ DisplayName = "撤销Excel修改"; FunctionName = "UndoExcelChanges"; Checked = $false },
        @{ DisplayName = "执行周末自动化"; FunctionName = "RunWeekendAutomation"; Checked = $false }
    )

    # 创建主窗体
    $form = New-Object System.Windows.Forms.Form
    $form.Text = "自动化脚本执行器"
    $form.Size = New-Object System.Drawing.Size(350, 550)
    $form.StartPosition = "CenterScreen"

    # 创建复选框容器(滚动面板)
    $scrollPanel = New-Object System.Windows.Forms.Panel
    $scrollPanel.Size = New-Object System.Drawing.Size(300, 450)
    $scrollPanel.Location = New-Object System.Drawing.Point(20, 20)
    $scrollPanel.AutoScroll = $true

    # 动态生成复选框
    $yPos = 10
    foreach ($func in $functionList) {
        $checkBox = New-Object System.Windows.Forms.CheckBox
        $checkBox.Text = $func.DisplayName
        $checkBox.Tag = $func.FunctionName # 存储对应的函数名
        $checkBox.Checked = $func.Checked
        $checkBox.Location = New-Object System.Drawing.Point(10, $yPos)
        $checkBox.Size = New-Object System.Drawing.Size(280, 20)
        $scrollPanel.Controls.Add($checkBox)
        $yPos += 25
    }

    # 创建运行按钮
    $runButton = New-Object System.Windows.Forms.Button
    $runButton.Text = "运行脚本"
    $runButton.Size = New-Object System.Drawing.Size(100, 30)
    $runButton.Location = New-Object System.Drawing.Point(120, 480)
    $runButton.Add_Click({
        $form.Enabled = $false # 运行时禁用窗体,防止重复点击
        Write-Host "`n===== 开始执行选中的函数 =====`n" -ForegroundColor Green
        
        # 按顺序执行选中的函数
        foreach ($cb in $scrollPanel.Controls | Where-Object { $_ -is [System.Windows.Forms.CheckBox] }) {
            if ($cb.Checked) {
                $funcName = $cb.Tag
                Write-Host "`n--- 执行: $($cb.Text) ---" -ForegroundColor Yellow
                & $funcName # 调用对应的函数
            }
        }

        Write-Host "`n===== 执行完成 =====`n" -ForegroundColor Green
        $form.Enabled = $true
    })

    # 添加控件到窗体
    $form.Controls.Add($scrollPanel)
    $form.Controls.Add($runButton)

    # 显示窗体
    $form.ShowDialog() | Out-Null
}

# 启动GUI
GenerateForm

使用说明

  1. 替换业务函数:将示例中的占位函数替换为你实际的函数实现
  2. 调整函数列表:在$functionList中修改显示名称、默认选中状态,新增/删除函数条目即可
  3. 运行脚本:直接在PowerShell控制台执行该脚本,弹出GUI后选择需要执行的函数,点击「运行脚本」即可在控制台看到执行过程和输出

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.22 17:45:49