0

Good morning

Is there a way I can have mandatory parameters filled up by user input in a message box?

Param(

  [Parameter(Mandatory=$True)][string]$parameter1,
  [Parameter(Mandatory=$True)][string]$parameter2,
  [Parameter(Mandatory=$True)][string]$parameter3

)

For instance, I want a pop-up box with a text field which the user must fill-up the value required by the mandatory parameter

$confirm = [System.Windows.MessageBox]::Show('Please confirm you want to start)

thanks a million to anyone who answers.

  • here is an example I found https://learn.microsoft.com/en-us/previous-versions/windows/it-pro/windows-powershell-1.0/ff730941(v=technet.10)?redirectedfrom=MSDN – Guenther Schmitz Aug 14 '20 at 06:31
  • My problem is not really the GUI since this line will do a text input box ```[System.Windows.MessageBox]::Show('Please confirm you want to start)``` but my problem is that in this text input box I need to put mandatory parameters – user11557166 Aug 14 '20 at 07:34
  • Instead of making parameter mandatory, you can make a input text validation in form of [string]::IsNullOrEmpty($string) – Karolina Ochlik Aug 14 '20 at 07:36
  • Does this answer your question? [Simple InputBox function](https://stackoverflow.com/questions/30534273/simple-inputbox-function) – Jawad Aug 14 '20 at 12:40
  • `[Parameter(Mandatory=$True)][string]$parameter1 = [System.Windows.MessageBox]::Show('Please confirm you want to start')` does this work as expected? – CrookedJ Aug 14 '20 at 13:05

1 Answers1

0

As mentioned in the answer linked in the comments, you can use the [Microsoft.VisualBasic.Interaction]::InputBox() static method to prompt the user for string input.

Now for the parameter binding behavior. This might seem a bit counter-intuitive, but you can override the default prompt with you're own GUI prompt if you remove the Mandatory flag and instead provide an expression that calls your GUI component as its default parameter value:

Param(
  [Parameter(Mandatory=$True)][string]$parameter1 = $([Microsoft.VisualBasic.Interaction]::InputBox("What should parameter1 be?", "Parameter1 input prompt"))
)

Now, if the user doesn't supply parameter argument, PowerShell won't interrupt with its own prompt (since the parameter is no longer Mandatory), and instead proceeds to evaluate the default value expression, in turn showing the input box prompt

Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206