Solved

Add computerinfo to text doc at logon

Posted on 2014-03-18
4
168 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
  • 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 39

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 39

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

In this previous article (https://oddytee.wordpress.com/2016/05/05/provision-new-office-365-user-and-mailbox-from-exchange-hybrid-via-powershell/), we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
Set OWA language and time zone in Exchange for individuals, all users or per database.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

758 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now