Solved

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

Posted on 2016-10-24
5
105 Views
Last Modified: 2016-10-24
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.
0
Comment
  • 3
  • 2
5 Comments
 
LVL 12

Accepted Solution

by:
Dustin Saunders earned 500 total points
ID: 41857696
Create an array of exceptions (either in the script or in a file you Get-Content import) and then check if the username is contained in the exceptions list.
Import-Module remotedesktop
$ClientUser =get-rdusersession | Group-Object -Property UserName |Where-Object {$_.Count -gt 1} | select -ExpandProperty name
$exceptions = "dsaunders","jsaunders","vsaunders","ssaunders"

$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

0
 
LVL 12

Expert Comment

by:Dustin Saunders
ID: 41857701
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

0
 
LVL 4
ID: 41857728
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!!!
0
 
LVL 4

Author Closing Comment

by:get-ADuser -F ($_.Name -eq "Todd")
ID: 41857734
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+
0
 
LVL 12

Expert Comment

by:Dustin Saunders
ID: 41857741
No problem, glad to help!
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
How to install and configure Citrix XenApp 6.5 - Part 1. In this video tutorial we have explained step by step installation of Citrix XenApp 6.5 Server on Windows Server 2008 R2 is explained in this video. We have explained the difference between…

830 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