# 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 = "RDP Sessions Report - " + $today $priority = "HIGH" $smtpServer = "SMTPSERVERHERE" $emailFrom = "RDPReport@YOURCO.local" $emailTo = "YOU@YOURCO.local" # Create a fresh variable to collect the results. You can use this to output as desired write-host "" write-host "" $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*"} -Properties * | where{$_.description -notlike "Failover*"} # 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`n" + $ServerName + " logged in by " + $RDPUser } } } # When running interactively, uncomment the Write-Host line below to see the full list on screen $SessionList write-host "" write-host "" $SessionList2 = "Disconnected SERVER SESSIONS REPORT - " + $today + "`n`n" 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 $state=$queryResult.STATE # We only want to display where a "person" is logged in. Otherwise unused sessions show up as USERNAME as a number If (($RDPUser -ne $NULL) -and ($sessionType -ne "console" )-and ($sessionType -ne "services")-and ($RDPUser -ne "65536" ) -and ($sessionType -ne "[1-9]") -and ($state -ne "Active")) { # When running interactively, uncomment the Write-Host line below to show the output to screen # Write-Host $ServerName logged in by $RDPUser on $sessionType $SessionList2 = $SessionList2 + "`n`n" + $ServerName + " has a disconnected session open by " + $sessionType } } } $SessionList2 $sessionlistfinal = $sessionlist + "`n`n" + $sessionlist2 # Send the report email Send-MailMessage -To $emailTo -Subject $subject -Body $SessionListfinal -SmtpServer $smtpServer -From $emailFrom -Priority $priority
Recent Comments