Created
March 7, 2016 05:39
-
-
Save Windos/785b7cb18d63cac6d98b to your computer and use it in GitHub Desktop.
Simple logic to emulate running a script monthly.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Set powershell job to run Weekly, on on the desired day. | |
# Wrap the code the job is running with the below so that it will only run | |
# when intended. | |
# e.g. Last Wednesday of month: | |
# | |
# $DayToRun = 'Wednesday' | |
# Occurance = -1 | |
# | |
# The scheduled job still runs once a week, but will only execute your code | |
# if the date is validated. | |
$Now = Get-Date | |
$DayToRun = 'Monday' | |
$OccuranceInMonth = 1 # It's an array so 1 = 2nd Occurance | |
$PossibleDays = @() | |
0..31 | ForEach-Object -Process { | |
$TestDate = (Get-Date -Year $Now.Year -Month $Now.Month -Day 1).AddDays($_) | |
if ($TestDate.Month -eq $Now.Month) | |
{ | |
if ($TestDate.DayOfWeek -eq $DayToRun) { | |
$PossibleDays += $TestDate.Day | |
} | |
} | |
} | |
$ValidRunDate = Get-Date -Year $Now.Year -Month $Now.Month -Day $PossibleDays[$OccuranceInMonth] | |
if ($Now.Date -eq $ValidRunDate.Date) | |
{ | |
# Put code here | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment