Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> ระบบ >> Windows Server

วิธีแสดงการแจ้งเตือนแบบ Pop-Up หรือ Balloon Tip จาก PowerShell

แม้ PowerShell จะเป็นภาษาคอนโซล แต่บางครั้งจำเป็นต้องแจ้งเตือนผู้ใช้จากสคริปท PS เกี่ยวกับเหตุการณ์เฉพาะหรือแจ้งให้พวกเขาทำบางอย่าง ตัวอย่างเช่น คุณสามารถแสดงการแจ้งเตือนแบบ pop-up หรือ balloon tip เกี่ยวกับการเสร็จสิ้นของสคริปท PoSh ที่หนักหรือเมื่อเกิดเหตุการณ์สำคัญ

วิธีที่ง่ายที่สุดคือการแสดงหน้าต่างที่มีข้อความใดๆ โดยใช้การเรียก Windows script subsystem (Wscript) จาก PowerShell

โค้ด PowerShell นี้จะแสดงหน้าต่างทั่วไปพร้อมข้อความของคุณและปุ่ม OK

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

วิธีแสดงการแจ้งเตือนแบบ Pop-Up หรือ Balloon Tip จาก PowerShell

โดยใช้คุณสมบัติที่แตกต่างกันของเมธอด Popup คุณสามารถปรับแต่งลักษณะที่ปรากฏของหน้าต่างโมดัลที่มีข้อความของคุณ คุณยังสามารถส่งคำตอบของผู้ใช้ (Yes/No) กลับไปยังสคริปทได้

วิธีแสดงการแจ้งเตือนแบบ Pop-Up หรือ Balloon Tip จาก 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>)

พารามิเตอร์:

  • <Text> — ข้อความ訊息 (สตริง);
  • <SecondsToWait> — ตัวเลข (ตัวเลือก) จำนวนวินาทีหลังจากนั้นหน้าต่างข้อความจะปิดอัตโนมัติ;
  • <Title> — สตริง (ตัวเลือก) ข้อความหัวข้อ (คำอธิบาย) ของหน้าต่าง pop-up;
  • <Type> — ตัวเลข (ตัวเลือก) การรวมของแฟล็กที่กำหนดประเภทปุ่มและไอคอน

ค่าที่เป็นไปได้ของแฟล็ก Type:

    • 0 — ปุ่ม OK;
    • 1 — ปุ่ม OK และ Cancel;
    • 2 — ปุ่ม Stop, Retry และ Skip;
    • 3 — ปุ่ม Yes, No และ Cancel;
    • 4 — ปุ่ม Yes และ No;
    • 5 — ปุ่ม Retry และ Cancel;
    • 16 — ไอคอน Stop;
    • 32 — ไอคอน Question;
    • 48 — ไอคอน Exclamation;
    • 64 — ไอคอน Information.

เมธอด Popup คืนค่าจำนวนเต็มที่ช่วยให้ทราบว่าผู้ใช้คลิกปุ่มใด ค่าที่เป็นไปได้:

  • -1 — timeout;
  • 1 — ปุ่ม OK;
  • 2 — ปุ่ม Cancel;
  • 3 — ปุ่ม Stop;
  • 4 — ปุ่ม Retry;
  • 5 — ปุ่ม Skip;
  • 6 — ปุ่ม Yes;
  • 7 — ปุ่ม No.

ข้อความ pop-up ที่น่าสนใจมากกว่า (balloon tips) อาจแสดงใน Windows 7, 8.1 & 10 ผ่าน Windows Forms API โค้ด PowerShell ต่อไปนี้จะแสดงข้อความ pop-up ใกล้แถบการแจ้งเตือน 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)

วิธีแสดงการแจ้งเตือนแบบ Pop-Up หรือ Balloon Tip จาก PowerShell

เพื่อสร้างข้อความ pop-up สีสันใน Windows 10 (PowerShell 5.0+) คุณยังสามารถใช้โมดูล PowerShell แยกต่างหาก BurntToast จาก PowerShell gallery

โมดูลติดตั้งจากที่เก็บออนไลน์โดยใช้ Windows 10 Package Manager:
Install-Module -Name BurntToast

ตัวอย่างเช่น ตอนนี้คุณสามารถเพิ่มการแจ้งเตือนสีสันลงในสคริปทจากบทความ "How to automatically disable Wi-Fi when Ethernet cable is connected" ได้อย่างง่ายดาย:

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)