简体   繁体   English

有没有一种方法可以警告远程计算机等待重新启动,并且让我的脚本等待重新启动完成?

[英]Is there a way to warn remote computer of pending restart AND have my script wait for the restart to complete?

I'm looking to combine the "You will be logged off in x minutes" functionality of 'shutdown.exe", and the "-Wait -For PowerShell" functionality of the Restart-Computer cmdlet. 我希望结合使用“ shutdown.exe”的“您将在x分钟内注销”功能和Restart-Computer cmdlet的“ -Wait -For PowerShell”功能。

I have a script that requires a remote computer to restart. 我有一个脚本,需要远程计算机重新启动。 I'd like to warn the user of the remote computer that their computer will restart in 5 minutes. 我想警告远程计算机的用户,他们的计算机将在5分钟后重新启动。 I would also like my script to wait until the restart completes before continuing forward. 我还希望我的脚本能够等到重新启动完成之后再继续前进。

Shutdown.exe has a great warning system and delay built in, but no great way to have the script wait for the reboot to complete. Shutdown.exe具有出色的警告系统和内置的延迟功能,但没有很好的方法让脚本等待重新启动完成。 I've tried a loop that waits until Test-Connection is $true, but of course a computer returns a ping before it can accept PowerShell commands like Invoke-Command. 我尝试了一个循环,直到Test-Connection为$ true为止,但是当然,计算机在可以接受诸如Invoke-Command之类的PowerShell命令之前会返回ping。 I could slap a Start-Sleep on the end, but the time between ping and PowerShell accepting commands varies per computer. 我可以在最后一巴掌开始睡眠,但是ping和PowerShell接受命令之间的时间因计算机而异。

Restart-Computer has a great "wait for PowerShell" feature, but there's no way to warn the user of the remote computer that their computer is about to restart. Restart-Computer具有出色的“等待PowerShell”功能,但是无法警告远程计算机的用户其计算机即将重启。 I've tried running the commands below, but the restart-computer cmdlet will throw an error if there's already a shutdown in progress. 我已经尝试运行以下命令,但是如果已经在执行关机操作,则restart-computer cmdlet将引发错误。

shutdown /r /t 300 /m \\computer $system
restart-computer -computername $system -Force -Wait -For PowerShell -Timeout 300 -Delay 2

我不会说这是对您问题的完整答案,但是请使用Test-WSMan代替Test-NetConnection (仅测试ping / TCP,如您所提到的那样),它将测试PowerShell Remoting所基于的协议(请注意其他协议)可能,但是您可能只是在您的环境中使用了此功能)。

You can try just sending the message just before your call to Restart-Computer using the msg command: 您可以尝试使用msg命令在致电Restart-Computer之前发送消息:

msg * /SERVER:$system /TIME:300 "Computer will be restarted in 5 minutes."
restart-computer -computername $system -Force -Wait -For PowerShell -Delay 2

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

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