3

I have bunch of servers and I want to check if I can connect to them with RDP. I have 2000 servers so I wanted to automate it.

I'm not very familiar with PowerShell, here is what I have:

list.txt:

ip1
ip2
ip3
...
ipn

Here is my code. I loop on each ips, connect, check if the connection was successfull and try to close it.

Get-Content C:\Users\MyUser\Documents\computers2.txt |
ForEach-Object{
    cmdkey /generic:TERMSRV/$_ /user:MyUser /pass:MyPassWord
    mstsc /v:$_
    Start-Sleep 90
    $app = Get-Process -processname "$_*"
    if (Get-winevent -comp $_ -FilterHashtable @{Logname='security'; ID=4624; starttime=(get-date).addMinutes(-10)} | where {$_.properties[8].value -eq 10 -and $_.properties[5].value -eq 'MyUser'}) {
        "$_" >> C:\Users\MyUser\Documents\valid.txt
    }
    $app.Kill()
}

Remote Desktop Connection opens and connect. The if statements works too. But I cannot manage to kill my fresh Remote Desktop Connection nammed " - Remote Desktop Connection". It seems like $app is empty.

I tried also:

Stop-Process -processname "$_*"

EDIT

I do not want to check if the remote machine has RDP on (check port with Test-NetConnection -Port 53 -ComputerName $_ ), but if a specific user has acces to the remote server.

Workaround

Get-Content C:\Users\MyUser\Documents\computers2.txt |
ForEach-Object{
    cmdkey /generic:TERMSRV/$_ /user:MyUser /pass:MyPassWord
    mstsc /v:$_
    Write-Host "Sleeping for 90 sec"
    Start-Sleep 90
    if (Get-winevent -comp $_ -FilterHashtable @{Logname='security'; ID=4624; starttime=(get-date).addMinutes(-10)} | where {$_.properties[8].value -eq 10 -and $_.properties[5].value -eq 'MyUser'}) {
        "$_" >> C:\Users\MyUser\Documents\result.txt
    }
    Get-Process | Where-Object { $_.Name -eq "mstsc" } | Select-Object -First 1 | Stop-Process
}

This work if you are sure to only have one RDP connection on the machine you are working on. For me i'm connected in RDP to that machine... So it will have 2 mstsc process running. For the moment it never killed my session, only the newer ones. But if someone else go in RDP on the machine, it could break everything.

BeGreen
  • 765
  • 1
  • 13
  • 39
  • Hi, nice question, actually, I was taking look over a little bit, and I dont think you need to open rdp, once it runs on specific port, you can just to test connection reg. the port number and the given address :). in PS, there should be [Test-NetConnection- module nettcpip](https://learn.microsoft.com/en-us/powershell/module/nettcpip/test-netconnection?view=win10-ps), and actually the same thing is at https://stackoverflow.com/questions/9566052/how-to-check-network-port-access-and-display-useful-message – xxxvodnikxxx Mar 26 '19 at 14:37
  • Possible duplicate of [How to check Network port access and display useful message?](https://stackoverflow.com/questions/9566052/how-to-check-network-port-access-and-display-useful-message), From independent source looks like [this](https://stackoverflow.com/a/9614040/4892907) is valid solution how to test ports :) – xxxvodnikxxx Mar 26 '19 at 14:39
  • 1
    I tried the port option, but it won't test if i can connect with a specefic user. – BeGreen Mar 26 '19 at 14:41
  • Ok, then its not an option for you :) – xxxvodnikxxx Mar 26 '19 at 14:44
  • I have found a workaround, I get all mstsc process and kill the first one. It works but I'm actually connected in RDP to the machine running the script, so it could kill my own session... – BeGreen Mar 26 '19 at 15:08
  • looks like you have the solution bellow :), actually, once you will run the command, you should be able to get PID somehow, or as shown bellow, you can work with the whole object then – xxxvodnikxxx Mar 26 '19 at 15:14

1 Answers1

3

You can validate that $app is properly populated by using :

$app = Get-WmiObject -Filter 'CommandLine LIKE "%$_%"' -Class Win32_Process
...
$app.Terminate()

Alternatively, you can utilize Start-Process with -PassThru to launch mstsc.exe and assign $app, and then when you're done: $app | Stop-Process

In action:

$app = Start-Process -FilePath C:\Windows\System32\mstsc.exe -ArgumentList "/v:$_" -PassThru
...
$app | Stop-Process
Maximilian Burszley
  • 18,243
  • 4
  • 34
  • 63
  • I tried to use `Start-Process` with `-PassThru` with the full path of `mstsc.exe`. I could retreive the ID from what I remember. Maybe I was wrong on the `$app | Stop-Process`. Going to check that asap. – BeGreen Mar 26 '19 at 15:17
  • 1
    Both solution worked. Thank you. I had some issue with Powershell Syntax. – BeGreen Mar 26 '19 at 15:22
  • Indeed, even harder if you come from Unix world – BeGreen Mar 26 '19 at 15:32