Link to home
Start Free TrialLog in
Avatar of get-ADuser -F ($_.Name -eq "Todd")
get-ADuser -F ($_.Name -eq "Todd")Flag for United States of America

asked on

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

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.
ASKER CERTIFIED SOLUTION
Avatar of oBdA
oBdA

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of get-ADuser -F ($_.Name -eq "Todd")

ASKER

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!!!!!!!!!
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.