Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Add computerinfo to text doc at logon

Posted on 2014-03-18
4
Medium Priority
?
185 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 41

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 41

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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
There are times when we need to generate a report on the inbox rules, where users have set up forwarding externally in their mailbox. In this article, I will be sharing a script I wrote to generate the report in CSV format.
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 response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

604 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