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
159 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 13

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 13

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 13

Expert Comment

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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

This article will help you understand what HashTables are and how to use them in PowerShell.
The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
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…
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…

615 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