troubleshooting Question

Modify Powershell script to first prompt the user

Avatar of Tek Info
Tek Info asked on
Scripting LanguagesPowershell
3 Comments1 Solution32 ViewsLast Modified:
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
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 3 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 3 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros