Created
March 16, 2014 16:18
-
-
Save daveselias/9585689 to your computer and use it in GitHub Desktop.
Gather RDP Session info and send to user
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Function Get-LoggedOnUser { | |
| <# | |
| .Synopsis | |
| Queries a computer to check for interactive sessions | |
| .DESCRIPTION | |
| This script takes the output from the quser program and parses this to PowerShell objects | |
| .NOTES | |
| Name: Get-LoggedOnUser | |
| Author: Jaap Brasser | |
| Version: 1.1 | |
| DateUpdated: 2013-06-26 | |
| .LINK | |
| http://www.jaapbrasser.com | |
| .PARAMETER ComputerName | |
| The string or array of string for which a query will be executed | |
| .EXAMPLE | |
| .\Get-LoggedOnUser.ps1 -ComputerName server01,server02 | |
| Description: | |
| Will display the session information on server01 and server02 | |
| .EXAMPLE | |
| 'server01','server02' | .\Get-LoggedOnUser.ps1 | |
| Description: | |
| Will display the session information on server01 and server02 | |
| #> | |
| param( | |
| [CmdletBinding()] | |
| [Parameter(ValueFromPipeline=$true, | |
| ValueFromPipelineByPropertyName=$true)] | |
| [string[]]$ComputerName = 'localhost' | |
| ) | |
| process { | |
| foreach ($Computer in $ComputerName) { | |
| quser /server:$Computer | Select-Object -Skip 1 | ForEach-Object { | |
| $CurrentLine = $_.Trim() -Replace '\s+',' ' -Split '\s' | |
| $HashProps = @{ | |
| UserName = $CurrentLine[0] | |
| ComputerName = $Computer | |
| } | |
| # If session is disconnected different fields will be selected | |
| if ($CurrentLine[2] -eq 'Disc') { | |
| $HashProps.SessionName = $null | |
| $HashProps.Id = $CurrentLine[1] | |
| $HashProps.State = $CurrentLine[2] | |
| $HashProps.IdleTime = $CurrentLine[3] | |
| $HashProps.LogonTime = $CurrentLine[4..6] -join ' ' | |
| } else { | |
| $HashProps.SessionName = $CurrentLine[1] | |
| $HashProps.Id = $CurrentLine[2] | |
| $HashProps.State = $CurrentLine[3] | |
| $HashProps.IdleTime = $CurrentLine[4] | |
| $HashProps.LogonTime = $CurrentLine[5..7] -join ' ' | |
| } | |
| New-Object -TypeName PSCustomObject -Property $HashProps | | |
| Select-Object -Property UserName,ComputerName,SessionName,Id,State,IdleTime,LogonTime | |
| } | |
| } | |
| } | |
| } | |
| #get list of all servers to check sessions on from Active Directory | |
| $AllServers = (Get-ADComputer -filter {name -like "*servernameconvention*"}).name | |
| #Check all sessions on Servers one by one | |
| $AllSessions=$List=$MasterList=@() | |
| ForEach($ComputerName in $AllServers){ | |
| $AllSessions += (Get-LoggedOnUser -ComputerName $ComputerName -ErrorAction SilentlyContinue) | |
| } | |
| #Filter results down to Sessions older than 1 Hour | |
| $AllSessions = $AllSessions | Where-Object {($_.IdleTime -like "*:*") -and ($_.IdleTime -gt "00:59")} | |
| #Find User information from Active Directory | |
| $Users = ($AllSessions | select Username -Unique) | |
| ForEach($U in ($Users).username){ | |
| $List += (Get-ADUser $U -Properties * | select Samaccountname, Name, mail, company) | |
| } | |
| #If username designated as a Domain Admin account Drop the "-DA" and locate the e-mail address of the account owner | |
| ForEach($u in $list){ | |
| IF(($u.mail -eq $null) -and ($u.samaccountname -like "*-DA")){ | |
| $SAM = $u.Samaccountname.substring(0,6) | |
| $U.mail = ((Get-ADUser $SAM -Properties mail).mail) | |
| } | |
| $MasterList += $U | |
| } | |
| #Add e-mail addresses from contacts (typically vendor accounts i.e. DELL, HP, etc.) | |
| $Contacts = Get-ADObject -Filter {Objectclass -like "contact"} -Properties * | |
| ForEach($U in $MasterList){ | |
| IF($U.mail -eq $null){ | |
| $Name = $U.name.split(',')[0] | |
| $Mail = $Contacts | Where-Object {($_.name -like "*$Name*") -and ($_.mail -like "*$($u.company)*")} | |
| $U.mail = $Mail.mail | |
| } | |
| } | |
| # Setup email parameters | |
| $today = Get-Date | |
| $priority = "Normal" | |
| $smtpServer = "mail.EmailServer.com" | |
| $emailFrom = "Notifications@EmailServer.com" | |
| #Send Individual Reports | |
| ForEach($u in $MasterList){ | |
| $subject = "Remote Server Sessions Report for $($u.samaccountname) - " + $today | |
| $Body = '<font face="Arial">' | |
| $Body += "<H4> Your UserID $($u.samaccountname), was found to have sessions exceeding 1 hour of idle time on the following servers. Please connect back in and log off properly.</H4><p>`n`n" | |
| $Body += "`n" | |
| $Body += '<font color="FF0000">' | |
| $Body += "<H5>This is an automated e-mail, please do not reply</H5><p>" | |
| $Body += "</font>" | |
| $Body += "`n" | |
| $Body += ($AllSessions | Where-Object {($_.username -like ($u).samaccountname)} | ConvertTo-html) | |
| $Body += "`n" | |
| IF($u.mail.count -gt 0){ | |
| $Body += "</font>" | |
| Write-Host "$($U.name), $($u.mail)" | |
| $emailTo = "$($u.mail)" | |
| Send-MailMessage -To $emailTo -Subject $subject -Body $Body -BodyAsHtml -SmtpServer $smtpServer -From $emailFrom -Priority $priority | |
| }ELSE{ | |
| $emailTo = "Admin@EmailServer.com" | |
| $Body += "`n `n" | |
| $Body += $U | Select Samaccountname, Name, Company | ConvertTo-Html | |
| $Body += "</font>" | |
| Send-MailMessage -To $emailTo -Subject $subject -Body $Body -BodyAsHtml -SmtpServer $smtpServer -From $emailFrom -Priority $priority | |
| } | |
| } | |
| #Master Report | |
| $emailTo = "Admin@EmailServer.com" | |
| $Subject = "Remote Server Sessions Report for - " + $today | |
| $Body = '<font face="Arial">' | |
| $Body += "Remote Server Sessions Report for - " + $today | |
| $Body += "`n" | |
| $Body += "`n" | |
| $Body += "The following is a list of sessions that have exceeded 1 hour" | |
| $Body += "`n" | |
| $Body += $AllSessions | Sort-Object Username | ConvertTo-Html | |
| $Body += "</font>" | |
| Send-MailMessage -To $emailTo -Subject $subject -Body $Body -BodyAsHtml -SmtpServer $smtpServer -From $emailFrom -Priority $priority |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment