Solved

Add computerinfo to text doc at logon

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

810 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