Advertisement
Guest User

Untitled

a guest
Mar 17th, 2018
504
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Import the Active Directory module for the Get-ADComputer CmdLet
  2. Import-Module ActiveDirectory
  3.  
  4. # Get today's date for the report
  5. $today = Get-Date
  6.  
  7. # Setup email parameters
  8. $subject = "Active Server RDP Sessions - " + $today
  9. $priority = "Normal"
  10. $smtpServer = "mail.domain.com"
  11. $emailFrom = "noreply@domain.com"
  12. $emailTo = "youremail@domain.com"
  13.  
  14. # Create a fresh variable to collect the results. You can use this to output as desired
  15. $SessionList = "ACTIVE SERVER SESSIONS REPORT - " + $today + "`n`n"
  16.  
  17. # Query Active Directory for computers running a Server operating system
  18. $90days = $today.AddDays(-90)
  19. $Servers = Get-ADComputer -SearchBase "OU=Server Accounts,DC=domain,DC=com" -Filter {Modified -gt $90days -and Enabled -eq $true} | sort Name
  20.  
  21.  
  22. # Loop through the list to query each server for login sessions
  23. ForEach ($Server in $Servers) {
  24.     $ServerName = $Server.Name
  25.  
  26.     Write-Host Interrogating $ServerName
  27.  
  28.     # When running interactively, uncomment the Write-Host line below to show which server is being queried
  29.     # Write-Host "Querying $ServerName"
  30.  
  31.     # Run the qwinsta.exe and parse the output
  32.     $queryResults = (qwinsta /server:$ServerName | foreach { (($_.trim() -replace "\s+",","))} | ConvertFrom-Csv)  
  33.      
  34.     # Pull the session information from each instance
  35.     ForEach ($queryResult in $queryResults) {
  36.         $RDPUser = $queryResult.USERNAME
  37.         $sessionType = $queryResult.SESSIONNAME
  38.          
  39.          
  40.         # We only want to display where a "person" is logged in. Otherwise unused sessions show up as USERNAME as a number
  41.         If (($RDPUser -match "[a-z]") -and ($RDPUser -ne $NULL)) {  
  42.             # When running interactively, uncomment the Write-Host line below to show the output to screen
  43.             #Write-Host $ServerName logged in by $RDPUser on $sessionType
  44.             $SessionList = $SessionList + "`n`n" + $ServerName + " logged in by " + $RDPUser + " on " + $sessionType
  45.         }
  46.  
  47.         # We also want disconnected sessions
  48.         If ($queryResult.ID -like "Disc" -and $queryResult.SESSIONNAME -ne "services") {
  49.             #Write-Host $ServerName logged in by $sessionType and disconnected
  50.             $SessionList = $SessionList + "`n`n" + $ServerName + " logged in by " + $sessionType + " and disconnected "
  51.         }
  52.     }
  53. }
  54.  
  55. # Send the report email
  56. Send-MailMessage -To $emailTo -Subject $subject -Body $SessionList -SmtpServer $smtpServer -From $emailFrom -Priority $priority
  57.  
  58. # When running interactively, uncomment the Write-Host line below to see the full list on screen
  59. $SessionList
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement