Udostępnij za pośrednictwem


PS without BS: Showing a popup box and killing it

This is a simple solution for those wanting to show a popup box, but not leave it up indefinitely. This may be useful if you are in a process and want to show an error, but don't actually leave it up overnight, maybe kill it at some point so processing can continue.

Here is the syntax (my script name is call_popup.ps1):
powershell.exe -file call_popup.ps1 and then:
-Message (required): The message you want to display to the user.
-MessageHeader (optional): The message on the title box. Default is blank.
-MessageLevel (optional): The icon you want to display to the user. Default is informational.
Other values: Error, Warning
-Days (optional): Number of days you want to show the dialog box. Default is 0 days. This can be combined with hours and minutes.
-Hours (optional): Number of hours you want to show the dialog box. Default is 0 hours. This can be combined with days and minutes.
-Minutes (optional): Number of minutes you want to show the dialog box. Default is 10 minutes. This can be combined with days and hours.

Example syntax:
-Message "Hello World" -Days 1
Displays an informational message which will terminate in 24 hours.

-Message "The world is coming to an end" -MessageLevel Warning
Displays a warning message that "The world is coming to an end" for 10 minutes and terminates.

 
param(
    [Parameter(Mandatory = $true, Position = 0)]
        [string]$Message,
    [Parameter(Mandatory = $false, Position = 1)]
        [string]$MessageHeader="",
    [Parameter(Mandatory = $false, Position = 2)]
        [string]$MessageLevel="64",
    [Parameter(Mandatory = $false, Position = 3)]
        [string]$Days="0",
    [Parameter(Mandatory = $false, Position = 4)]
        [string]$Hours="0",
    [Parameter(Mandatory = $false, Position = 5)]
        [string]$Minutes="10"
)

$MessageTime = New-TimeSpan -Days $Days -Hours $Hours -Minutes $Minutes
$DisplayTime = (get-date) + $MessageTime

$Message+="`n`nThis message will terminate at $DisplayTime`nor when you click OK."
$ConvertedTime=($MessageTime.Minutes*60)+($MessageTime.Hours*60*60)+($MessageTime.Days*60*60*24)

switch ($MessageLevel)
{
    "Error" {$MessageLevel="16"}
    "Warning" {$MessageLevel="48"}
    "Informational" {$MessageLevel="64"}
    default {$MessageLevel="64"}
}

$a = new-object -comobject wscript.shell
$b = $a.popup($Message,$ConvertedTime,$MessageHeader,$MessageLevel)

Happy Scripting!

— Easy link to my blog: https://aka.ms/leesteve
If you like my blogs, please share it on social media and/or leave a comment.