?
Solved

Add computerinfo to text doc at logon

Posted on 2014-03-18
4
Medium Priority
?
179 Views
Last Modified: 2014-03-20
Hi,

We would like to log computerinformation everytime a users logs on to their computer, to a file on a share.

For example, if Computer01 logs on, it logs its computername, username, time etc. to a textfile on a share. The next time it logs on, it replaces this line. All the other computers do the same into the same file, so when it inserts the line, it makes sure it puts the line in so its in order.

Example:

AComputer01  User01  Time01
BComputer02  User02  Time02
CComputer03  User03  Time03
0
Comment
Question by:Sum Wum
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 14

Expert Comment

by:Justin Yeung
ID: 39936965
create it as a powershell ps1 script, add it as a logon script

"ComputerName = " + $env:computername + ";" + "User Name = " + $env:username + ";" + "Logon Time = " + $(Get-Date).Tostring() | Out-File \\server\share.txt -append
0
 
LVL 40

Assisted Solution

by:footech
footech earned 2000 total points
ID: 39937916
Recording the logon info is easy.  Updating the file everytime so that it only includes the latest entry from a particular computer is less so.  You would have to read in the file, find a particular entry, replace text, then write the entire file out again.  And if you have a bunch of computers trying to do this at the same time, I think it's possible that you could get errors due to the file being locked, or some updates being overwritten with older data.  You can give the following a shot.
$comp = $env:computername
$user = $env:username
$time = Get-Date -Format g
$data = Get-Content logonlist.txt

#Check whether the file has an existing entry for the computer.
If ($data -match "^$comp,")
{
    #Update the entry
    $data -replace "^($comp),.+$","`$1,$user,$time" | Out-File logonlist.txt -Encoding ascii
}
Else
{
    #If no matching entry is found, append a new entry to the file. 
    Write-Output "$comp,$user,$time" | Add-Content logonlist.txt
}

Open in new window

0
 
LVL 1

Author Comment

by:Sum Wum
ID: 39942007
Footech: It works pretty good, but is it possible to add the new entry in the file so it is in order?

If you for example have 3 computers with these names:
London
Copenhagen
Auburn

I would like to add them to the document in order:
Auburn
Copenhagen
London

Is it possible?
0
 
LVL 40

Accepted Solution

by:
footech earned 2000 total points
ID: 39942701
Personally, I don't think it's worth the effort.  You can easily open the file at any time with Excel and sort by whichever column you want (it may be more appropriate to give the file a .CSV extension).

Rather than reworking the entire script to create objects with properties that can then be exported, I'll just provide you with something that can be tacked on at the end.  Basically it just reads in the file, does a sort, then outputs the file again.
( Import-Csv logonlist.txt -Header Computer,User,Time ) | Sort -Property Computer | % { Write-Output "$($_.computer),$($_.user),$($_.time)" } | Out-File logonlist.txt

Open in new window

0

Featured Post

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A brief introduction to what I consider to be the best editor for PowerShell.
Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question