Am sure I'm way off here & there's probably an easier way to do this, but I have to change some cron text schedules & change the hour in which they run..
The cron itself is in a text file so I was thinking of using powershell to get the content, update it & save it.. but I'm not very good with regex etc with this. -I've cobbled something together from internet sources, but something is still slightly missing....
I can do all the grabbing of the file content & writing etc, it's just the slightly more advanced replacement of the 2nd variable that's an issue....
Here's my code:
$testText = "cron(0 20 * * ? *)"
$pattern = '(\().+?(\))'
$new1 = [regex]::Matches($testText, $pattern).Value
"new1 $new1"
$new2 = $($new1 -replace '\s+', ' ').split()
"new2 $new2"
$new3 = $new2[1]-1 #Set back an hour
#$new3 = $new2[1] -replace $new2[1],"22" #Specify Hour
"new3 $new3"
$new4 = $testText -Replace($new2,$new3)
"new4 $new4"
This results in this output - where annoyingly the first bit is knocked off..
new1 (0 20 * * ? *)
new2 (0 20 * * ? *)
new3 19
new4 cron(19* * ? *)
I've split this using spaces as the 1st item after the parenthesis can be 1 or 2 digits & after the 2nd set of chars(the hour) these can also be different.. i.e. cron(30 06 ? * MON-FRI *)
Any help would be appreciated... - it's been a long day & my brain is very tired!!
$testText = "cron(0 20 * * ? *)"
$cron = $testText.split(' ').split('(').split(')')
'cron(' +
$cron[1] + ' ' +
(([datetime]::Today).AddHours($cron[2] -1).Hour) + ' ' +
$cron[3] + ' ' +
$cron[4] + ' ' +
$cron[5] + ' ' +
$cron[6] +
')'
or perhaps a little easier
$testText = "cron(0 20 * * ? *)"
$cron = $testText.split(' ').split('(').split(')')
$cron[2] = ([datetime]::Today).AddHours($cron[2]-1).Hour
'cron(' + ($cron -join ' ') + ')'