我的代码的目的是在瘦客户机上运行RDP连接(在自定义shell中)。每当取消RDP登录或终止RDP连接时,登录窗口都会再次出现。这将始终向用户显示其登录屏幕。
另外,因为RD代理维护15分钟的握手超时,所以我希望在会话尚未打开时每15分钟运行一次刷新。因此,我必须找到某种方法来检测用户是否没有登录到RD主机。
脚本运行得很好。尽管总是对更聪明的运行方式感兴趣。
Start-Process C:\Windows\System32\mstsc.exe C:\Connection\Terminal-Server.RDP
$previous_Handles = 0
While(1) {
$process = Get-Process mstsc
$timer = New-Object System.Threading.Timer
$timer.Interval = 900000
$timer.Enabled = $true
$timer.Autoreset = $true
Register-ObjectEvent -InputObject $timer -EventName Elapsed -SourceIdentifier TimerElapsed -Action {Stop-Process -Id $process.Id}
if((($process.Handles -le 700) -and ($previous_Handles -ge 700) -or ($process -eq $null)))
{
Stop-Process -Id $process.Id
$previous_Handles = 0
$Timer.Stop()
Unregister-Event -Force -SourceIdentifier TimerElapsed
Start-Process C:\Windows\System32\mstsc.exe C:\Connection\Terminal-Server.RDP
}
elseif($process.Handles -ge 925) {
$process = Get-Process mstsc
$previous_Handles = $process.Handles
$Timer.Stop()
Start-Sleep -m 500
Unregister-Event -Force -SourceIdentifier TimerElapsed
}
else {
Start-Sleep -m 500
$previous_Handles = $process.Handles
$Timer.Start()
}
}发布于 2015-09-10 06:55:44
此脚本将在发现进程关闭一秒后打开RDP窗口,并将继续循环,直到PowerShell进程停止。
while($true)
{
$RDPprocess = Start-Process mstsc -ArgumentList path-to.rdp -PassThru
do {Start-Sleep -Seconds 1}
until($RDPprocess.HasExited)
}https://stackoverflow.com/questions/32438907
复制相似问题