• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 58
  • Last Modified:

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.
0
enthuguy
Asked:
enthuguy
  • 4
  • 3
2 Solutions
 
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
Simplify Active Directory Administration

Administration of Active Directory does not have to be hard.  Too often what should be a simple task is made more difficult than it needs to be.The solution?  Hyena from SystemTools Software.  With ease-of-use as well as powerful importing and bulk updating capabilities.

 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Simplify Active Directory Administration

Administration of Active Directory does not have to be hard.  Too often what should be a simple task is made more difficult than it needs to be.The solution?  Hyena from SystemTools Software.  With ease-of-use as well as powerful importing and bulk updating capabilities.

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now