I am looking to write a script to test a server to see if the RDP port is available. Is there a way to do so?
First you want to make sure the computer is Pingable. Here is a "Pingable" snipit (https://snipt.net/dvsit/fastpingable/). Once you know it is pingable then test the port.
But check the registry if you can: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Terminal Server value fDenyTSConnection
can you explain why try to ping at first, please?
Ping is a command sent on ICMP protocol which is by default disabled on most corporate networks because of secutiry reasons (at least at incoming nodes, ping amplification attacks), and AFAIK blocked by firewall on Windows 7 by default. The RDP service is listening on TCP 3389 by default. Of course trying to connect to the service and recieving logon error or connection is the best test but it should be ok to test if the port is listening.
the process of testing a port over network is documented here:
For local (or remote powershell) the simplest way is probably WMI documented in link to the Technet, you have posted.
Yo dude, there certainly is a way to do this, and the script is already written for you.
It pings, it checks the RDP port (3389) and any other ports you want to check, and it even beeps at you when it is successful.
Just follow this link:
Another one I like is this one by Justin Dearing, Autoconnect RDP:
This one first pings, then automatically open an mstsc session once RDP is available.
Nice! My wish-list for this one would be to add the capability to pass security credentials so you don't have to key in domain\user & password. I don't think that is supported and no longer allowed to be passed in clear-text using an .rdp connection file (which makes sense).
What I'm thinking would be cool is if there was a capability to pass an encrypted credentials file (e.g. http://poshtips.com/2011/03/04/import-and-export-user-credentials-in-powershell/) to speed up the logon process while somewhat maintaining security. Again, I don't think this would be supported by MSTSC but it might be possible via an API.
Yea, when you said it I thought that might be useful. I looked for some methods to be able to facilitate this and wrote a short function that provides this functionality.
I posted it on Technet, have a look at it there:
Connect-Mstsc - Open RDP Session with credentials
Let me know what you think of it.
Very cool and nice work-around there using cmdkey! I Like It!
My next thought is that I still need to securely store user credentials so I can feed them into your function to be used by cmdkey. I suppose that this could be done using the script I referenced earlier. e.g. pull the secured credentials from an encoded xml file, feed that into the connect-mstsc function and be on our merry way.