December 9, 2010

How to display a Yes/No message box ?

There isn’t a msgbox or an input function in Powershell, but you can directly call Windows.Forms.MessageBox  to do it. Thansks to Bart De Smet for his tutorial about that.

[reflection.assembly]::Load("System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089") | out-null

#.SYNOPSIS 
# Display a confrimation dialog
# Return$True if the user click 'Yes'
#.LINK
# http://bartdesmet.net/blogs/bart/archive/2006/09/16/4429.aspx
function ConfirmMessageBox {
	param(
		[parameter(
		Mandatory=$False)]
		[String]$WinTitle='PowerShell Script',
		[parameter(
		Mandatory=$False)]		
		$MsgText='Do you really want to continue ?'
	)
	$result = [Windows.Forms.MessageBox]::Show($MsgText, $WinTitle, [Windows.Forms.MessageBoxButtons]::YesNo, [Windows.Forms.MessageBoxIcon]::Question)
	If ($result -eq [Windows.Forms.DialogResult]::Yes) {
		Return $true
	}
	Else {
		Return $false
	}
}

No comments:

Post a Comment