Windows – How to check that mstsc.exe has successfully set up a RDP connection

batch-filepowershellrdpscriptingwindows

We use mstsc.exe to establish RDP connections to other servers in batch files, which are then terminated after some tasks have been run. This basically works fine, but every now and then it takes mstsc.exe extraordinarily long to set up the connection. Is there a way to check / hint that mstsc has finished the set up of the link successfully and that the RDP connection can be used?

Best Answer

You can check if the RDP logon was sucessful by querying the security log of the target system. Logon events are ID 4624, RDP logons are type 10. Other logon types https://www.ultimatewindowssecurity.com/securitylog/encyclopedia/event.aspx?eventid=4624

$user2find = "santaClaus"
$target = "server1234"
Get-winevent -comp $target -FilterHashtable @{Logname='security'; ID=4624; starttime=(get-date).addMinutes(-5)} | where {$_.properties[8].value -eq 10 -and $_.properties[5].value -eq $user2find}
Related Topic