Modify script to prompt user

Member_2_5306354
Member_2_5306354 used Ask the Experts™
on
I would like to modify this PS script so that it prompts the person that is running the script to enter how many days ago the report is for.
At the moment the script is set for 1 day ago, however I want it to prompt the user and then they user can enter 1,2,3 or 4 etc.. days back.. and then it will run the script based on whatever the user has input..

Thanks,

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

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

 ###PROGRAM START###
      # Invokes the Send-MailMessage function to send notification email
      $splat = @{
            From =                  'email1@mail.com'
            To =                  Get-Content -Path ( Join-Path $PSScriptRoot 'EmailDist.txt')
            SmtpServer =      'smtp.email.com'
            Subject =            $subject
            body =                  $MessageBody
            BodyAsHtml =      $true
            Attachment =      ( Join-Path $PSScriptRoot 'Attachment.pdf' )
      }
      Send-MailMessage @splat
###PROGRAM END###
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:
Try this:
$userDays = Read-Host -Prompt "How many days ago is this report for (no input = 1 day)"
If ($userDays) {
	Try {
		$daysAgo = [Int32]$userDays
	} Catch {
		Write-Warning "'$($userDays)' is not a number; will use 1 day!"
		$daysAgo = 1
	}
} Else {
	$daysAgo = 1
}
###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 for: "+$date+" "
	#Body of Email
		$MessageBody = "Attached is the Report for $date. Thank you."
###USER VARIABLES END###

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

Open in new window

Author

Commented:
@ObdA.. thank you very much.  This works well.  However if I want to change it so that I have an option for the date being today, in addition to any past dates, would much have to be modified for this?
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
You mean to allow 0 days?
Easy fix: in line 2, replace
If ($userDays) {
with
If ($userDays -ne $null) {

Author

Commented:
Many thanks for all your help.

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