Powershell – List Users Logged Into A Range Of Computers

So this script get a list of users logged into computers. It uses my personal favourite RDS PowerShell module but works on PCs. Note: some firewall rule(s) inbound may be required.Its something I did one night when pondering how to show an availability sheet for rooms where there is only 1 specialist PC in a room for use by many people (and there are 30 rooms)

So rather than use a calendaring system, just look at  an internet webpage and choose your nearest room that is free.

It only looks for an active user. Any use that has been switched out (disconnected status) is filtered out.

# Module psterminalservices obtained from https://psterminalservices.codeplex.com/

import-module -name psterminalservices
$SecurityGroup = "My Security group"
$Result = $NULL
$ListofComputers = Get-ADGroupMember -Identity $SecurityGroup 
$Output = New-Object System.Object

$Result = foreach ($item in $ListofComputers.name)
{
$ComputerState = get-tssession -ComputerName $item -ErrorAction SilentlyContinue -WarningAction SilentlyContinue |where {($_.State -match "Active")}

if ($ComputerState)
{
$UserDetails = $ComputerState.username.split("\") |get-aduser
$Output = New-Object System.Object
            $Output| Add-Member -type NoteProperty -name Computername -value $item
            $Output| Add-Member -type NoteProperty -name Location -value (get-adcomputer -identity $item -Properties HomePage).HomePage
            $Output| Add-Member -type NoteProperty -name Givenname -value $UserDetails.Givenname
            $Output| Add-Member -type NoteProperty -name UserName -value $UserDetails.SamAccountName
            $Output| Add-Member -type NoteProperty -name Surname  -value $UserDetails.Surname 
           
            $Output
}
else
{
$Output = New-Object System.Object
            $Output| Add-Member -type NoteProperty -name Computername -value $item
            $Output| Add-Member -type NoteProperty -name Location -value (get-adcomputer -identity $item -Properties HomePage).HomePage
            $Output| Add-Member -type NoteProperty -name UserName -value ""
            $Output| Add-Member -type NoteProperty -name Givenname -value ""
            $Output| Add-Member -type NoteProperty -name Surname  -value ""
            $Output
}

}
$Result  

 

 

Leave a comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.