PowerShell script with specific date/time range - datetime

I am trying to run a powershell script for exchange 2010 to pull information from a specific date/time range.
example: show me amount of received emails from monday-friday time range 6am-11pm
get-messagetrackinglog -resultsize unlimited -Recipient TEST#TEST.COM -Server EXCHANGE -Start "3/4/2013 6:00:00 AM" -End "3/6/2013 23:00:00 PM" | select messageid -unique | measure
but I would like to make the date range not so static. so If i run the script at 11pm on Friday night, every week, how can i get it to do this query for the last 5 days.
I was trying adding in (get-date).adddays(-5) but I can't figure out how to add that in.
any help will be greatly appreciated.

Try this
for($i=-4,$i -lt 0,$i++){
$start = (get-date -hour 6).adddays($i);
$end = (get-date -hour 23).adddays($i);
Write-host $start.DayoftheWeek (get-messagetrackinglog -resultsize unlimited -Recipient TEST#TEST.COM -Server EXCHANGE -Start $start -End $end | select messageid -unique | measure).count
}

The get-messagetrackinglog cmdlet only takes a single argument for -start and -end, so you can't specify that in a single command.
You can run 5 separate queries of the messagetracking logs from 6AM-11PM, each on different days and aggregate those results together, or you can do one query for all the logs from 6AM on the first day to 11PM on the last day, then filter out the ones that are timestamped between 11PM and 6AM in the interim days.

Just trying to answer the part about the past date and hour and assuming you are running this at 11pm but want to go back to 6am (17hrs diff)...
Maybe use something like:
$past=(Get-Date).adddays(-5).addhours(-17)
And then try
-start $past -end (get-date)

Related

Cron job run at the wrong time in AIX 7.1

I had configured my cronjob to run in every first Monday of the month 8:40am as below
40 08 1-7 * 1 /fs/test/testtime.sh
But it not only run on Monday, it also run on today which is Tuesday.
Is there anything i miss out?
From the man page for crontab (my emphasis):
Note: The day of a command's execution can be specified by two fields - day of month, and day of week. If both fields are restricted (i.e., aren't *), the command will be run when either field matches the current time.
For example, 30 4 1,15 * 5 would cause a command to be run at 4:30 am on the 1st and 15th of each month, plus every Friday.
So, in your case, the job runs on every one of the first seven days in each month, plus every Monday.
You can do what you wish by adding an AND condition in the command rather than relying on an OR condition in the time specification, something like:
40 08 1-7 * * test $(date +\%u) -eq 1 && /fs/test/testtime.sh
This will run the actual cron job on all those days (first seven days in each month) but the payload (the script) will only run if the day is Monday.

batch file Subtracting hour current time on Windows

I need to subtract, for example, 1 hour from current time in a .bat file on Windows 7.
I do it like this.
set day=%date:~0,2%
set month=%date:~-7,2%
set year=%date:~-4,4%
:: ———————————————————————–
set hour=%time:~0,2%
set /A hour= hour - 1
if %hour% lss 0 set hour=23
if %hour% lss 10 set hour=0%hour%
echo %year%-%month%-%day% %hour%:00
But the problem, it is get consistence when the subtracting results a day before on a month before. For example, this date, 2016-03-01 00:05 I get 2016-03-01 23:05. I need to get 2016-02-29 23:05
I found several batch script to subtract one day, but nothing with hours or minutes.
It's not completely Batch, but I find that there are so many exceptions and special cases, that it's not worth it re-coding everything in Batch, especially when it's been done before. I like to just invoke Powershell from a batch file, like this.
for /f "delims=" %%a in ('"powershell [DateTime]::Now.AddHours(-1).ToString('yyyy-MM-dd HH:mm')"') do echo %%a
Output will be something like: 2016-10-18 02:05
Batch files are not good in date/time operations. Spend much time with similar issues, I can recommend to use external tool such (as window port of unix 'date' command for example) and perform any operations on timestamps.

How to use ubuntu command line to find a holiday?

How to use ubuntu command to find the date of Mother's Day (Second Sunday of May) in 2017?
Since -v doesn't work on ubuntu, it works on Mac.
Lets say its usually the second sunday of may :
fhenri#machine:~$ date -v1d -v5m -v+1y -v+2w -v-sun
Dim 14 mai 2017 22:56:28 CEST
you should look at man date to get more examples and to find out if you need another particular sunday or fix day
I was running on my macos (freeBSD based) and did not realized date was not uniform across *nix system.
on ubuntu, it revealed a bit more difficult but here how I would go
vagrant#ubuntu:~$ firstofmay=$(date -d '05/01/2017')
vagrant#ubuntu:~$ firstsunday=$(date -d "$firstofmay" '+%Y-%m')-$(( 8 - $(date -d "$firstofmay" '+%u') ))
vagrant#ubuntu:~$ secondsunday=$(date -d "$firstsunday + 1 week" '+%Y-%m-%d')
vagrant#ubuntu:~$ echo $secondsunday
2017-05-14
first I initialize the 1st of may as my base date
then I get a format of the first sunday from this month: day are 7 based and $(date -d "$firstofmonth" '+%u') will be the day number of the first of may (i.e.: monday is 1); this gives a format such as YYYY-MM-dd
adding 1 week to this date I have previously gives the correct date - adjust the formatting if you want it different

Create a batch file to load a webpage when the date and time are correct

I am trying to get a webpage to load when the date and time are correct. The code I have come up with is:
#echo off
:repeat
set CurrentTime=%time:~0,2%.%time:~3,2%
set CurrentDate=%date:/=-%
echo.
echo %CurrentDate% %CurrentTime%
echo.
IF %CurrentTime% == Tue 08-04-2014 13.04 goto load
timeout /t 1 >nul
goto repeat
:load
start/MAX iexplore.exe"" "http://www.youtube.com.au"
timeout /t 6 >nul
It will work if I remove the CurrentDate and the date from the IF statement but it won't if I don't. I do need the date and time to work.
Thanks.
IF "%date% %Time%"=="Tue 08-04-2014 13.04" goto load
shoud work for you. You need to use the "quotes" to group the string as a single entity, otherwise IF has no way of telling whether the == is a part of the string to be compared or it's the comparison operator. (and that assumes that your date format is Tue 08-04-2014 and time is 13.04)
Aditionally to the solution that Magoo has posted, note that %DATE% returns the current date using the short date format that is fully and endlessly customizable by the users. One user may configure its system to return Tue 08/04/2014 and another user may choose Apr8th14. It's a complete nightmare for a BAT programmer. Even in the same computer. This is also true for the ~t modifier when expanding a variable containing a filename.
There are several solutions, google a bit to find them. The easiest in my opinion is to use WMIC
WMIC Path Win32_LocalTime Get Day,Hour,Minute,Month,Second,Year /Format:table
returns the date in a convenient way to directly parse with a FOR command.
FOR /F "skip=1 tokens=1-6" %%A IN ('WMIC Path Win32_LocalTime Get Day^,Hour^,Minute^,Month^,Second^,Year /Format:table') DO (
SET /A currentdate=%%F*10000+%%D*100+%%A
)
IF "%currentdate"=="20140408" goto load
You can also use a scheduled task if that suits the reason for you wanting to load the page.

Schedule Task : 6 months from now

I'm tryin' to find a way (from a batch file) that I can use to create a scheduled task that will execute 6 months from now. I've looked all over the net, and I'll I've come across is AT and SCHTASKS that will schedule the task monthly ..
I'm looking for a solution that will execute this task every 6 months.
I know this will require some time/date manipulation .. finding the month (number of month) and then adding 6 to it; once it passes '12' it goes back to 1 etc .. Then lining those numbers up with the months name, and using SCHTASKS or AT to schedule the task.
I know how to use SCHTASKS to schedule a task from a batch file; I just need the code to find the month, add 6 to it, grab the months name in 6 months time, and put that into a variable so I can parse it into the SCHTASKS.
I hope this all makes sense.
My english is not so great.
TIA,
Greg
Thanks to everyone who replied, and offered suggestions. I slept on it, woke up with the flu, and had a brainwave. Funny how things work out. I know this is overkill, and someone will come up with a better suggestion, but here goes anyway ..
REM Grab month number and put into variable
FOR /F "TOKENS=1,2 eol=/ DELIMS=/ " %%A IN ('DATE/T') DO SET mm=%%B
REM Six months from now
set /a addmm=%mm% + 6
if %addmm% gtr 6 (set /a sixmonths=%addmm% - 12)
REM Determine month name
if %sixmonths%==1 (set monthname=JAN)
if %sixmonths%==2 (set monthname=FEB)
if %sixmonths%==3 (set monthname=MAR)
if %sixmonths%==4 (set monthname=APR)
if %sixmonths%==5 (set monthname=MAY)
if %sixmonths%==6 (set monthname=JUN)
if %sixmonths%==7 (set monthname=JUL)
if %sixmonths%==8 (set monthname=AUG)
if %sixmonths%==9 (set monthname=SEP)
if %sixmonths%==10 (set monthname=OCT)
if %sixmonths%==11 (set monthname=NOV)
if %sixmonths%==12 (set monthname=DEC)
REM Schedule Task
schtasks /create /TN TuneUpReminder /RU system /TR TuneUpReminder.bat /SC MONTHLY /M %monthname%
Why not use the Windows Scheduler?
Most of the information you need is included in this article: http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/schtasks.mspx?mfr=true
You can use the monthly setting, with a value of 6 surely?
Here's a crazy idea: in case you want this script on a server (which isn't rebooted), you could write a batch file which waits for six months and then executes the given program. To wait for a specific number of seconds you could abuse the 'ping' command, like this:
ping -n %SECS% localhost > NUL
This command will effectively pause for %SECS% seconds and then return. To wait for six months, simply wait for something like (365 / 2) * 24 * 60 * 60 seconds.
Here's a little batch file which implements this idea:
#echo off
set CMD=echo Half a year elapsed
set /a SECS_IN_HALF_A_YEAR=365 / 2 * 24 * 60 * 60
loop_start:
ping -n %SECS_IN_HALF_A_YEAR% localhost > NUL
%CMD%
goto loop_start
I'm not saying it's pretty, but I thought it's a funky idea. Maybe some food for thought. :-)
you can download coreutils for windows . Then use date command like this
C:\test>gnu_date "+%Y%m%d" -d "6 months"
20110404
(it is renamed to gnu_date.exe )
I have not played with schtasks, but depending on what format of date it uses, you can change the parameters to suit schtasks
C:\test>gnu_date "+%Y-%m-%d" -d "6 months"
2011-04-04
If you need time as well
C:\test>gnu_date "+%Y-%m-%d-%H:%M:%S" -d "6 months"
2011-04-04-18:12:35
Use a for loop to save the date to a variable as desired. then pass it to schtasks for scheduling
Since Greg's answer made me cry inside, here is some array like syntax:
for /F "tokens=%sixmonths%" %%A IN ("JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC") DO set monthname=%%A
And since there’s always more than one way to skin a bat[ch]?
set /a sixmonths=%sixmonths% * 4
set months=666 JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC
setlocal ENABLEDELAYEDEXPANSION
set monthname=!months:~%sixmonths%,3!
REM Optional: setlocal DISABLEDELAYEDEXPANSION

Resources