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
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
40 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
  • 2
3 Comments
 
LVL 84

Accepted Solution

by:
oBdA earned 500 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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Utilizing an array to gracefully append to a list of EmailAddresses
A procedure for exporting installed hotfix details of remote computers using powershell
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

809 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