将服务器重启划分为每周一次的算法
Algorithm to divide server reboots to once a week
必须向我正在处理的脚本添加功能,该脚本将在 citrix 场中设置可变数量的服务器,然后将它们设置为每周仅重启一次。因此,如果有 2 个服务器,它会将它们设置为在周六和周日重新启动。如果有 7 个,则将经过一周。如果有 10 个,它将设置 2 在 3 天重启,1 在一周的其余时间重启。只是在寻找一个基本算法,说明我和同事如何不断思考它并提出似乎过于复杂的逻辑。
像这样?
$Servers = 1..10 |% {"Server$_"}
$Servers |
foreach {"$_ reboots on $([DayOfWeek]($i++%7))"}
Server1 reboots on Sunday
Server2 reboots on Monday
Server3 reboots on Tuesday
Server4 reboots on Wednesday
Server5 reboots on Thursday
Server6 reboots on Friday
Server7 reboots on Saturday
Server8 reboots on Sunday
Server9 reboots on Monday
Server10 reboots on Tuesday
试试这个:
#$computerlist = Get-Content myservers.txt
$computerlist = 1..11 | % { "Server$_" }
$day = 1
$computerlist | ForEach-Object {
#[System.DayOfWeek] is an enum which goes from 0 to 6, so we subtract 1 from day-value.
#Or you could use $day = 0 and if($day -eq 6) .... Depends on what you're going to do.
"$_ should restart on day $([System.DayOfWeek]($day-1))"
#Next day
if($day -eq 7) { $day = 1 } else { $day++ }
}
Server1 should restart on day Sunday
Server2 should restart on day Monday
Server3 should restart on day Tuesday
Server4 should restart on day Wednesday
Server5 should restart on day Thursday
Server6 should restart on day Friday
Server7 should restart on day Saturday
Server8 should restart on day Sunday
Server9 should restart on day Monday
Server10 should restart on day Tuesday
Server11 should restart on day Wednesday
必须向我正在处理的脚本添加功能,该脚本将在 citrix 场中设置可变数量的服务器,然后将它们设置为每周仅重启一次。因此,如果有 2 个服务器,它会将它们设置为在周六和周日重新启动。如果有 7 个,则将经过一周。如果有 10 个,它将设置 2 在 3 天重启,1 在一周的其余时间重启。只是在寻找一个基本算法,说明我和同事如何不断思考它并提出似乎过于复杂的逻辑。
像这样?
$Servers = 1..10 |% {"Server$_"}
$Servers |
foreach {"$_ reboots on $([DayOfWeek]($i++%7))"}
Server1 reboots on Sunday
Server2 reboots on Monday
Server3 reboots on Tuesday
Server4 reboots on Wednesday
Server5 reboots on Thursday
Server6 reboots on Friday
Server7 reboots on Saturday
Server8 reboots on Sunday
Server9 reboots on Monday
Server10 reboots on Tuesday
试试这个:
#$computerlist = Get-Content myservers.txt
$computerlist = 1..11 | % { "Server$_" }
$day = 1
$computerlist | ForEach-Object {
#[System.DayOfWeek] is an enum which goes from 0 to 6, so we subtract 1 from day-value.
#Or you could use $day = 0 and if($day -eq 6) .... Depends on what you're going to do.
"$_ should restart on day $([System.DayOfWeek]($day-1))"
#Next day
if($day -eq 7) { $day = 1 } else { $day++ }
}
Server1 should restart on day Sunday
Server2 should restart on day Monday
Server3 should restart on day Tuesday
Server4 should restart on day Wednesday
Server5 should restart on day Thursday
Server6 should restart on day Friday
Server7 should restart on day Saturday
Server8 should restart on day Sunday
Server9 should restart on day Monday
Server10 should restart on day Tuesday
Server11 should restart on day Wednesday