Create a popup message in Powershell

久未见 提交于 2021-02-08 09:46:24

问题


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


回答1:


kpogue's helpful answer explains your problem well and offers a solution that is effective, but suboptimal due to relying on global variables.

Let me suggest a different approach, where you use a function to simply define the form and return that definition, on which you can call the .Show() and .Close() methods as needed:

Function New-PopUpForm {

  Add-Type -AssemblyName System.Windows.Forms  

    # Create the form.
    $objForm = New-Object System.Windows.Forms.Form -Property @{
      Text = "Test"
      Size = New-Object System.Drawing.Size 220,100
    }

    # Create a label...
    $objLabel = New-Object System.Windows.Forms.Label -Property @{
      Location = New-Object System.Drawing.Size 80,20
      Size = New-Object System.Drawing.Size 100,20
      Text = "Hi there!"
    }

    # ... and add it to the form.
    $objForm.Controls.Add($objLabel)

    # Return the form.
    return $objForm
}

You can then use the function as follows:

$form = New-PopupForm

$form.Show() 

# ...

$form.Close()

Note that once you call .Close(), the form instance stored in $form is disposed of and cannot be reused - simply call New-PopupForm again to create a new instance.




回答2:


You're creating a new instance of the form in each call to Popup-Message. So, the form is not closing becausing you're trying to close a new form which has never been opened. Meanwhile, the old form no longer has anyone referencing it, it's just sitting out in space. You can create a global reference as shown below, there are probably more artful ways of doing this.

Function Popup-Message {

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

Add-Type -AssemblyName System.Windows.Forms  

# Build Form
if ($Global:objForm -eq $null){
$Global:objForm = New-Object System.Windows.Forms.Form
$Global:objForm.Text = "Test"
$Global:objForm.Size = New-Object System.Drawing.Size(220,100)

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



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


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



回答3:


we can use One line to create the powershell popup with Wscript.Shell COM object

Syntax: intButton = objShell.Popup(strText,[nSecondsToWait],[strTitle],[nType])

#creating object os WScript
$wshell = New-Object -ComObject Wscript.Shell -ErrorAction Stop
#invoking the POP method using object
$wshell.Popup("please wait message?",5,"Wait Until Scrip execution",48+4)


来源:https://stackoverflow.com/questions/55013191/create-a-popup-message-in-powershell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!