Veck
Veck

Reputation: 125

How to bringtofront a messagebox in Powershell

Just need a little help with a Powershell Script.

I have a last messagebox on my script. what i want to accomplish is bring the messagebox in front of all the windows.

cmdlet that i use is

$end=[system.Windows.Forms.Messagebox]::Show('StartUP Tool Progress Completed!','StartUP Warning')

Upvotes: 1

Views: 4879

Answers (2)

Kriss Milne
Kriss Milne

Reputation: 529

Alternatively, if all you need is a message box you can use the Wscript Shell:

$wshell = New-Object -ComObject Wscript.Shell
$wshell.Popup("StartUP Tool Progress Completed",0,"Completed",0x0)

For more information: Popup Method

Upvotes: 3

John Zabroski
John Zabroski

Reputation: 2359

This is a WinForms question, more than a PowerShell question. You'll need to pass in Form.ActiveForm. Form.ActiveForm would give you the currently active form, even if you are raising your MessageBox from any other class.

However, I think you might want to look at Read-Host -AsSecureString or, more preferably, Get-Credential if the prompt is for confidential data.

Read-Host uncontrollably stops the script to prompt the user, which means that you can never have another script that includes the script that uses Read-Host.

Thankfully, PowerShell has a lot of built-in help for launching dialogs. You're trying to ask for parameters.

You should use the 

[Parameter(Mandatory=$true)]

 attribute, and correct typing, to ask for the parameters. Read up on "params" if you haven't already.

If you use Parameter attribute on a [SecureString], it will prompt for a password field. If you use this on a Credential type, ([Management.Automation.PSCredential]), the credentials dialog will pop up, if the parameter isn't there. A string will just become a plain old text box. If you add a HelpMessage to the parameter attribute (that is, [Parameter(Mandatory = $true, HelpMessage = 'New User Credentials')]) then it will become help text for the prompt.

Finally, you can try this dirty trick, leveraging Microsoft Visual basic DLLs:

[System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic') | Out-Null

$computer = [Microsoft.VisualBasic.Interaction]::InputBox("Enter a computer name", "Computer", "$env:computername")

Upvotes: 1

Related Questions