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
23 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 83

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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 …
This article will help you understand what HashTables are and how to use them in PowerShell.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

706 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

16 Experts available now in Live!

Get 1:1 Help Now