Go Premium for a chance to win a PS4. Enter to Win

x
?
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
Medium Priority
?
245 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 14

Accepted Solution

by:
Dustin Saunders earned 2000 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 14

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 14

Expert Comment

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

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
In the absence of a fully-fledged GPO Management product like AGPM, the script in this article will provide you with a simple way to watch the domain (or a select OU) for GPOs changes and automatically take backups when policies are added, removed o…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Loops Section Overview

886 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