将服务器重新启动分成每周一次的算法

时间:2015-01-22 16:04:02

标签: algorithm powershell

必须为我正在处理的脚本添加功能,这将在citrix服务器场中设置可变数量的服务器,然后将它们设置为每周仅重启一次。因此,如果有2台服务器,它将在星期六和星期日将它们设置为重新启动。如果有7个,它将经历一周。如果有10个,它将设置2以在3天内重新启动,1个星期的剩余时间。只是在寻找一个基本的算法,就像我和同事一样继续思考它并提出看起来太复杂的逻辑。

2 个答案:

答案 0 :(得分:3)

喜欢这个吗?

$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

答案 1 :(得分:2)

试试这个:

#$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