Solved

Powershell script that finds RDS Users.  (I'm really close)  But my variable for users has more than one, it wont work.

Posted on 2016-10-25
3
33 Views
Last Modified: 2016-10-25
I had this question solved yesterday by Dustin Saunders, but I ran into one more detail that caused my script not to work.  

https://www.experts-exchange.com/questions/28978554/I-have-a-working-Powershell-Script-that-finds-RDUsers-but-I-need-to-add-an-exception-list-to-this-code.html

Here is the code that works if only "One" user logs on twice.

Import-Module remotedesktop

$ClientUser = get-rdusersession | Group-Object -Property UserName |Where-Object {$_.Count -gt 1} | select -ExpandProperty name
$exceptions = "drh","mdb","timh","jana"
$UserSessions = Get-RDUserSession -CollectionName TIMSV7 | Where-Object {$_.username -eq $ClientUser}

if ($UserSessions)
     {
     foreach ($UserSession in $UserSessions)
         {
            if ($exceptions -notcontains $($UserSession.username))
            {
                $UserSessionID = $UserSession.UnifiedSessionID
                $UserSessionHost = $UserSession.HostServer
               
                Invoke-RDUserLogoff -HostServer $UserSessionHost -UnifiedSessionID $UserSessionID -force
            }
         }
}

If you see Variable $ClientUser that is for the users that signed in twice.  Because I run this::

get-rdusersession | Group-Object -Property UserName |Where-Object {$_.Count -gt 1} | select -ExpandProperty name
I get this result for one user (Which the program works as it should)
RDST

But if it runs again, and there are more that one user that logged on twice, it wont work.  Example:
get-rdusersession | Group-Object -Property UserName |Where-Object {$_.Count -gt 1} | select -ExpandProperty name
And it comes up this
RDST
TIMSTEST


My script doesn't work.  I know it has something to do with my $UserSessions -eq $clientUser that suddenly had two.  I need help to resolve if multiple people end up in the $ClientUser.   Also the $Exemptions needs to work too.  This is so those users don't get logged off.   REALLY could use some help.
0
Comment
  • 2
3 Comments
 
LVL 83

Accepted Solution

by:
oBdA earned 500 total points
ID: 41859161
Try it like this:
Import-Module RemoteDesktop
$Exceptions = "dsaunders", "jsaunders", "vsaunders", "ssaunders"

$ClientUser = Get-RDUserSession | Group-Object -Property UserName | Where-Object {$_.Count -gt 1} | Select-Object -ExpandProperty Name

$MultiSessions = Get-RDUserSession -CollectionName TIMSV7 | Where-Object {$ClientUser -contains $_.Username}
If ($MultiSessions) {
	ForEach ($UserSession in $MultiSessions) {
		If ($Exceptions -notcontains $($UserSession.username)) {
			$UserSessionID = $UserSession.UnifiedSessionID
			$UserSessionHost = $UserSession.HostServer
			Invoke-RDUserLogoff -HostServer $UserSessionHost -UnifiedSessionID $UserSessionID -force
		}
	}
}

Open in new window

1
 
LVL 4
ID: 41859218
You are absolutely amazing!!!   That worked!!!   Thank you is just not enough.  You made my day.  I have been working on this for a while now trying so hard to learn it.  I love Powershell.  I got mixed up in all the ForEach's, and variables.  Racking my brain so hard.  You're nothing short of a genius! I will continue classes on this, because it seems to be one powerful tool.  

Again.   Thanks!!!!!!!!!
0
 
LVL 4

Author Closing Comment

by:get-ADuser -F ($_.Name -eq "Todd")
ID: 41859229
oBdA saved the day.  This script is going to save us so many headaches.  And so much appreciation to helping me so quickly.  Nothing short of an A+ in my book.
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In this previous article (https://oddytee.wordpress.com/2016/05/05/provision-new-office-365-user-and-mailbox-from-exchange-hybrid-via-powershell/), we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
Synchronize a new Active Directory domain with an existing Office 365 tenant
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question