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

在PowerShell中能否自定义MessageBox.Icon图标?

自定义MessageBox图标需求及解决方案

需求说明

目前已实现自动更新公司应用后提示重启的脚本,但不想使用Windows.System.Form提供的默认图标,希望将本地保存的.png文件存入变量(例如$ICON = -File C:PATH\Image.png),并通过$MessageIcon = [MessageBoxImage]::$ICON调用,以此显示公司专属图标,让用户知晓提示来自内部IT部门。

现有功能代码

以下是当前可正常运行的PowerShell脚本:

choco upgrade all -y --log-File "C:\ProgramData\chocolatey\logs\chocolatey.log"
if ($LASTEXITCODE -eq 3010) {
    New-Item -Path C:\ProgramData\chocolatey\logs\rebootnow.txt -Force
}
else {
    return
}
if (Test-Path -Path C:\ProgramData\chocolatey\logs\rebootnow.txt) {
    try {
        Add-Type -AssemblyName PresentationCore, PresentationFramework
    }
    catch {
        Write-Host "Exit 2 - Beim Anlegen der Libaries für Windows Forms ist ein Fehler aufgetreten."
        exit 2
    }
    $ButtonType = [System.Windows.MessageBoxButton]::YesNo
    $MessageboxTitle = "Information"
    $Messageboxbody = "Es wurde im Hintergrund vom IT Administrator ein Update für alle Firmeninternen Apps durchgeführt. Es ist ein Geräte-Neustart nötig um das Update zu finalisieren. Möchten Sie jetzt einen Geräte-Neustart durchführen?"
    $MessageIcon = [System.Windows.MessageBoxImage]::Information

    try {
        $result = [System.Windows.MessageBox]::Show($Messageboxbody, $MessageboxTitle, $ButtonType, $MessageIcon)
    }
    catch {
        Write-Host "Exit 3 - Beim erstellen der Windows Forms aus den verschiedenen Objekten ist ein Fehler aufgetreten."
        exit 3
    }

    if ($result -eq [System.Windows.MessageBoxResult]::Yes) {
        Remove-Item -Path "C:\ProgramData\chocolatey\logs\rebootnow.txt"
        Shutdown.exe /r /t 15
    }
    else {
        return
    }
}
else {
    return
}

最终解决方案

经尝试,仅通过原生PowerShell无法实现自定义MessageBox图标的需求。使用VSCode的PowerShell Pro Tool创建Windows Forms并关联脚本后,已成功实现显示自定义公司图标的功能。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.17 07:18:21