Modify Powershell report to show a calendar

Member_2_5306354
Member_2_5306354 used Ask the Experts™
on
I would like to modify this script, so that a calendar showing the last 30 days shows, after which at that time the users can input how many days back they want to the report to run.

[code]$maxDays = 365
Do {
      $userDays = Read-Host -Prompt "How many days ago is this report for (0 = today, max $($maxDays), X = exit)"
      $userDays = $userDays.Trim()
} Until (($userDays -eq 'X') -or (($userDays -match '\d+') -and ($maxDays -ge $userDays)))
If ($userDays -eq 'X') {
      Exit
}
$daysAgo = [Int32]$userDays
$daysAgo
###SETUP START###
#-------DO NOT MODIFY-------#
      #Yesterdays Date
            $date = ((Get-Date).AddDays((-1 * $daysAgo))).ToString("MMM'/'dd'/'yyyy")
###SETUP END###

###USER VARIABLES START###
#-------MODIFY AS NEEDED-------#
    #Mail Subject
        $Subject = "Report: "+$date+" "
    #Body of Email
        $MessageBody = "Attached is theReport for $date. Thank you."
###USER VARIABLES END###

###PROGRAM START###
      # Invokes the Send-MailMessage function to send notification email
      $splat = @{
            From =                  'email1@email.com'
            To =                  Get-Content -Path ( Join-Path $PSScriptRoot 'EmailDistList.txt')
            SmtpServer =      'smtp.server.com'
            Subject =            $subject
            body =                  $MessageBody
            BodyAsHtml =      $true
            Attachment =      ( Join-Path $PSScriptRoot 'Attachment.pdf' )
      }
      Send-MailMessage @splat
###PROGRAM END###/code]
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
This now uses the selected date directly:
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 208, 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 = 30
$reportDate = Select-DateForm -Prompt 'Select the report start date:' -MinDate ([DateTime]::Now.AddDays(-1 * $maxDays)) -MaxDate ([DateTime]::Now)
If (-not $reportDate) {
      Exit
}
$reportDate

###SETUP START###
#-------DO NOT MODIFY-------#
      #Yesterdays Date
            $date = $reportDate.ToString("MMM'/'dd'/'yyyy")
###SETUP END###

###USER VARIABLES START###
#-------MODIFY AS NEEDED-------#
    #Mail Subject
        $Subject = "Report: "+$date+" "
    #Body of Email
        $MessageBody = "Attached is theReport for $date. Thank you."
###USER VARIABLES END###

###PROGRAM START###
      # Invokes the Send-MailMessage function to send notification email
      $splat = @{
            From =                  'email1@email.com'
            To =                  Get-Content -Path ( Join-Path $PSScriptRoot 'EmailDistList.txt')
            SmtpServer =      'smtp.server.com'
            Subject =            $subject
            body =                  $MessageBody
            BodyAsHtml =      $true
            Attachment =      ( Join-Path $PSScriptRoot 'Attachment.pdf' )
      }
      Send-MailMessage @splat
###PROGRAM END###

Open in new window

Author

Commented:
Thanks so much.  This seems to work however I can't seem to see the Saturday dates since the Windows is cut off on the right side.. I am not fully able to see the calendar window.... Is there a way so that I can also see the Saturday dates on the right side?
Thanks so much in advance, however this would be perfect if I was just able to see those dates.
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
Did you get it fixed by increasing the form width, or do you still need help with this?

Author

Commented:
Yes thanks so much I got this working

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial