希望这不会太困难,但我正在编写一个脚本,从域中查询用户信息并导出到CSV。
该脚本由我们的管理人员运行,他们输入脚本所需的只是用户名。
棘手的部分是我需要根据即将到来的星期五命名CSV。
Note: I am in Australia so my date format is DD-MM-YYYY
我目前正在考虑的方式如下:
# 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
除了星期六之外的所有日子都可以。
我做错了什么?
答案 0 :(得分:7)
这会在下周五找到:
$date = Get-Date
while ($Date.DayOfWeek -ne "Friday") {$date = $date.AddDays(1)}
答案 1 :(得分:5)
我不确定我是否关注了你,但下周五我会得到:
$date = Get-Date
for($i=1; $i -le 7; $i++)
{
if($date.AddDays($i).DayOfWeek -eq 'Friday')
{
$date.AddDays($i)
break
}
}
答案 2 :(得分:4)
这是一个单行,也可以做到这一点:
$Date = @(@(0..7) | % {$(Get-Date).AddDays($_)} | ? {$_.DayOfWeek -ieq "Friday"})[0]
如果您需要特定时间:
$Date = @(@(0..7) | % {$(Get-Date "12:00").AddDays($_)} | ? {($_ -gt $(Get-Date)) -and ($_.DayOfWeek -ieq "Friday")})[0]
答案 3 :(得分:2)
您可以使用以下内容获取本周五的日期,无论您运行代码的日期如何:
$today = (Get-Date).Date
$nextfriday = $today.AddDays([int][dayofweek]::Friday - $today.DayOfWeek)
如果你想要在下周五(如果你在星期六跑步,那么下周五周五),你最后需要一个额外的修改器:
if ($today -ge $nextfriday) {$nextfriday = $nextfriday.AddDays(7)}
答案 4 :(得分:1)
请尝试以下方式查找下周五:
$ScriptRuntime = Get-Date -Day 4 -Month 5 -Year 2013
$i= switch ( [int][dayofweek]::Friday - [int] $ScriptRuntime.dayofweek )
{
-1 { 6 }
default { $_ }
}
$ScriptRuntime.adddays($i)
答案 5 :(得分:1)
参加聚会有点晚,但这是我的看法(无循环,无条件):
[datetime] $MyDate = get-date
[DayOfWeek] $NextDayOfWeek = 'Friday'
$MyDate.AddDays( (7 - [int] $MyDate.DayOfWeek + [int] $NextDayOfWeek )%7 )
如果要验证更多日期,请执行以下操作:
[datetime] $MyDate = get-date
[DayOfWeek] $NextDayOfWeek = 'Friday'
1..30 | ForEach-Object {
[PSCustomObject]@{
DayOfWeek = $MyDate.DayOfWeek ;
Date = $MyDate;
$("Next$NextDayOfWeek") = $MyDate.AddDays((7 - [int] $MyDate.DayOfWeek + [int] $NextDayOfWeek )%7)
};
$MyDate = $MyDate.AddDays(1)
}
答案 6 :(得分:0)
我会这样做:
$Date = Get-Date
$targetDay = [int][dayofweek]::Friday
# I use this logic to figure out the next Friday
# else we would get Friday of this week
$intToday = if([int]$Date.DayOfWeek -le [int][dayofweek]::Friday ){
[int]$Date.DayOfWeek
} else {
[int]$Date.DayOfWeek - 7
}
$nextFriday = $date.AddDays($targetDay - $intToday)
我遇到了同样的问题,但我认为使用循环有点太多了。
我发现了一篇关于PowerShell Find Date of a previous day in any week的文章,但是对于日期进行硬编码的值,根据您的Windows环境,星期日可能不是索引0
(零)。所以,我用逻辑作为灵感,并想出了上面的那个。
希望它有助于下一个人。
答案 7 :(得分:0)
我将Shay的示例放入扩展函数中,因此您还可以定位所需日期的确切时间。
Function Find-NextDate {
[CmdletBinding()]
param (
# Parameter help description
[Parameter(Mandatory=$true,
Position=1)]
[validatenotnullorempty()]
[ValidateSet("Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday")]
[string]$Day,
[Parameter(Mandatory=$false,
Position=2)]
[validatenotnullorempty()]
[string]$Time
)
process {
if ($Time) {
$Date = (Get-Date $Time)
for($i=1; $i -le 7; $i++) {
if ($Date.AddDays($i).DayOfWeek -eq "$Day") {
$Date.AddDays($i)
break
}
}
}
else {
$Date = (Get-Date)
for($i=1; $i -le 7; $i++) {
if ($Date.AddDays($i).DayOfWeek -eq "$Day") {
$Date.AddDays($i)
break
}
}
}
}
}
答案 8 :(得分:-1)