茫茫網海中的冷日
         
茫茫網海中的冷日
發生過的事,不可能遺忘,只是想不起來而已!
 恭喜您是本站第 1670876 位訪客!  登入  | 註冊
主選單

Google 自訂搜尋

Goole 廣告

隨機相片
IMG_60D_00144.jpg

授權條款

使用者登入
使用者名稱:

密碼:


忘了密碼?

現在就註冊!

微軟帝國 : [轉貼]Display Messagebox with Powershell

發表者 討論內容
冷日
(冷日)
Webmaster
  • 註冊日: 2008/2/19
  • 來自:
  • 發表數: 15771
[轉貼]Display Messagebox with Powershell
#28 : Display Messagebox with Powershell
Although, I never came accross such a situation where I need to throw a messagebox. But, just as I was playing, I tried to throw messagebox from Powershell.

Generating a Messagebox -

1. Load the Assembly
[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")

Output-
GAC Version Location
--- ------- --------
True v2.0.50727 C:\Windows\assembly\GAC_MSIL\System.Windows.Forms\2.0.0.0__b77a5c561934e089\System.Windows.Forms.dll

Note: If you don't want the output, you can simple redirect to Out-nul. This will skip displaying assembly loading statement.

2. Display a simple Messagebox
[System.Windows.Forms.MessageBox]::Show("We are proceeding with next step.")

Now, the messagebox appears something like this -


If you see above message, you will find Title is missing. Let's add a title also by adding below piece of code -
[System.Windows.Forms.MessageBox]::Show("We are proceeding with next step." , "Status")



So, this was all about showing message with title. This was just OK message so, there is nothing to decide for user except pressing OK button.
Types of Messageboxes :
We have 6 types of Messageboxes in Powershell -
0: 	OK
1: 	OK Cancel
2: 	Abort Retry Ignore
3: 	Yes No Cancel
4: 	Yes No
5: 	Retry Cancel

Note: The number mentioned in left is the third parameter of Messagebox.

If you want to show Yes No, just add 4 as third parameter -
[System.Windows.Forms.MessageBox]::Show("We are proceeding with next step." , "Status" , 4)

Now, this will display a Messagebox like this -


How to get values from Messagebox?
As you know, when you press any button, you need to get the result and work upon the decision -
$OUTPUT= [System.Windows.Forms.MessageBox]::Show("We are proceeding with next step." , "Status" , 4)
if ($OUTPUT -eq "YES" )
{
..do something

}
else
{
..do something else
}

The value of button pressed is stored in $OUTPUT variable. This variable can then be used for your programming logic.

I have given just a primer how to use Messagebox class. But, if you want to go indepth of System.Windows.Forms.MessageBox class, you may look for the link below -

http://msdn.microsoft.com/en-us/library/system.windows.forms.messageboxbuttons.aspx

原文出處:
Som's Powershell Tips: #28 : Display Messagebox with Powershell
冷日
(冷日)
Webmaster
  • 註冊日: 2008/2/19
  • 來自:
  • 發表數: 15771
[轉貼]Powershell How to show a message box

Powershell: How to show a message box

Sometimes while a powershell script is running you want to show a MessageBox with a information or warning to the user. In Windows Powershell no Commandlet exists to show a Message Box.

Nevertheless it is possible by using the .NET Windows.Forms.MessageBox class:-).

First of all load the assembly.



# Load assembly
[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")

To show the messagebox call the static function show(“Message Text”)



$oReturn=[System.Windows.Forms.Messagebox]::Show("This is the Message text")

The function returned a value of enum System.Windows.Forms.DialogResult, that indicates which Button was pressed.
Possible Returncodes, depending on which button was pressed, are:



[system.enum]::getValues($oReturn.GetType())
None
OK
Cancel
Abort
Retry
Ignore
Yes
No

The default Button is the OK Button, there are further 5 combinations, see below.



[system.enum]::getNames([System.Windows.Forms.MessageBoxButtons])|foreach{[console]::Writeline("{0,20} {1,-40:D}",$_,[System.Windows.Forms.MessageBoxButtons]::$_.value__)}
OK 0
OKCancel 1
AbortRetryIgnore 2
YesNoCancel 3
YesNo 4
RetryCancel 5

An Example, a Message box with an Ok and a Cancel button and a check which button was pressed:



$oReturn=[System.Windows.Forms.MessageBox]::Show("Message Text","Title",[System.Windows.Forms.MessageBoxButtons]::OKCancel)
switch ($oReturn){
"OK" {
write-host "You pressed OK"
# Enter some code
}
"Cancel" {
write-host "You pressed Cancel"
# Enter some code
}
}

[System.Windows.Forms.MessageBoxButtons]::OKCancel

[System.Windows.Forms.MessageBoxButtons]::OKCancel

Some examples

[System.Windows.Forms.MessageBoxButtons]::AbortRetryIgnore

[System.Windows.Forms.MessageBoxButtons]::AbortRetryIgnore


[System.Windows.Forms.MessageBoxButtons]::YesNoCancel

[System.Windows.Forms.MessageBoxButtons]::YesNoCancel

you can also use the number instead of the numeric constants to specify the buttons



[System.Windows.Forms.MessageBox]::Show("Message Text","Title",1)

This oneliner shows all possible Button combinations consecutively



[system.enum]::getValues([System.Windows.Forms.MessageBoxButtons])|foreach {[System.Windows.Forms.MessageBox]::Show("["+$_.GetType()+"]::"+$_.ToString(),"Message box Buttons",$_)}

You can style the message box with an icon, 4 are available



[system.enum]::getNames([System.Windows.Forms.MessageBoxIcon])|foreach{[console]::Writeline("{0,20} {1,-40:D}",$_,[System.Windows.Forms.MessageBoxIcon]::$_.value__)}
None 0
Hand 16
Error 16
Stop 16
Question 32
Exclamation 48
Warning 48
Asterisk 64
Information 64

Example:



[System.Windows.Forms.MessageBox]::Show("Message Text","Title",[System.Windows.Forms.MessageBoxButtons]::OKCancel,[System.Windows.Forms.MessageBoxIcon]::Warning)

[System.Windows.Forms.MessageBoxIcon]::Warning

[System.Windows.Forms.MessageBoxIcon]::Warning

Same with numbers instead of numeric constants



[System.Windows.Forms.MessageBox]::Show("Message Text","Title",1,48)

the remaining…

[System.Windows.Forms.MessageBoxIcon]::Question

[System.Windows.Forms.MessageBoxIcon]::Question

[System.Windows.Forms.MessageBoxIcon]::Hand, Stop or Error

[System.Windows.Forms.MessageBoxIcon]::Hand, Stop or Error


[System.Windows.Forms.MessageBoxIcon]::Asterisk or Information

[System.Windows.Forms.MessageBoxIcon]::Asterisk or Information

 

All available icons consecutively



[system.enum]::getValues([System.Windows.Forms.MessageBoxIcon])|foreach {[System.Windows.Forms.Messagebox]::Show("["+$_.GetType()+"]::"+$_.ToString(),"Message box Icons",[System.Windows.Forms.MessageBoxButtons]::OK,$_)}

Michael


原文出處:How to show a message box with windows powershellMichls Tech Blog
前一個主題 | 下一個主題 | 頁首 | | |



Powered by XOOPS 2.0 © 2001-2008 The XOOPS Project|