Link to home
Start Free TrialLog in
Avatar of Tek Info
Tek Info

asked on

Modify Powershell script to first prompt the user

I would like a message to pop up before this  Powershell script is run:

"Please note that the following is just a test"
User clicks on OK,,,,and then.....

and then this part of the script is run ..

Function Select-DateForm {
[CmdletBinding()]
Param(
	[String]$Title,
	[String]$Prompt,
	[DateTime]$MinDate,
	[DateTime]$MaxDate
)
	Function Show-FormMain {
		Add-Type -AssemblyName System.Drawing
		Add-Type -AssemblyName System.Windows.Forms
		[System.Windows.Forms.Application]::EnableVisualStyles()

		$formMain = New-Object -TypeName System.Windows.Forms.Form
		$formMain.ClientSize = New-Object -TypeName 'System.Drawing.Size' -ArgumentList 250, 250
		$formMain.FormBorderStyle = 'FixedSingle'
		$formMain.MaximizeBox = $false
		$formMain.MinimizeBox = $true
		$formMain.Text = If ($Title) {$Title} Else {'Date Selection'}
		
		$labelPrompt = New-Object -TypeName System.Windows.Forms.Label
		$labelPrompt.Location = New-Object -TypeName 'System.Drawing.Point' -ArgumentList 16, 8
		$labelPrompt.AutoSize = $true
		$labelPrompt.Text = If ($Prompt) {$Prompt} Else {'Please select a date:'}
		$formMain.Controls.Add($labelPrompt)

		$monthCalendar = New-Object -TypeName System.Windows.Forms.MonthCalendar
		$monthCalendar.Location = New-Object -TypeName 'System.Drawing.Point' -ArgumentList 16, 32
		$monthCalendar.MaxSelectionCount = 1
		If ($MinDate) {$monthCalendar.MinDate = $MinDate}
		If ($MaxDate) {$monthCalendar.MaxDate = $MaxDate}
		$formMain.Controls.Add($monthCalendar)
		
		$buttonOK = New-Object -TypeName System.Windows.Forms.Button
		$buttonOK.Location = New-Object -TypeName 'System.Drawing.Point' -ArgumentList 40, 208
		$buttonOK.Size = New-Object -TypeName 'System.Drawing.Size' -ArgumentList 72, 25
		$buttonOK.Text = '&OK'
		$buttonOK.add_Click({
			Param($Sender, $EventArgs)
				$Script:returnValue = $monthCalendar.SelectionStart
				$formMain.Close()
			})
		$formMain.Controls.Add($buttonOK)
		
		$buttonCancel = New-Object -TypeName System.Windows.Forms.Button
		$buttonCancel.Location = New-Object -TypeName 'System.Drawing.Point' -ArgumentList 120, 208
		$buttonCancel.Size = New-Object -TypeName 'System.Drawing.Size' -ArgumentList 72, 25
		$buttonCancel.Text = '&Cancel'
		$formMain.Controls.Add($buttonCancel)

		$formMain.CancelButton = $buttonCancel
		$formMain.AcceptButton = $buttonOK
		
		[void]$formMain.ShowDialog()
	}
	Show-FormMain
	Return $returnValue
}

$maxDays = 60
$reportDate = Select-DateForm -Prompt 'Select the Date, then click OK:' -MinDate ([DateTime]::Now.AddDays(-1 * $maxDays)) -MaxDate ([DateTime]::Now)
If (-not $reportDate) {
      Exit
}
$reportDate

Open in new window

Avatar of David Johnson, CD
David Johnson, CD
Flag of Canada image

in line 59 add the following
[System.Windows.MessageBox]::Show("Please note that the following is just a test")

Open in new window


Function Select-DateForm {
[CmdletBinding()]
Param(
	[String]$Title,
	[String]$Prompt,
	[DateTime]$MinDate,
	[DateTime]$MaxDate
)
	Function Show-FormMain {
		Add-Type -AssemblyName System.Drawing
		Add-Type -AssemblyName System.Windows.Forms
		[System.Windows.Forms.Application]::EnableVisualStyles()

		$formMain = New-Object -TypeName System.Windows.Forms.Form
		$formMain.ClientSize = New-Object -TypeName 'System.Drawing.Size' -ArgumentList 250, 250
		$formMain.FormBorderStyle = 'FixedSingle'
		$formMain.MaximizeBox = $false
		$formMain.MinimizeBox = $true
		$formMain.Text = If ($Title) {$Title} Else {'Date Selection'}
		
		$labelPrompt = New-Object -TypeName System.Windows.Forms.Label
		$labelPrompt.Location = New-Object -TypeName 'System.Drawing.Point' -ArgumentList 16, 8
		$labelPrompt.AutoSize = $true
		$labelPrompt.Text = If ($Prompt) {$Prompt} Else {'Please select a date:'}
		$formMain.Controls.Add($labelPrompt)

		$monthCalendar = New-Object -TypeName System.Windows.Forms.MonthCalendar
		$monthCalendar.Location = New-Object -TypeName 'System.Drawing.Point' -ArgumentList 16, 32
		$monthCalendar.MaxSelectionCount = 1
		If ($MinDate) {$monthCalendar.MinDate = $MinDate}
		If ($MaxDate) {$monthCalendar.MaxDate = $MaxDate}
		$formMain.Controls.Add($monthCalendar)
		
		$buttonOK = New-Object -TypeName System.Windows.Forms.Button
		$buttonOK.Location = New-Object -TypeName 'System.Drawing.Point' -ArgumentList 40, 208
		$buttonOK.Size = New-Object -TypeName 'System.Drawing.Size' -ArgumentList 72, 25
		$buttonOK.Text = '&OK'
		$buttonOK.add_Click({
			Param($Sender, $EventArgs)
				$Script:returnValue = $monthCalendar.SelectionStart
				$formMain.Close()
			})
		$formMain.Controls.Add($buttonOK)
		
		$buttonCancel = New-Object -TypeName System.Windows.Forms.Button
		$buttonCancel.Location = New-Object -TypeName 'System.Drawing.Point' -ArgumentList 120, 208
		$buttonCancel.Size = New-Object -TypeName 'System.Drawing.Size' -ArgumentList 72, 25
		$buttonCancel.Text = '&Cancel'
		$formMain.Controls.Add($buttonCancel)

		$formMain.CancelButton = $buttonCancel
		$formMain.AcceptButton = $buttonOK
		
		[void]$formMain.ShowDialog()
	}
	Show-FormMain
	Return $returnValue
}
[System.Windows.MessageBox]::Show("Please note that the following is just a test")
$maxDays = 60
$reportDate = Select-DateForm -Prompt 'Select the Date, then click OK:' -MinDate ([DateTime]::Now.AddDays(-1 * $maxDays)) -MaxDate ([DateTime]::Now)
If (-not $reportDate) {
      Exit
}
$reportDate

Open in new window

Avatar of Tek Info
Tek Info

ASKER

Thanks David,
However this does not work for me..
I get this type of error:


Unable to find type [System.Windows.MessageBox].
At......file.ps1:59 char:1
+ [System.Windows.MessageBox]::Show("Please note that the following is  ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Windows.MessageBox:TypeName) [], RuntimeException
    + FullyQualifiedErrorId : TypeNotFound


ASKER CERTIFIED SOLUTION
Avatar of Tek Info
Tek Info

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial