Windows

重新啟動主機後重新連接遠端桌面連接

  • June 9, 2015

我經常發現自己使用遠端桌面連接到工作站或伺服器以執行需要重新啟動的任務。在這些情況下,我通常需要在主機重新聯機後重新連接,以確保一切按計劃進行或繼續我的工作。在這些情況下,我通常會在命令提示符下啟動“ping -t”,讓我知道何時可以重新連接。

但是,我偶爾會在等待主機重新上線時因其他事情而分心,而忘記了回來。當主機重新聯機並允許我重新連接(理想情況下只需點擊一下)時收到警報會非常好。

有誰知道一個簡單的方法來完成這個?我認為必須有一個免費的實用程序可用,或者可以使用 PowerShell 腳本來完成。

我每天使用的快速而骯髒的 Powershell 腳本:

<#
.Synopsis
Checks for and connects to RDP on a given server.
.Description
This script tests for an open port 3389 (standard Windows RDP), and if it finds it, passes the IP to mstsc.exe. Otherwise it retries indefinitely. Ctrl+C will halt the script.
.Parameter ip
IP or FQDN of a Windows machine to test. Only takes one argument.
.Parameter wait
Will assume that the machine is still up, wait until it stops responding (even once), and then try to connect. Good for machines that are about to reboot.
.Parameter Verbose
Will print a line each time it tries unsuccessfully.
.Example
rdpupyet.ps1 ITG-SRV-INF01 -Verbose
#>
[CmdletBinding()]
param($ip, [switch]$wait)

function Get-DownYet ($ip) {
   Write-Verbose "Waiting for $IP to shut down."
   do {
       try {$up = New-Object System.Net.Sockets.TCPClient -ArgumentList $ip,3389}
       catch {$up = $false}
   }
   until ($up -eq $false)
   Write-Verbose "$IP no longer responding."
}

Write-Verbose "Testing RDP Connection... Ctrl+C to quit."

if ($wait) {Get-DownYet $ip}
do {
   try {$success = New-Object System.Net.Sockets.TCPClient -ArgumentList $ip,3389}
   catch {Write-Verbose "RDP not active. Retrying."}
}
while (!$success)

mstsc.exe /v:$ip /admin

恐怕不會超時。但這可以很容易地添加,以及成功響應的快速嗶聲。

我更滿意的一點(取自網際網路上目前不記得的來源)是,當伺服器響應 ping 時,它不會嘗試連接 - 相反,只有當標準 RDP 埠可訪問時。

引用自:https://serverfault.com/questions/276872