0

We have a batch file that runs an end-of-month process. Right now it's a manual process, but we'd like to automate it based on when EOM falls. If the last work day of the month is a Friday (or other weekday), we run the script on Friday night or Saturday. If it falls on a Saturday or Sunday, the script is run on Monday following the weekend. There may be a few exceptions, but that's the general idea.

We're having trouble figuring out how to automate this based on date. Any options will be considered. Powershell, batch, etc...

Any help would be greatly appreciated.

Edit - The dates it selects to run can be a bit random. If we could have it read in a text file with a list of dates to run that would work too.

So we could have a list of dates like: 04-30-2015, 05-31-2015, 06-29-2015,

Then a script could be run that says if today is equal to any of these dates, run the batch file.

subzeroLV
  • 73
  • 1
  • 1
  • 8
  • Does it have to be batch? Why not just Windows Task Scheduler? It would be much simpler - I think to do your logic there. – Leptonator Apr 06 '15 at 18:33
  • So... Scheduled task running on the 1st of each month? – Mathias R. Jessen Apr 06 '15 at 19:04
  • @MathiasR.Jessen Don't think so since EOM for June would be run on July 3rd from what I gather. _Friday night or Saturday_ is where I wonder since are we supposed to guess then? – Matt Apr 06 '15 at 19:07
  • A simple way to do this would be to run the script daily using the Windows task scheduler and have the script check if it's the correct time to actually do something. – jurgenb Apr 06 '15 at 19:13
  • @n3wjack I think that is what the op wants but he needs help with _have the script check if it's the correct time_ – Matt Apr 06 '15 at 19:15
  • The current edit make this _A LOT_ easier. Running a task against that would take all of the logic out of this. Your script just has to check what day it is. – Matt Apr 06 '15 at 19:40

2 Answers2

0

The logic isn't completely clear to me, but as said in the comment above, you could run a PowerShell script using Windows Task Scheduler every day (or only Friday/Monday?) and have that script check if the time is right to do something.

From what I can tell it either has to run on Friday or Monday. You can get the current date in PowerShell with the Get-Date command. If you pass this through Get-Member you can see all the methods you have on the date object to figure out if the time is right to do something.

get-date | get-member

You'll probably need some methods or properties like this to implement the check:

$today = get-date 
$today.DayOfWeek         # prints e.g. "Monday"
$today.DayOfWeek -eq 1   # Returns True on Monday
$today.AddDays(1)        # Next day, the number can be negative or positive
$today.Day              # Returns 6 right now (april 6th)
jurgenb
  • 472
  • 4
  • 11
0

There are plenty of resources that discuss calling a PowerShell script in Task Scheduler. If what you currently do is run a batch then configure your task to run at 5:00pm every day checking the date against all the dates in your text file.

$milestones = Get-Content c:\temp\dates.txt
$today = Get-Date -Format "MM-dd-yyyy"
If($milestones -contains $today){
    # Do stuff and things. 
    # cmd.exe /K C:\Path\To\Batch.bat
}

If there was a line in the text file "c:\temp\dates.txt" for "04-06-2015" that would satisfy the If condition. Then you could uncomment the line with cmd and update as required.

If you have issues with these concepts it is expected that you do a little research before you ask. If you are still stuck after that please either edit your question of ask a new question.

Community
  • 1
  • 1
Matt
  • 45,022
  • 8
  • 78
  • 119