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.
enthuguyAsked:
Who is Participating?

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

x
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?
0
enthuguyAuthor Commented:
Hi Ares, thx for your response.

1.
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)

2.
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 :)
https://docs.microsoft.com/en-gb/powershell/module/Microsoft.PowerShell.Utility/Export-Csv?view=powershell-5.1
0
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.
0
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.
0
Ares KurkluSoftware EngineerCommented:
If any of these help

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

(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
(getloggeduser.ps1)

echo $env:username

qwinsta | select-string "Active"


(sendmail.ps1)
#smtp.office365.com
#$SMTPServer = "smtp.gmail.com"
$SMTPServer = "smtp.office365.com"
$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.to.add($to)
$message.from = $username
$message.attachments.add($attachment)

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

$smtp.send($message)
write-host "Mail Sent"
0
enthuguyAuthor Commented:
Thanks very much

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

echo $env:username

qwinsta | select-string "Active"
0
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):
https://gallery.technet.microsoft.com/scriptcenter/Find-user-logon-duration-667b8c48

PowerShell Script to fetch Logon/Logoff user on particular server {Get-WinEvent} {Get-EventLog}
https://social.technet.microsoft.com/Forums/lync/en-US/4f6815f1-2998-484c-a423-fe6507f1548c/powershell-script-to-fetch-logonlogoff-user-on-particular-server-getwinevent-geteventlog?forum=winserverpowershell

How to Detect Last Logon Date and Time for all Active Directory Users:
https://www.lepide.com/how-to/track-last-logon-date-and-time-for-active-directory-users.html
0

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
Powershell

From novice to tech pro — start learning today.