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.
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)
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.
Related
i have a .ps script file job which runs daily(except weekends and holidays). i have block of code which should execute once in a month only. as it is a daily job so it should execute first working day of the month only. Rest of the code executes daily but this my own section should execute once in a month.
Is there any way i can keep the if condition in powershell script, how it knows after month changes????
Need inputs.
$day = (get-date).day
$triggerday = 1 #if you want the script to execute on the 1st day of the month. "1" appears only once a month
if($triggerday -eq $day )
{
#Do something here
}
So in my Powershell script I am writing I need to code in a date that is 5 days in the past. I have this but I know it wont work with my formatting method I am using. I just need to figure out how to format it and make it 5 days prior for automation.
$StartTime = (Get-date).AddDays(-5)
$EndTime = Get-date -format "yyyy-MM-ddTHH:mm:ss.fffZ"
The end time has the formatting I want. But I cant apply the formatting to the start time after I perform the AddDays operation.
Thanks for any help. I've been racking my brain over this and I am sure I am just missing something simple.
$StartTime = (get-date).AddDays(-5).ToString("yyyy-MM-ddTHH:mm:ss.fffZ")
I am working with powershell that retrieves timesheet information from a biometric device. One of the search arguments used to query the timesheet is the date and time. Currently, I retrieve the date and time in the local machine where my powershell is installed. My code inside my powershell script for getting the date is:
get-date -format yyyy-MM-dd
However, if someone changes the date and time of the computer, my query gets affected. Is there a way to retrieve the current datestamp for Asia/Manila using powershell? I dont want to use local machine time anymore.
You can convert the date to universal time before formatting it as a string:
(Get-Date).ToUniversalTime().ToString('yyyy-MM-dd')
With the script from https://gallery.technet.microsoft.com/scriptcenter/Get-Network-NTP-Time-with-07b216ca you can get the time from a ntp server.
download the tool and start it like that:
.\Get-NtpTime -Server asia.pool.ntp.org | Format-List *
You can find other ntp pools oh this site http://www.pool.ntp.org/de/
So I am trying to get the date 3 months back using the Powershell command Get-Date. However, when I try to add multiple get-dates in one line it errors out or doesn't give me the results I'm looking for.
The End result I'm trying to get is $checkDate = 6-7-2016
I've tried this, but it doesn't really work:
$checkDate = (Get-Date).month -3 "-" (Get-Date).day "-" (Get-Date).year
Any ideas on how to accomplish this? I'm newer to Powershell and not exactly sure how to concatenate properly.
I'm using PS 4.0
What you are trying to do is format a calculated date. So let's start with calculating the date:
(Get-Date).AddMonths(-3)
That gets you today's date minus 3 months. Next you want to format it in a specific manner. That being Month-Day-Year. That can be done as such:
(Get-Date).AddMonths(-3).ToString("M-d-yyyy")
That results in:
6-7-2016
I've literally just C+P this code into my PowerShell, related to this question I asked on Super User.
It gives me exactly what I'm looking for but only for today's emails, can someone tell me how to add a a filter for emails the last two months for example? I looked through the code and I can't see anything related to data range so there might be another cmdlet that needs to be ran?
From a cursory glance at the script you linked, the individual emails appear to be iterated through in a foreach loop that uses the output of Get-MessageTrace as the collection of objects to iterate through. According to this documentation, Get-MessageTrace has a -StartDate and -EndDate parameter that you can specify a range of dates with.
So you just need to use those parameters to get a longer range of dates. Here's an untested example of what you'll probably need to do for the past two months:
Get-MessageTrace -StartDate (Get-Date).AddMonths(-2) -EndDate (Get-Date)
Edit: According to the parameter documentation, you may have to do some additional formatting after getting the date. I'm unfortunately not somewhere where I can test this, but here's what it says:
Use the short date format defined in the Regional Options settings for the computer on which the command is run. For example, if the computer is configured to use the short date format mm/dd/yyyy, enter 03/01/2010 to specify March 1, 2010. You can enter the date only, or you can enter the date and time of day. If you enter the date and time of day, you must enclose the argument in quotation marks ("), for example, "10/05/2010 5:00 PM".
This may not be necessary since you're already passing a DateTime object (the output of Get-Date) instead of a string... but worth mentioning, if you simply want to hard-code a string instead of getting the current time.