?
Solved

Powershell script that finds RDS Users.  (I'm really close)  But my variable for users has more than one, it wont work.

Posted on 2016-10-25
3
Medium Priority
?
50 Views
Last Modified: 2016-10-25
I had this question solved yesterday by Dustin Saunders, but I ran into one more detail that caused my script not to work.  

https://www.experts-exchange.com/questions/28978554/I-have-a-working-Powershell-Script-that-finds-RDUsers-but-I-need-to-add-an-exception-list-to-this-code.html

Here is the code that works if only "One" user logs on twice.

Import-Module remotedesktop

$ClientUser = get-rdusersession | Group-Object -Property UserName |Where-Object {$_.Count -gt 1} | select -ExpandProperty name
$exceptions = "drh","mdb","timh","jana"
$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
            }
         }
}

If you see Variable $ClientUser that is for the users that signed in twice.  Because I run this::

get-rdusersession | Group-Object -Property UserName |Where-Object {$_.Count -gt 1} | select -ExpandProperty name
I get this result for one user (Which the program works as it should)
RDST

But if it runs again, and there are more that one user that logged on twice, it wont work.  Example:
get-rdusersession | Group-Object -Property UserName |Where-Object {$_.Count -gt 1} | select -ExpandProperty name
And it comes up this
RDST
TIMSTEST


My script doesn't work.  I know it has something to do with my $UserSessions -eq $clientUser that suddenly had two.  I need help to resolve if multiple people end up in the $ClientUser.   Also the $Exemptions needs to work too.  This is so those users don't get logged off.   REALLY could use some help.
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
  • 2
3 Comments
 
LVL 85

Accepted Solution

by:
oBdA earned 2000 total points
ID: 41859161
Try it like this:
Import-Module RemoteDesktop
$Exceptions = "dsaunders", "jsaunders", "vsaunders", "ssaunders"

$ClientUser = Get-RDUserSession | Group-Object -Property UserName | Where-Object {$_.Count -gt 1} | Select-Object -ExpandProperty Name

$MultiSessions = Get-RDUserSession -CollectionName TIMSV7 | Where-Object {$ClientUser -contains $_.Username}
If ($MultiSessions) {
	ForEach ($UserSession in $MultiSessions) {
		If ($Exceptions -notcontains $($UserSession.username)) {
			$UserSessionID = $UserSession.UnifiedSessionID
			$UserSessionHost = $UserSession.HostServer
			Invoke-RDUserLogoff -HostServer $UserSessionHost -UnifiedSessionID $UserSessionID -force
		}
	}
}

Open in new window

1
 
LVL 4
ID: 41859218
You are absolutely amazing!!!   That worked!!!   Thank you is just not enough.  You made my day.  I have been working on this for a while now trying so hard to learn it.  I love Powershell.  I got mixed up in all the ForEach's, and variables.  Racking my brain so hard.  You're nothing short of a genius! I will continue classes on this, because it seems to be one powerful tool.  

Again.   Thanks!!!!!!!!!
0
 
LVL 4

Author Closing Comment

by:get-ADuser -F ($_.Name -eq "Todd")
ID: 41859229
oBdA saved the day.  This script is going to save us so many headaches.  And so much appreciation to helping me so quickly.  Nothing short of an A+ in my book.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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.
A brief introduction to what I consider to be the best editor for PowerShell.
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…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

777 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