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

如何用PowerShell增大弹窗窗口及文本的尺寸?

问题描述

我正在开发弹窗功能,用于在服务台联系终端用户前,引导特定用户完成任务。目前使用以下脚本:

$name = read-host "Enter computer name "
$msg = read-host "Enter your message "
Invoke-WmiMethod -Path Win32_Process -Name Create -ArgumentList "msg * $msg" -ComputerName $name

该脚本简洁基础,但管理层要求增大弹窗窗口及文本的尺寸,担心用户会快速关闭弹窗。我已查阅多个论坛寻找解决方案,希望有不复杂的方法。看到一些使用$objForm的文章,但这些自定义弹窗的实现对我来说有些复杂。

解决方案

方案1:使用PowerShell内置MessageBox(简单易上手)

不需要复杂的WinForms配置,直接利用.NET的MessageBox类远程执行,窗口和字体默认比msg命令的弹窗更大,还能自定义标题和按钮样式:

$computerName = Read-Host "Enter computer name"
$message = Read-Host "Enter your message"
$title = "服务台通知" # 可自定义标题

# 构建远程执行的脚本块
$scriptBlock = {
    param($msg, $title)
    Add-Type -AssemblyName System.Windows.Forms
    [System.Windows.Forms.MessageBox]::Show($msg, $title, [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Information)
}

# 远程执行脚本块
Invoke-Command -ComputerName $computerName -ScriptBlock $scriptBlock -ArgumentList $message, $title

说明:

  • 弹窗尺寸会根据内容自动调整,文本显示更清晰,比msg弹窗更醒目
  • 需确保执行脚本的账号有目标机器的远程权限,且目标机器已启用PowerShell远程(未启用可先执行Enable-PSRemoting -Force开启)

方案2:简化版自定义WinForms弹窗(可控性更强)

如果需要进一步调整窗口大小、字体大小,这里提供一个简化封装的版本,去掉复杂配置项,只保留核心可自定义参数:

$computerName = Read-Host "Enter computer name"
$message = Read-Host "Enter your message"
$windowWidth = 600 # 自定义窗口宽度
$windowHeight = 300 # 自定义窗口高度
$fontSize = 14 # 自定义字体大小

$scriptBlock = {
    param($msg, $width, $height, $fontSize)
    Add-Type -AssemblyName System.Windows.Forms
    Add-Type -AssemblyName System.Drawing

    # 创建主窗口
    $form = New-Object System.Windows.Forms.Form
    $form.Text = "服务台重要通知"
    $form.Size = New-Object System.Drawing.Size($width, $height)
    $form.StartPosition = "CenterScreen" # 窗口居中显示
    $form.TopMost = $true # 置顶显示,避免被遮挡

    # 创建文本标签
    $label = New-Object System.Windows.Forms.Label
    $label.Text = $msg
    $label.Font = New-Object System.Drawing.Font("Arial", $fontSize)
    $label.AutoSize = $false
    $label.Size = $form.ClientSize
    $label.TextAlign = [System.Drawing.ContentAlignment]::MiddleCenter
    $label.Padding = New-Object System.Windows.Forms.Padding(20)

    # 创建确认按钮
    $button = New-Object System.Windows.Forms.Button
    $button.Text = "我已阅读"
    $button.Size = New-Object System.Drawing.Size(120, 40)
    $button.Location = New-Object System.Drawing.Point(($form.ClientSize.Width - 120)/2, $form.ClientSize.Height - 60)
    $button.Add_Click({$form.Close()})

    # 添加控件到窗口
    $form.Controls.Add($label)
    $form.Controls.Add($button)

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

Invoke-Command -ComputerName $computerName -ScriptBlock $scriptBlock -ArgumentList $message, $windowWidth, $windowHeight, $fontSize

说明:

  • 直接修改$windowWidth、$windowHeight、$fontSize参数即可调整弹窗大小和文本尺寸
  • 窗口默认置顶,确保用户能立刻看到

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.24 11:52:21