0

If I run the script on 2/4/2013or any other day in February I want the date to be change to 1/1/2013. I am replacing date on line 59 with this value.

#Get the content of the CMS Script.
$CMSReport = Get-content C:\reports\CMSReport.acsauto

# Go to line 59 and replace the date for Last Month date.
$CMSReport[58] = $CMSReport[58] -replace "([1-9]|0[1-9]|1[012])[- /.]([1-9]|0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d", [datetime]::Today.ToShortDateString()
$CMSReport | Set-Content C:\reports\testCMS.acsauto

#Run the CMS script
Invoke-Expression -command "c:\reports\testCMS.acsauto"
Khaled
  • 36,533
  • 8
  • 72
  • 99
Carlos
  • 21
  • 1
  • 1
  • 4

2 Answers2

3

Get the value, parse it as a date, AddMonths(-1), put it back. Replace is not the way to go here.

Bart De Vos
  • 17,911
  • 6
  • 63
  • 82
0
# Todays date
$cntDate = Get-Date

# First day of current month
$firstCntMonth = Get-Date -Day 1 -Month $cntDate.Month -Year $cntDate.Year -Hour 0 -Minute 0 -Second 0

# Last day of previous month
$lastPrevMonth = $firstCntMonth.AddDays(-1)

Write-Host $lastPrevMonth

In this example I used Get-Date to polulate the $cntDate variable, you might want to construct the current date from elsewhere.

I'm sorry, without more information about where you're grabbing the date from I can't really tell you much more.. The other answer has the right idea, grab the other date, calculate the value that you want and insert the correct date rather than trying to replace in one line.