Powershell-查找下一个星期五 [英] Powershell - Find the next Friday

查看:98
本文介绍了Powershell-查找下一个星期五的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

希望这并不难,但是我正在编写一个脚本,用于查询域中的用户信息并导出为CSV。

Hoping this won't be too difficult but I am writing a script that queries user information from the domain and exports to CSV.

该脚本由我们的行政人员,他们只需输入用户名即可。

The script is run by our administrative staff and all they need to input into the script is the username.

棘手的是,我需要根据即将到来的星期五来命名CSV。

The tricky part is that I need the CSV to be named based on the coming Friday.

Note: I am in Australia so my date format is DD-MM-YYYY

我目前正在寻找的方式如下:

The way I am currently looking at going about it is as below:

# Grab the Script Run Timestamp
$ScriptRuntime = Get-Date -Day 4 -Month 5 -Year 2013

# Grab the next Friday (including today)
$NextFriday = $ScriptRuntime.AddDays(0 + $(0,1,2,3,4,5,6 -eq 5 - [int]$ScriptRuntime.dayofweek))

Write-Host "Script Run:  "$ScriptRuntime
Write-Host "Next Friday: "$NextFriday


    ,除星期六外的所有日期都可以正常工作。
  • 如果我将日期设为2013年第5个月第26天,则返回2013年5月31日

  • 如果我将日期设为2013年第5个月第27天,则返回返回2013年5月31日

  • 如果我将2013年5月28日作为一天,则返回2013年5月31日

  • 如果我将日期设为2013年5月29日,则返回31/05/2013

  • 如果我将日期设为2013年5月30日,则返回31/05 / 2013

  • 如果我将这一天作为2013年第5个月的第31天运行,则返回2013年5月31日

  • 如果我运行这一天作为2013年第1个月的第6天,则返回2013年5月31日(应为2013年6月6日)

  • 如果我将第2天作为第6个月的第2天,则返回2013它返回07/06/2013

  • If i run the day as Day 26, Month 5, Year 2013 it returns 31/05/2013
  • If i run the day as Day 27, Month 5, Year 2013 it returns 31/05/2013
  • If i run the day as Day 28, Month 5, Year 2013 it returns 31/05/2013
  • If i run the day as Day 29, Month 5, Year 2013 it returns 31/05/2013
  • If i run the day as Day 30, Month 5, Year 2013 it returns 31/05/2013
  • If i run the day as Day 31, Month 5, Year 2013 it returns 31/05/2013
  • If i run the day as Day 1, Month 6, Year 2013 it returns 31/05/2013 (this should be 07/06/2013)
  • If i run the day as Day 2, Month 6, Year 2013 it returns 07/06/2013

我在做什么错了?

推荐答案

我不确定我是否在关注你,但是下个星期五我可以得到:

I'm not sure I'm following you but here's I would get next Friday:

$date = Get-Date

for($i=1; $i -le 7; $i++)
{        
    if($date.AddDays($i).DayOfWeek -eq 'Friday')
    {
        $date.AddDays($i)
        break
    }
}

这篇关于Powershell-查找下一个星期五的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆