Posted On July 4, 2020

Terminal Service Auditing – Generate Report of RDP Sessions with Certain Login Dates

kimconnect 0 comments
blog.KimConnect.com >> Codes , Windows >> Terminal Service Auditing – Generate Report of RDP Sessions with Certain Login Dates
# getLoginEvents.ps1

function getLoginEvents{
    param(
        $computername=$env:computername,
        $daysLimit=30
        )
    $ErrorActionPreference='stop'    
    try{        
        $logins=Get-WinEvent -ComputerName $ComputerName -LogName "Microsoft-Windows-TerminalServices-LocalSessionManager/Operational"| `
                ?{$_.ID -match '21|25' -and $_.TimeCreated -ge (get-date).AddDays(-$daysLimit)}| `
                Select Id,TimeCreated, Message
        $loginEvents=[System.Collections.ArrayList]::new()
        Foreach ($login in $logins){ 
            $loginTime = $login.TimeCreated
            $eventId=$login.Id
            $loginType=switch ($eventId){
                '21'{'New Session';break}
                '25'{'Reconnection';break}
                }
            $x = $login.Message -split "`n" 
            $user = ($x|Select-Object -Index "2").Substring(6)
            $source=($x|Select-Object -Index "4").Substring(24)       
            $null=$loginEvents.Add([PSCustomObject]@{
                loginTime = $loginTime;
                username = $user;
                loginType=$loginType;
                loginSource = $source;
                })
        }
        return $loginEvents
        }
    catch{
        write-warning "$($error[0])"
        return $null
    }
}
getLoginEvents

# Sample Output
#
#loginTime             username             loginType    loginSource
#---------             ----                 ---------    ---------
#7/3/2020 9:34:35 PM   KIMCONNECT\RAMBO1... Reconnection 192.168.0.127
#7/3/2020 9:34:19 PM   KIMCONNECT\RAMBO1... Reconnection 192.168.0.66
#7/3/2020 8:57:44 PM   KIMCONNECT\RAMBO1... Reconnection 192.168.0.127
#7/3/2020 5:59:24 PM   KIMCONNECT\RAMBO1... Reconnection 192.168.0.66
#7/3/2020 3:42:04 PM   KIMCONNECT\RAMBO1... Reconnection 192.168.0.66
#7/2/2020 9:20:19 PM   KIMCONNECT\RAMBO1... Reconnection 192.168.0.66

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Post

PowerShell: Remove Virtual Machine Snapshots in VMM

Base Cmdlets: $vmmServer='SOMETHINGHERE' $vmName='VMNAMEHERE' $snapshots=Get-SCVMCheckpoint -vmmserver $vmmServer -vm $(get-scvirtualmachine $vmName) $snapshots|%{Remove-SCVMCheckpoint -VMCheckpoint $_} Automation: #…

Install Microsoft FTP on Windows 2008 R2

1. Install IIS without FTP 2. Download FTP a. x64: b. x86: 3. Create FTP…

PowerShell: Get IP’s From Computer Names

Resolve from Names to IPs: $names=@( 'TESTVM001', 'TESTVM002', 'TESTVM003' ) foreach($name in $names){ $ips =…