简体   繁体   English

同时在所有主机上重新启动计算机

[英]restart computer on all hosts at the same time

I have an idea about restarting all of host/VMs in my inventory. 我有一个关于重新启动清单中所有主机/ VM的想法。 I created some short script which restarting one by one of hosts. 我创建了一些简短的脚本,可以一台主机重新启动。 But it's take to long time. 但这需要很长时间。 How can i do that all hosts to be restarted at the same time? 我该如何同时重启所有主机?

My code: 我的代码:

foreach ($host in $hosts) {
    Restart-Computer -ComputerName $host -Wait
    Write-Host "$host restarted "
}

Restart-Computer will accept multiple entries for ComputerName so you can perform the restart on more than one host at a time. Restart-Computer将接受多个条目ComputerName ,所以你可以一次多台主机上执行重新启动。

Using this you can initiate the restart in batches, say three at a time, this will significantly decrease the time it takes without adding too much load on the VM Cluster/Host. 使用此功能,您可以分批启动重新启动,例如一次启动三个,这将显着减少所需的时间,而不会在VM群集/主机上增加过多的负载。

$hosts = @("server01","server02","server03","server04","server05","server06","server07","server08","server09","server10")

[int]$batches = 3 #number of computers to restart in each 'batch'
[int]$skip = 0

do {
    $selected_hosts = $hosts | Select-Object -First $batches -Skip $skip

    Restart-Computer -ComputerName $selected_hosts -Wait -WhatIf
    Write-Host "$selected_hosts restarted"

    $skip = $skip + $batches
}
while ($selected_hosts)

Note: Remove -WhatIf when you're ready to run the script live, with this in place Restart-Computer will just write to Console informing you what it would do and not actually performing the restart. 注意:删除-WhatIf如果您准备好实时运行脚本,则在适当的位置执行此操作, Restart-Computer将仅写入控制台,告知您它将执行的操作,而不实际执行重新启动。

is it a good idea ?? 这是不是一个好主意 ?? - Restart-Computer -asJob Or maybe are better solutions ?? -Restart-Computer -asJob还是更好的解决方案?

$j = Restart-Computer -ComputerName "Server01", "Server02" -AsJob
PS C:\> $Results = $j | Receive-Job
PS C:\> $Results

Can you try WMI to reboot the servers. 您可以尝试WMI重新启动服务器吗? I found one such method on social.technet.microsoft Forum. 我在social.technet.microsoft论坛上找到了一种这样的方法。

$server = get-content c:\Servers.txt

(gwmi -Class Win32_OperatingSystem -ComputerName $server).Win32Shutdown(6)
If ($?) {
Write-Host "$server successfully rebooted"
}Else{
Write-Host "Could not reboot $server"
}

You can find other methods also like using a batch file. 您还可以找到其他方法,例如使用批处理文件。 Check out this link. 查看链接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM