Listing users from RD farm and "kill" one of them

Hi all,

Allow me to “pick” your brain a bit?

I want to build a script in order to list all users from a RD farm and “kill” a specific user session. I had something done like this (sorry for the rough scripting):

# Gather the server name
$Servers = ('1','2','3','4','5')

# find all user IDs using rds
$users = @((Invoke-Command -ComputerName $Servers -ScriptBlock {Get-Process *rds* -IncludeUserName | Select-Object -ExpandProperty UserName}))

# List out the usernames which are connected and using WON
echo $users

# Which username to kill
$username = Read-Host -Prompt 'Type the username session you wish to kill'

foreach ($Server in $Servers) {
Invoke-RDUserLogoff -HostServer $Server -UnifiedSessionId $username
}

When running the script, the list of users present on the RD farm comes back exactly like I wanted, but when I type in the username I want to “kill” the session, it just comes back:

"Invoke-RDUserLogoff : Cannot process argument transformation on parameter ‘UnifiedSessionID’. Cannot convert value “coelhn01” to type “System.Int32”. Error: “Input string was not in a correct format.”

A little help please?

Thank you so much!

All the best!

I’m not familiar with `Invoke-RDUserLogoff`, but it seems to me that it’s expecting an int for -UnifiedSessionID and you’re providing a string-username. Documentation suggests that this is correct.

You probably need to convert the username to a session ID, and pass that instead of the user. The same documentation I link above suggests that you should:

Use Get-RDUserSession to retrieve the unique ID for a specific user session.
 

Thanks for the reply James :slight_smile:

And yes, you’re right. I went on “robot mode” and typed in the “-UnifiedSessionId” bit and didn’t even had a second look.

I’ll try to obtain a sessionID and go from there.

Cheers!

Windows Server 2012 R2: Get a list of active Remote Desktop Users
https://www.petri.com/windows-server-2012-r2-get-list-active-remote-desktop-users

PowerShell script to Find RDP sessions on Servers
A lightweight PowerShell script to query your Active Directory servers searching for active and in-use/disconnected sessions on the Console and through RDP. Requires Active Directory PowerShell module and the QWINSTA command (ships with Windows 2008 R2)
https://gallery.technet.microsoft.com/scriptcenter/PowerShell-script-to-Find-d2ba4252