Mundy

IT Knowledge Base

User Tools

Site Tools


list_rds_connected_and_disconnected_sessions_on_all_servers

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

list_rds_connected_and_disconnected_sessions_on_all_servers [2018/04/09 09:56] (current)
Line 1: Line 1:
 +====== List RDS Connected and Disconnected Sessions on All Servers ======
  
 +Following scripts adapted from [[https://​gallery.technet.microsoft.com/​scriptcenter/​PowerShell-script-to-Find-d2ba4252|PowerShell script to Find RDP sessions on Servers]]
 +
 +<file powershell list-disconnected-sessions.ps1>​
 +# Import the Active Directory module for the Get-ADComputer CmdLet ​
 +Import-Module ActiveDirectory ​
 + 
 +# Get today'​s date for the report ​
 +$today = Get-Date ​
 + 
 +# Create a fresh variable to collect the results. You can use this to output as desired ​
 +$SessionList = "​ACTIVE SERVER SESSIONS REPORT - " + $today + "​`n`n" ​
 + 
 +# Query Active Directory for computers running a Server operating system ​
 +$Servers = Get-ADComputer -Filter {OperatingSystem -like "​*server*"​} ​
 + 
 +# Loop through the list to query each server for login sessions ​
 +ForEach ($Server in $Servers) { 
 +    $ServerName = $Server.Name ​
 + 
 +    # When running interactively,​ uncomment the Write-Host line below to show which server is being queried ​
 +    Write-Host "​`nDisconnected sessions on $ServerName `n ------------------------------`n" ​
 + 
 +    # Run the qwinsta.exe and parse the output ​
 +    $queryResults = (qwinsta /​server:​$ServerName | foreach { (($_.trim() -replace "​\s+",","​))} | ConvertFrom-Csv)  ​
 +     
 +    # Pull the session information from each instance ​
 +    ForEach ($queryResult in $queryResults) { 
 +        $RDPUser = $queryResult.USERNAME ​
 +        $sessionType = $queryResult.SESSIONNAME ​
 +
 +        If (($RDPUser -notmatch "​[a-z]"​) -and ($RDPUser -ne $NULL) -and ($sessionType -ne "​services"​) -and ($sessionType -ne "​console"​) -and ($sessionType -ne "​rdp-tcp"​)) {  ​
 +            $sessionType ​
 +        } 
 +    } 
 +}
 +
 +#​$SessionList
 +</​file>​
 +
 +active sessions:
 +<​hidden>​
 +<​code>​
 +# Import the Active Directory module for the Get-ADComputer CmdLet ​
 +Import-Module ActiveDirectory ​
 + 
 +# Get today'​s date for the report ​
 +$today = Get-Date ​
 + 
 +# Setup email parameters ​
 +$subject = "​ACTIVE SERVER SESSIONS REPORT - " + $today ​
 +$priority = "​Normal" ​
 +$smtpServer = "​mail.smtp2go.com" ​
 +$emailFrom = "​ironbark-25526@mundy.co" ​
 +$emailTo = "​danielm@calibreone.com.au" ​
 + 
 +# Create a fresh variable to collect the results. You can use this to output as desired ​
 +$SessionList = "​ACTIVE SERVER SESSIONS REPORT - " + $today + "​`n`n" ​
 + 
 +# Query Active Directory for computers running a Server operating system ​
 +$Servers = Get-ADComputer -Filter {OperatingSystem -like "​*server*"​} ​
 + 
 +# Loop through the list to query each server for login sessions ​
 +ForEach ($Server in $Servers) { 
 +    $ServerName = $Server.Name ​
 + 
 +    # When running interactively,​ uncomment the Write-Host line below to show which server is being queried ​
 +    Write-Host "​Querying $ServerName" ​
 + 
 +    # Run the qwinsta.exe and parse the output ​
 +    $queryResults = (qwinsta /​server:​$ServerName | foreach { (($_.trim() -replace "​\s+",","​))} | ConvertFrom-Csv)  ​
 +     
 +    # Pull the session information from each instance ​
 +    ForEach ($queryResult in $queryResults) { 
 +        $RDPUser = $queryResult.USERNAME ​
 +        $sessionType = $queryResult.SESSIONNAME ​
 +
 +        # We only want to display where a "​person"​ is logged in. Otherwise unused sessions show up as USERNAME as a number ​
 +        If (($RDPUser -match "​[a-z]"​) -and ($RDPUser -ne $NULL)) {  ​
 +            # When running interactively,​ uncomment the Write-Host line below to show the output to screen ​
 +            # Write-Host $ServerName logged in by $RDPUser on $sessionType ​
 +            $SessionList = $SessionList + "​`n"​ + $ServerName + " logged in by " + $RDPUser + " on " + $sessionType ​
 +        } 
 +    } 
 +
 + 
 +# Send the report email 
 +#​Send-MailMessage -To $emailTo -Subject $subject -Body $SessionList -SmtpServer $smtpServer -From $emailFrom -Priority $priority ​
 + 
 +# When running interactively,​ uncomment the Write-Host line below to see the full list on screen ​
 +$SessionList
 +</​code>​
 +</​hidden>​
list_rds_connected_and_disconnected_sessions_on_all_servers.txt · Last modified: 2018/04/09 09:56 (external edit)