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
48 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
Comment Utility
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
Comment Utility
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

Author Comment

by:get-ADuser -F ($_.Name -eq "Todd")
Comment Utility
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")
Comment Utility
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
Comment Utility
No problem, glad to help!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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 …
"Migrate" an SMTP relay receive connector to a new server using info from an old server.
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…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now