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

I have a working Powershell Script that finds RDUsers , but I need to add an exception list to this code.

Here is a rundown of my environment.  We have a Business system program that runs primarily on RDS.  We have an RDS collection consisting of 7 servers.  The license to use this is expensive and the system would know if we go over.  So in order to not allow a user to log in twice (Keep in mind a GPO to log on once, won't work with 7 servers and the broker).  I created this script, and it works great.  "However"!!!  A monkey wrench came into this plan.  There are several users that DO need to log on with the same account twice.  So I need some kind of If function that if a certain user doubled up, its ok.  And not to Invoke-RDuserlogoff, like I have in the script.  I can create a CSV, or whatever, but I don't know how to add it to my script.  EXAMPLE.  User JANA, TMB, DLL, are exempt from this script logging them off.  Here is the code.  That is set to find anyone that is logged in twice.

Import-Module remotedesktop
$ClientUser =get-rdusersession | Group-Object -Property UserName |Where-Object {$_.Count -gt 1} | select -ExpandProperty name

$UserSessions = Get-RDUserSession -CollectionName TIMSV7 | Where-Object {$_.username -eq $ClientUser}

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

So again this works.  Now how do I add somewhere in this not to log off users that I specify in variable, CSV, TXT?  Something.  I will be running this script every 15 minutes, and the users I want to exempt, wont change unless they leave the company.
ASKER CERTIFIED SOLUTION
Avatar of Dustin Saunders
Dustin Saunders
Flag of United States of America image

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
If you want to use a text file, create one with one user per line and then use get-content:
Import-Module remotedesktop
$ClientUser =get-rdusersession | Group-Object -Property UserName |Where-Object {$_.Count -gt 1} | select -ExpandProperty name
$exceptions = Get-Content "C:\scripts\exceptionList.txt"

$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
            }
         }
}

Open in new window

Avatar of get-ADuser -F ($_.Name -eq "Todd")

ASKER

THATS IT!!!!   Thank you so much!!!  And thank you for both options.  I think I am still living in the VBA world of "End IF" or Else IF"   or something like that.  But I just tested it and it works like a charm!!!
Outstanding and quick.  I knew I was close but this would have taken me a while to get it right.  Dustin thanks so much for both choices.  I will use this in my future scripts!!!   A+
No problem, glad to help!