How to modify Powershell script to use 0 as current day

Member_2_5306354
Member_2_5306354 used Ask the Experts™
on
I would like to modify this Powershell script so if you enter the number 0, it will accept today's date.
If Enter is pressed, it should not move ahead until only a Number is entered for the amount of days back.

Thanks in advance.

[code]$userDays = Read-Host -Prompt "How many days ago is this report for (no input = today)"
If ($userDays -ne $null) {
      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###/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:
Like taht, for example?
$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###

Open in new window

Author

Commented:
Perfect, thank you.

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