我正在写一个脚本,一次重启多台计算机。
我试图让它成为类似人类的行为,一旦计算机重新启动,它将ping并检查计算机是否会回来,但我希望它能够持续ping回没有出现10分钟的计算机最后,如果没有回应,它说这台电脑出了问题而且没有回来。
如果你看到这一行,我再次导入计算机以重新启动后测试连接是否可能不这样做并且仅测试连接重启的计算机。谢谢。
Start-Sleep -Seconds 120
$coms=GC c:\restarted.txt
完整的脚本。
$computers=GC c:\list.txt
ForEach ($computer in $computers)
{if((Test-Connection -Cn $computer -BufferSize 16 -Count 1 -ea 0 -quiet)){
Try {
Restart-computer -ComputerName $computer -force -ErrorAction stop
Write-Host "Restarting $computer" -f green
$($computer)|Out-File c:\restarted.txt -append
}
Catch {
[system.exception]
Write-Warning "Failed to restart $($computer) `n$error[0]"
}
}
else {
Write-Host "cannot reach $computer and cannot restart" -BackgroundColor red
}
}
## wait for 2 Minutes to check if the computer is coming back!
Start-Sleep -Seconds 120
$coms=GC c:\restarted.txt
Start-Sleep -Seconds 1
ForEach ($com in $coms)
{if(!(Test-Connection -Cn $com -BufferSize 16 -Count 1 -ea 0 -quiet))
{write-host "$com still booting" -f red}
else {
Write-Host "$com is coming back !" -f Green
}
}
答案 0 :(得分:0)
我希望它不断回击那些没有出现的电脑 10分钟,最后如果没有回应它说,有些事情发生了 这台电脑出了问题但没有回来
我重新导入计算机以重新启动后测试连接 有可能不这样做,并测试连接只有那些计算机 哪些是重新启动
[Array]
$computers = GC c:\list.txt
ForEach ($computer in $computers){
if((Test-Connection -Cn $computer -BufferSize 16 -Count 1 -ea 0 -quiet)){
Try{
Restart-computer -ComputerName $computer -force -ErrorAction stop
Write-Host "Restarting $computer" -f green
$computer | Out-File c:\restarted.txt -append
[array]$restartedComputers += $computer # store restarted computers is variable here
}Catch [system.exception] { # typo: type should come before opening curly brace
Write-Warning "Failed to restart $($computer) `n$error[0]"
}
}else{
Write-Host "cannot reach $computer and cannot restart" -BackgroundColor red
}
}
## wait for 2 Minutes to check if the computer is coming back!
Start-Sleep -Seconds 120
ForEach ($com in $restartedComputers){
# set the end time as ten minutes from now
$endTime = (Get-Date).AddMinutes(10)
# for 10 minutes, try to ping.
do{
if(!(Test-Connection -Cn $com -BufferSize 16 -Count 1 -ea 0 -quiet)){
# if it fails, set $online to false and wait 30 seconds.
Write-Host "$com still booting" -f red
$online = $false
Start-Sleep -s 30
}else{
# if it works, set $online to true and exit from this do-while loop
Write-Host "$com is coming back !" -f Green
$online = $true
return;
}
}while((Get-Date) -lt $endTime)
# check whether the computer was contacted in the end.
if(-not $online){
# tried to ping for 10minutes and failed: do stuff.
}
# move on to next computer.
}