Skip to content

Commit 9ea3685

Browse files
committed
Create RDP-disconnected-logon-sessions.txt
1 parent ed0176c commit 9ea3685

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Input and output file paths
2+
$serversFile = "D:\SupportStore\Scripts\rdp-disconnected-sessions\servers.txt"
3+
$outputFile = "D:\SupportStore\Scripts\rdp-disconnected-sessions\DisconnectedSessionsReport.csv"
4+
5+
# Check if input file exists
6+
if (-Not (Test-Path $serversFile)) {
7+
Write-Host "Input file $serversFile not found." -ForegroundColor Red
8+
exit
9+
}
10+
11+
# Initialize output file with headers
12+
"ServerName,LogonAccount,SessionStatus,IdleTimeOrDisconnectedTime" | Out-File -FilePath $outputFile -Encoding UTF8
13+
14+
# Read server list from file
15+
$servers = Get-Content $serversFile
16+
17+
# Iterate over each server
18+
foreach ($server in $servers) {
19+
Write-Host "Checking server: $server"
20+
21+
try {
22+
# Query session details using qwinsta
23+
$sessions = qwinsta /server:$server | ForEach-Object {
24+
($_.Trim() -replace '\s{2,}', ',').Split(',')
25+
} | Where-Object { $_[0] -match '^[0-9]+$' -and $_[3] -eq 'Disc' }
26+
27+
# Process each disconnected session
28+
foreach ($session in $sessions) {
29+
$logonAccount = $session[1]
30+
$status = $session[3]
31+
$idleTime = $session[4]
32+
33+
# Append to output file
34+
"$server,$logonAccount,$status,$idleTime" | Out-File -FilePath $outputFile -Append -Encoding UTF8
35+
}
36+
} catch {
37+
Write-Host "Failed to query $server" -ForegroundColor Yellow
38+
}
39+
}
40+
41+
Write-Host "Report generated at $outputFile" -ForegroundColor Green

0 commit comments

Comments
 (0)