1

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')

Veck
  • 125
  • 1
  • 3
  • 13
  • try ShowDialog and not show – Esperento57 Feb 27 '18 at 04:42
  • Does this answer your question? [In Powershell how do I bring a messagebox to the foreground, and change focus to a button in the message box](https://stackoverflow.com/questions/34299433/in-powershell-how-do-i-bring-a-messagebox-to-the-foreground-and-change-focus-to) – Dennis Mar 27 '23 at 17:17

2 Answers2

3

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

Kriss Milne
  • 509
  • 2
  • 4
1

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")
John Zabroski
  • 2,212
  • 2
  • 28
  • 54