简体   繁体   中英

How to create a popup message in Powershell without buttons

I'm trying to create a message dialogue in Powershell where the user has no option to action on the message as that is the intention. So the message will have the X button grayed along with the buttons (not showing buttons are even better).

The closest I could reach was disabling the X via below code:

$wshell = New-Object -ComObject Wscript.Shell -ErrorAction Stop
$wshell.Popup("Aborted",0,"ERROR!",48+4)

But cannot figure out disabling button part. Below MS articles were of little help as well:

http://blogs.technet.com/b/heyscriptingguy/archive/2006/07/27/how-can-i-display-a-message-box-that-has-no-buttons-and-that-disappears-after-a-specified-period-of-time.aspx

https://msdn.microsoft.com/en-us/library/x83z1d9f(v=vs.84).aspx

Referred to few other articles over net some even suggesting custom made buttons using HTML, or VB library. But not what I was looking for.

Any help/hint/suggestion would be deeply appreciated.

Regards, Shakti

Dig into the .NET Windows.Forms namespace, you can make pretty much any kind of window you want with that:

https://msdn.microsoft.com/en-us/library/system.windows.forms.aspx

Here's a quick sample window w/ no buttons that can't be moved/closed by the user, but closes itself after 5 seconds:

Function Generate-Form {

    Add-Type -AssemblyName System.Windows.Forms    

    # Build Form
    $objForm = New-Object System.Windows.Forms.Form
    $objForm.Text = "Test"
    $objForm.Size = New-Object System.Drawing.Size(220,100)

    # Add Label
    $objLabel = New-Object System.Windows.Forms.Label
    $objLabel.Location = New-Object System.Drawing.Size(80,20) 
    $objLabel.Size = New-Object System.Drawing.Size(100,20)
    $objLabel.Text = "Hi there!"
    $objForm.Controls.Add($objLabel)

    # Show the form
    $objForm.Show()| Out-Null

    # wait 5 seconds
    Start-Sleep -Seconds 5

    # destroy form
    $objForm.Close() | Out-Null

}

generate-form

Using the script above as a launching point I'm attempting to make a function that will allow me to popup a please wait message run some more script then close the popup

Function Popup-Message {

    param ([switch]$show,[switch]$close)

    Add-Type -AssemblyName System.Windows.Forms  

    # Build Form
    $objForm = New-Object System.Windows.Forms.Form
    $objForm.Text = "Test"
    $objForm.Size = New-Object System.Drawing.Size(220,100)

    # Add Label
    $objLabel = New-Object System.Windows.Forms.Label
    $objLabel.Location = New-Object System.Drawing.Size(80,20) 
    $objLabel.Size = New-Object System.Drawing.Size(100,20)
    $objLabel.Text = "Hi there!"
    $objForm.Controls.Add($objLabel)



    If ($show)
    {
        $objForm.Show() | Out-Null
        $global:test = "Show"
    }


    If ($close)
    {
        # destroy form
        $objForm.Close() | Out-Null
        $global:test = "Close"
    }
}

I can then get the popup to display by:

Popup-Message -show

At this point I can see the $test variable as Show

But when I try to close the window with:

Popup-Message -close

But the popup window will not close If I look at $test again it will show as Close

I'm assuming this has something to do with keeping the function in the Global Scope but I can't figure out how to do this with the form

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM