星期一午夜在Powershell [英] Midnight last Monday in Powershell
问题描述
我正在尝试一个脚本,返回今天,本周和本月收到的和从某些邮箱发送的电子邮件数量。我写了这个,感觉很好,直到我意识到我的逻辑错了,它返回了最后24小时(获取日期(附加(-1))和连续的连续24小时时间段(get-date.adddays( - 7))等等。
I'm trying to hack up a script that returns the amount of email received to and sent from certain mailboxes today, this week and this month. I wrote this and felt good about myself until I realised my logic was wrong and it was returning the last 24 hours (get-date.(adddays(-1)) and last 7 consecutive 24 hour periods (get-date.adddays(-7)) etc.
计算出来的这个月的第一天是不是很难,找到一个可以复制的片段
Figuring out the first day of the month to count from wasn't that hard, found a snippet to copy
$firstDayOfMonth = Get-Date ((("01/" + (Get-Date $today).Month).ToString() + "/" + ((Get-Date $today).Year).ToString() + " 00:00:00"))
以01开头,并将今天的月份和年份添加为字符串,然后追加午夜时间,将整个事物捆绑到system.time对象中,但我不能在一周内完成。
which starts with 01/ and adds today's month and years as string then appends midnight time and bundles the whole thing into a system.time object, but I can't do that with the week.
System.time对象有一个.DayOfWeek属性,我可以改变它,以确定我们上周一的几天,我有一个$星期一变量,上个星期一包含 ,如何修改该变量以引用最后的午夜星期一?
System.time objects have a .DayOfWeek property and I can switch that to figure out how many days we are from last Monday, and I have a $Monday variable that contains this time last Monday, how do I modify that variable to refer to midnight last Monday?
推荐答案
本月第一个星期一
First Monday of the Month
do {
$n++
$date = (date -Hour 0 -Minute 0 -Second 0).AddDays(-$n)
}
Until ( `
$date.DayOfWeek -eq "Monday" -and `
$date.Day -le 7 `
)
本周第一个星期一
First Monday of the Week
$n = 0
do {
$date = (date -Hour 0 -Minute 0 -Second 0).AddDays(-$n)
$n++
}
Until ( $date.DayOfWeek -eq "Monday" )
这篇关于星期一午夜在Powershell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!