powershell - Get previous month's last day and if its not Saturday then get all the dates till saturday (incl Sat) -
- get last day of previous month.
- if not saturday (of week) dates last day (whatever dayofweek is) till saturday of week (in yyyymmdd format)
for example:
if running script on 7th , 14th of september 2016 script should first find last day of previous month 20160831 (wednesday).
now should me dates last day till saturday of week (including saturday)
20160901 20160902 20160903
i store each of dates in different variables used later in script.
i have figured how last day of previous month (in right format) cant figure out how dates day till saturday.
this script needs dynamic schedule run every month.
param ([system.datetime] $date = $(get-date)) $numdays = $date.day $lastday = $date.adddays(-$numdays) $lastday = $date.adddays(-$numdays).dayofweek
# first day of month @ midnight, , step day last day of previous month ${date stepper} = (get-date -day 1 -hour 0 -minute 0 -second 0 -millisecond 0).adddays(-1) # step forward, 1 day @ time, output last day of month until following saturday $counter = 1 { ${date stepper} = ${date stepper}.adddays(1) set-item -path "variable:var$counter" -value ${date stepper}.tostring("yyyymmdd") $counter++ } while ([system.dayofweek]::saturday -gt ${date stepper}.dayofweek) # test outputs write-host "var1 = $var1" write-host "var2 = $var2" write-host "var3 = $var3" write-host "var4 = $var4" write-host "var5 = $var5" write-host "var6 = $var6" write-host "var7 = $var7"
Comments
Post a Comment