当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 如何从 PowerShell 显示弹出窗口或气球提示通知?

作者:精品下载站 日期:2024-12-14 20:02:18 浏览:14 分类:玩电脑

如何从 PowerShell 显示弹出窗口或气球提示通知?


尽管 PowerShell 是一种控制台语言,但有时需要通过 PS 脚本向用户通知特定事件或提示他们执行某些操作。例如,您可以显示有关繁重 PoSh 脚本完成或重要事件发生时的弹出通知或气球提示。

最简单的方法是使用 PowerShell 中的 Windows 脚本子系统 (Wscript) 调用来显示包含任何文本的窗口。

此 PowerShell 代码将显示一个公共窗口,其中包含您的消息和“确定”按钮。

$wshell = New-Object -ComObject Wscript.Shell
$Output = $wshell.Popup("The report generation script is successfully completed!")

[玩转系统] 如何从 PowerShell 显示弹出窗口或气球提示通知?

使用 Popup 方法的不同属性,您可以自定义包含消息文本的模式窗口的外观。您还可以向脚本返回用户的答案(是/否)。

[玩转系统] 如何从 PowerShell 显示弹出窗口或气球提示通知?

$Output = $wshell.Popup("The report generation script is successfully completed! Do you want to display a report on the screen?",0,"The report is ready",4+32)

Popup方法的一般语法和可用参数:

Popup(<Text>,<SecondsToWait>,<Title>,<Type>)

参数:

  • — 消息文本(字符串);

  • — 一个数字(可选)。消息窗口自动关闭之前的秒数;

  • —字符串(可选)。弹出窗口的标题文本(caption);

  • —数字(可选)。确定按钮和图标类型的标志组合。


可能的类型标志值:

    • 0——确定按钮;

  • 1 — 确定和取消按钮;

  • 2 — 停止、重试和跳过按钮;

  • 3 — 是、否和取消按钮;

  • 4 — 是和否按钮;

  • 5 — 重试和取消按钮;

  • 16 — 停止图标;

  • 32 — 问题图标;

  • 48 — 感叹号图标;

  • 64 — 信息图标。

Popup 方法返回一个整数,通过该整数可以知道用户单击了哪个按钮。可能的值:

  • -1——超时;

  • 1——确定按钮;

  • 2 — 取消按钮;

  • 3——停止按钮;

  • 4 — 重试按钮;

  • 5——跳过按钮;

  • 6 — 是按钮;

  • 7 — 无按钮。

更有吸引力的弹出消息(气球提示)可以通过Windows Forms API在Windows 7、8.1和10中显示。以下 PowerShell 代码将在 Windows 10 通知栏旁边显示一条弹出消息,该消息将在 20 秒后自动消失。

Add-Type -AssemblyName System.Windows.Forms
$global:balmsg = New-Object System.Windows.Forms.NotifyIcon
$path = (Get-Process -id $pid).Path
$balmsg.Icon = [System.Drawing.Icon]::ExtractAssociatedIcon($path)
$balmsg.BalloonTipIcon = [System.Windows.Forms.ToolTipIcon]::Warning
$balmsg.BalloonTipText = ‘This is the pop-up message text for the Windows 10 user'
$balmsg.BalloonTipTitle = "Attention $Env:USERNAME"
$balmsg.Visible = $true
$balmsg.ShowBalloonTip(20000)

[玩转系统] 如何从 PowerShell 显示弹出窗口或气球提示通知?

要在 Windows 10 (PowerShell 5.0+) 中创建彩色弹出消息,您还可以使用 PowerShell 库中的单独 PowerShell 模块 BurntToast

使用以下命令从在线存储库安装该模块:

Install-Module -Name BurntToast

例如,现在您可以轻松地向脚本中添加“如何在连接以太网电缆时自动禁用 Wi-Fi”一文中的彩色通知:

New-BurntToastNotification -Text "Disconnecting from Wi-Fi network", "You have been disconnected from your Wi-Fi network since your device was connected to a high-speed Ethernet LAN" -AppLogo C:\PS\changenetwork.png

因此,您知道如何使用 PowerShell 向用户显示通知。如果用户有扬声器,您甚至可以播放最喜欢的旋律:

[console]::beep(440,500)
[console]::beep(440,500)
[console]::beep(440,500)
[console]::beep(349,350)
[console]::beep(523,150)
[console]::beep(440,500)
[console]::beep(349,350)
[console]::beep(523,150)
[console]::beep(440,1000)
[console]::beep(659,500)
[console]::beep(659,500)
[console]::beep(659,500)
[console]::beep(698,350)
[console]::beep(523,150)
[console]::beep(415,500)
[console]::beep(349,350)
[console]::beep(523,150)
[console]::beep(440,1000)
[console]::beep(880,500)
[console]::beep(440,350)
[console]::beep(440,150)
[console]::beep(880,500)
[console]::beep(830,250)
[console]::beep(784,250)
[console]::beep(740,125)
[console]::beep(698,125)
[console]::beep(740,250)
[console]::beep(455,250)
[console]::beep(622,500)
[console]::beep(587,250)
[console]::beep(554,250)
[console]::beep(523,125)
[console]::beep(466,125)
[console]::beep(523,250)
[console]::beep(349,125)
[console]::beep(415,500)
[console]::beep(349,375)
[console]::beep(440,125)
[console]::beep(523,500)
[console]::beep(440,375)
[console]::beep(523,125)
[console]::beep(659,1000)
[console]::beep(880,500)
[console]::beep(440,350)
[console]::beep(440,150)
[console]::beep(880,500)
[console]::beep(830,250)
[console]::beep(784,250)
[console]::beep(740,125)
[console]::beep(698,125)
[console]::beep(740,250)
[console]::beep(455,250)
[console]::beep(622,500)
[console]::beep(587,250)
[console]::beep(554,250)
[console]::beep(523,125)
[console]::beep(466,125)
[console]::beep(523,250)
[console]::beep(349,250)
[console]::beep(415,500)
[console]::beep(349,375)
[console]::beep(523,125)
[console]::beep(440,500)
[console]::beep(349,375)
[console]::beep(261,125)
[console]::beep(440,1000)

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯