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

Algorithm 将服务器重新启动划分为每周一次的算法,algorithm,powershell,Algorithm,Powershell,我必须向正在编写的脚本中添加功能,该脚本将在citrix服务器场中设置可变数量的服务器,然后将它们设置为每周仅重新启动一次。因此,如果有两台服务器,它会将它们设置为在周六和周日重新启动。如果有7个,这将贯穿整个星期。如果有10个,它将设置2在其中3天重新启动,1在本周剩余时间重新启动。我和一位同事一直在反复思考这个问题,并提出似乎太复杂的逻辑,只是想找到一个基本的算法 $Servers = 1..10 |% {"Server$_"} $Servers | foreach {"$_ reboot

我必须向正在编写的脚本中添加功能,该脚本将在citrix服务器场中设置可变数量的服务器,然后将它们设置为每周仅重新启动一次。因此,如果有两台服务器,它会将它们设置为在周六和周日重新启动。如果有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
像这样

$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
试试这个:

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

是否希望每周重新启动每台服务器一次,并确保每天的最大重新启动总数尽可能少?是否希望每周重新启动每台服务器一次,并确保每天的最大重新启动总数尽可能少?