How to automate window login reporting on Windows Server 2012 R2

Hi Experts,

In Windows Server 2012 R2, would like to send report of users who have logged In. Basic info like ...
1. Login name
2. Login date time
3. Logoff date time
4. Duration for that session.

Preferably, through scripting...reason, I can execute remotely (pull) and schedule within windows server and push data through send email or any way of communication.
This server is  a remote desktop (Jump box)

Please help

Thanks in advance.
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ares KurkluSoftware EngineerCommented:
Hi, do you want to report when someone RDP in?
enthuguyAuthor Commented:
Hi Ares, thx for your response.

I have jenkins agent running on this windows server. So I was thinking the pull model here. we can even place powershell script or trigger task scheduler which will generate csv file and pull it back to master jenkins (caller server)

Same powershell script invocation or trigger task scheduler to push/send email with csv.

Found this, but when I used it I get no data :)
Ares KurkluSoftware EngineerCommented:
I have done something similar RDP logins that's why i asked, as you mentioned in #1 I used a scheduler task to trigger in the even of someone logged into the server which then was calling a dos script which was using 2 powershell scripts one was  retrieving the logged in user information and dumping into a text file the other powershell script is emailng the login text file as attachment.
Acronis True Image 2019 just released!

Create a reliable backup. Make sure you always have dependable copies of your data so you can restore your entire system or individual files.

enthuguyAuthor Commented:
Thanks, that would be perfect :)

additionally, schedule a job to pull the data from this host periodically.
Ares KurkluSoftware EngineerCommented:
If any of these help

You will have to create the event then when event triggered calls login.bat

@echo off
"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -command "C:\Tmp\getloggeduser.ps1" > "c:\temp\login.txt"
"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -command "C:\Tmp\sendmail.ps1"

This get logged rdp users

echo $env:username

qwinsta | select-string "Active"

#$SMTPServer = ""
$SMTPServer = ""
$SMTPPort = "587"

$Username = ""
$Password = ""

$to = ""
$cc = ""
$cc2 = ""
$subject = "Rdp Login"
$body = "Login details in attachment"
$attachment = "C:\temp\login.txt"

$message = New-Object System.Net.Mail.MailMessage
$message.subject = $subject
$message.body = $body
$message.from = $username

$smtp = New-Object System.Net.Mail.SmtpClient($SMTPServer, $SMTPPort);
$smtp.EnableSSL = $true
$smtp.Credentials = New-Object System.Net.NetworkCredential($Username, $Password);

write-host "Mail Sent"
enthuguyAuthor Commented:
Thanks very much

can I have "getloggeduser.ps1" as well pls? I will give a try :)
Ares KurkluSoftware EngineerCommented:
Was already in there

echo $env:username

qwinsta | select-string "Active"
Naveen SharmaCommented:
You can create the batch files that enables an admin to keep a log of users log on to and when they log off. Moreover, you can audit active directory to track user log on/log off date and time with granular reports.

Something like this:

Get-EventLog System -Source Microsoft-Windows-WinLogon -After (Get-Date).AddDays(-5) -ComputerName computername

Open in new window

Find user logon duration (PowerShell):

PowerShell Script to fetch Logon/Logoff user on particular server {Get-WinEvent} {Get-EventLog}

How to Detect Last Logon Date and Time for all Active Directory Users:

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.