Solved

Add computerinfo to text doc at logon

Posted on 2014-03-18
4
173 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 500 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 500 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Synchronize a new Active Directory domain with an existing Office 365 tenant
This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
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…

730 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