troubleshooting Question

How to modify Powershell script to use 0 as current day

Avatar of E=mc2
E=mc2 asked on
PowershellScripting Languages
2 Comments1 Solution93 ViewsLast Modified:
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]
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 2 Comments.
Start Free Trial
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 2 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