HOW TO GET ALL REMOTE LOGGED ON USERS

Lingareddy Chandrakanth Reddy 1 Reputation point
2022-02-23T11:45:03.377+00:00

I wanted a simple way to get all (remote) logged on (and disconnected) users on all servers from my list.

I use the QUERY SESSION command for this purpose (as I found it to be the most quick and reliable one). The command is called as QWINSTA in my code (which is the same as QUERY SESSION) and works from Windows 2012 and up.

But with my script i will get the details of (server name, session name, sessionId and session state), but i need User IDOL time and User Logon time as well, but i feel with QWINSTA we cannot achieve that, with QUER User we can get those details.

Can some one help me to get all these details in my output (USERNAME, SESSIONNAME, ID, STATE, IDLE TIME, LOGON TIME)

Below one is my Code.

## Clear Host Console
Clear-Host

## Define Variable for Server Count
$z = 0

##Set Default Script Location
Set-Location -Path "C:\Users\reddy\Desktop\Active or Disc users"

## Provide List of Servers to Check for the Disconnected user session
$Servers = Get-Content ".\Servers\AZ_Servers.txt"

## Get Servers Count
$count = $Servers.count 

## Define Date for the Out file
$dt = Get-Date -Format yyyyMMdd
$Date = Get-Date

## Define Path for the Out File
$exportFile = ".\Out\RDP_DisConnected_Users.csv"

## Define Array for Storing the User sessions
$openSessions = @()

## Loop through each server to find the User Disconnected session
Foreach ($ServerName in $Servers)
{

#initiate counter for showing progress
    $z = $z + 1

# Start writing progress 
Write-Progress -Activity "Processing Server: $z out of $count servers." -Status " Progress" -PercentComplete ($z/$Servers.count*100)

## Add the servers if you want to exclude any
$ExcludedServers = "EXCLUDESRV01", "EXCLUDESRV02", "EXCLUDESRV03"
If ($ExcludedServers -notcontains $ServerName)
{
Write-Host "Getting session information for $ServerName"
$sessions = qwinsta /server $ServerName| ?{ $_ -notmatch '^ SESSIONNAME' } | %{
$item = "" | Select "ServerName", "Username", "Id", "State"
$item.ServerName = $ServerName
#$item.SessionName = $_.Substring(1,18).Trim()
$item.Username = $_.Substring(19,20).Trim()
$item.Id = $_.Substring(39,9).Trim()
$item.State = $_.Substring(48,8).Trim()
$item
}
$openSessions += $sessions | where { ($_.Username -ne "") -and ($_.Username -ne "Administrator") -and ($_.State -ne "Active")}
}
Else { Write-Host "Skipping named computer $ServerName" -ForegroundColor Green}
}

$openSessions | Export-Csv "$exportFile" -NoTypeInformation
Windows for business Windows Server User experience PowerShell
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Rich Matheisen 47,901 Reputation points
    2022-02-23T15:25:53.367+00:00

    Maybe this PowerShell module will help: 1.0

    0 comments No comments

  2. Gijs 1 Reputation point
    2022-02-23T15:34:54.667+00:00

    Are your servers using a connection broker? If the anwer is yes, then you can use the Powershell module RemoteDesktop

    Get-Module -ListAvailable
    Install-Module RemoteDesktop
    Import-Module RemoteDesktop

    Get-RDUserSession -ConnectionBroker "servername.domainname.local"


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.