MSH - Is it possible to show a message box and and get input from user

17 weergaven
Naar het eerste ongelezen bericht

Rahul Agarwal

ongelezen,
2 dec. 2005 01:54:5902-12-2005
aan
Hi All

Is it possible to show a windows message box to alert or get user input
while running a script. I found that prompting on the command line can
sometime be easily missed if its a long running script and there is lots of
output of the same, a windows message box would be helpful.

thanks in advance
rahul


James Truher [MSFT]

ongelezen,
2 dec. 2005 15:32:1902-12-2005
aan

popping up a message box is pretty straight forward

[void][reflection.assembly]::LoadWithPartialName("system.windows.forms")
$msg = [windows.forms.messagebox]
$button = [windows.forms.messageboxbuttons]::OK
$icon = [windows.forms.messageboxicon]::Exclamation
$msg::show("Message Here","Title Here",$button,$icon)

or using COM, it's even simpler

$shell = new-object -COM wscript.shell
$shell.popup("Message",10,"Title",32)

This has the benefit of having a timeout (second parameter is timeout in
seconds). Checkout the MSDN documentation on the Popup method for more
info.

However, collecting input from the user using a gui form is a little more
involved and is really an exercise in winforms programming. Here's the
simplest way I know to do this - but I'm really not a windows programmer.
If somebody knows a better way, I'd love to know!

[void][reflection.assembly]::LoadWithPartialName("system.windows.forms")
# Create the form
$form = new-object system.windows.forms.form
$form.Text = "MSH Input"
$form.height = 110
# Create the label
$label = new-object system.windows.forms.label
$label.Text = "Enter your name:"
$label.dock = "top"
# Create the textbox for input
$textbox = new-object system.windows.forms.textbox
$textbox.dock = "top"
# Create the OK button and when the OK button is clicked hide the form
$OK = new-object system.windows.forms.button
$OK.Text = "OK"
$OK.dock = "bottom"
$OK.Add_Click({$form.hide()})
# add the controls to the form
$form.Controls.AddRange(@($textbox,$label,$OK))
# This bit of code is needed because the focus is given
# to the form, so we use Shell.AppActivate to force
# the issue
$form.visible = $true
$shell = new-object -COM wscript.shell
[void]$shell.AppActivate("MSH Input")
$form.visible = $false
# show the form
$results = $form.showdialog()
# show the results
$textbox.Text
$form.dispose()


--
James W. Truher [MSFT]
Monad Program Management
Microsoft Corporation
This posting is provided "AS IS" with no warranties, and confers no rights.

"Rahul Agarwal" <agarwa...@hotmail.com> wrote in message
news:uTSjR1w9...@TK2MSFTNGP10.phx.gbl...

/\/\o\/\/

ongelezen,
2 dec. 2005 16:53:1702-12-2005
aan

I have three other possibilities

1) you can use the VB.NET inputbox but it has also the Focus Problem.

[reflection.assembly]::LoadWithPartialName("'Microsoft.VisualBasic")
$r = [Microsoft.VisualBasic.Interaction]::inputbox("Enter :","title")
$r
input

2) or you can use VBscript, that handles the focus right.

make this vbscript :

'InputBox.vbs
a = inputbox(wscript.arguments(0),wscript.arguments(1))
wscript.echo a

Make following Function :

MSH>function inputbox {cscript //nologo G:\Monad\inputbox.vbs $args[0]
$args[1]}

Use like this.

MSH>inputbox "enter" "title"
input

and Add errorhandling as you like ;-)

the Vbscript way is the most simple.

3) or you can do the .NET part and compile it to an exe.

run this MSH script :

$provider = new-object Microsoft.VisualBasic.VBCodeProvider
$params = new-object System.CodeDom.Compiler.CompilerParameters
$params.GenerateExecutable = $true
$params.GenerateInMemory = $False
$refs = "System.dll","Microsoft.VisualBasic.dll","System.Data.DLL"
$params.ReferencedAssemblies.AddRange($refs)
# VB.NET EXAMPLE
$txtCode = @'
Imports Microsoft.VisualBasic
Imports System
Module Module1
Sub Main(ByVal strArgs() As String)
dim result as string
result = inputbox(strargs(0),strArgs(1))
console.writeline(result)
End Sub
End Module
'@
$results = $provider.CompileAssemblyFromSource($params, $txtCode)
$results

TempFiles : {C:\Documents and Settings\mow\Local
Settings\Temp\2heysyo7.exe}
Evidence :
CompiledAssembly : 2heysyo7, Version=0.0.0.0, Culture=neutral,
PublicKeyToken=null
Errors : {}
Output : {}
PathToAssembly : C:\Documents and Settings\mow\Local
Settings\Temp\2heysyo7.exe
NativeCompilerReturnValue : 0

MSH>& 'C:\Documents and Settings\mow\Local Settings\Temp\2heysyo7.exe'
"test" "title"
lol

then you can rename the generated exe and use it then.

this is the way I use (whith errorchecking that is ;-))

MSH>$r = G:\Monad\Inputbox.exe "test" "title"
MSH>$r
input

gr /\/\o\/\/

/\/\o\/\/

ongelezen,
2 dec. 2005 18:15:5902-12-2005
aan
> James Truher [MSFT] wrote:
>
>> popping up a message box is pretty straight forward
>>
>> [void][reflection.assembly]::LoadWithPartialName("system.windows.forms")
>> $msg = [windows.forms.messagebox]
>> $button = [windows.forms.messageboxbuttons]::OK
>> $icon = [windows.forms.messageboxicon]::Exclamation
>> $msg::show("Message Here","Title Here",$button,$icon)
>>

this can also be written like this (you can leave out the TYPE)

[System.windows.forms.messagebox]::show("Message",'Title','OK','Exclamation')

So very straight forward, ...
... but has the Same Focus issue as the InputBox,so is a bit useless.

gr /\/\o\/\/

(also here the VBscript or .NET exe ways mentioned by me for the
Inputbox will go, and the COM way mentioned by James Truher)

James Truher [MSFT]

ongelezen,
2 dec. 2005 20:50:2302-12-2005
aan
inline below

--
James W. Truher [MSFT]
Monad Program Management
Microsoft Corporation
This posting is provided "AS IS" with no warranties, and confers no rights.

"/\/\o\/\/" <n...@Spam.mow> wrote in message
news:eR2Res49...@TK2MSFTNGP09.phx.gbl...
<snip>


>
> I have three other possibilities
>

> 2) or you can use VBscript, that handles the focus right.
>
> make this vbscript :
>
> 'InputBox.vbs
> a = inputbox(wscript.arguments(0),wscript.arguments(1))
> wscript.echo a
>
> Make following Function :
>
> MSH>function inputbox {cscript //nologo G:\Monad\inputbox.vbs $args[0]
> $args[1]}
>
> Use like this.
>
> MSH>inputbox "enter" "title"
> input
>
> and Add errorhandling as you like ;-)
>
> the Vbscript way is the most simple.

I like this very much - taking a clue from you C# example, I created a
script that builds the vbs file and then deletes it after running:

param ( $Message, $Title )
function new-tempfile ( $extension )
{
$env:temp + "\" + (new-object random (get-date).totalseconds).next() +
"$extension"
}
trap { remove-item $tempfile ; exit }
$tempfile = new-tempfile .vbs

@"


a = inputbox(wscript.arguments(0),wscript.arguments(1))
wscript.echo a

"@ > $tempfile

cscript //nologo $tempfile $Message $Title
remove-item $tempfile


That way I can distribute this as a whole and don't need to hand out more
files (similar to your C# version, but without the compilation, etc.)


<snip>


/\/\o\/\/

ongelezen,
4 dec. 2005 05:15:4104-12-2005
aan

Glad you like the Vbscript solution,

Still this focusing issue is annoying, it happens with all forms.

with a form I can set the TopMost property to solve this.

$form.topmost = $true

this has his problems to (as it stay's topmost) for dialog usage it is
doable.

I know GUI is not "officially supported" yet in MSH (till V2 ?),
but it even happens if I try to "wrap" it in a CMDlet.

for example this Example to get to the OpenFileDialog :


using System;
using System.Windows.Forms;
using System.Management.Automation;
// ShowFileDialog.cs
/// OpenFileDialog CDMlet
/// /\/\o\/\/ 2005

[Cmdlet("show", "OpenfileDialog")]
public class ShowOpenFileDialog : Cmdlet
{
protected override void EndProcessing()
{
System.Windows.Forms.OpenFileDialog OFD = new
System.Windows.Forms.OpenFileDialog() ;
DialogResult result = new DialogResult();
result = OFD.ShowDialog();
Console.WriteLine(result);
WriteObject(OFD.FileName);
}
}

it still will not get the focus, so I can not implement a dialog in a
CMDlet also.

it would be very nice to have a way to get them Focus and hence make it
possible to use the standard dialogs in MSH.

gr /\/\o\/\/


Allen beantwoorden
Auteur beantwoorden
Doorsturen
0 nieuwe berichten