Link to home
Start Free TrialLog in
Avatar of NTTIT
NTTIT

asked on

Log on script (2)

With thanks to AndrewJDavis, the script when I log into windows takes down the username perfectly. But as I am on a server, it would be good if the computer name could also be taken down. Can someone add to this script so that the computer name is taken down too

DIM fso, NTBMFile, NTBMNet, strUser
Set fso = CreateObject("Scripting.FileSystemObject")
Set NTBMNet = WScript.CreateObject("WScript.Network")
strUser =NTBMNet.UserName
on error resume next

'WScript.Echo "Starting Script!"

' You will need to change this line to reflect the file that you want to store the info in.
Set NTBMFile = fso.OpenTextFile("H:\saved.txt", 8, True)
NTBMFile.Write((Date) & " , ")
NTBMFile.Write((Time) & " , ")
NTBMFile.Writeline (struser &" , Logged ON")
NTBMFile.Close

'In the final version comment out or delete the line below so that it doesnt popup for your end user it is only for testing purposes.
WScript.Echo "Finished"



Thanks Guys
Avatar of sirbounty
sirbounty
Flag of United States of America image

DIM fso, NTBMFile, NTBMNet, strUser
Set fso = CreateObject("Scripting.FileSystemObject")
Set NTBMNet = WScript.CreateObject("WScript.Network")
strUser =NTBMNet.UserName
'simply add this next line...
strPC=NTBMNet.Computername
on error resume next
...oops - and modify this line:

NTBMFile.Writeline (struser & " Logged on to " & strPC)
ASKER CERTIFIED SOLUTION
Avatar of sirbounty
sirbounty
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of NTTIT
NTTIT

ASKER

It didn't add anything to the .txt file
For clarity, also add this constant:

Const ForAppend=8
DIM fso: Set fso = CreateObject("Scripting.FileSystemObject")
Dim NTBMNet: Set NTBMNet = WScript.CreateObject("WScript.Network")
strUser =NTBMNet.UserName
strPC=NTBMNet.ComputerName
Dim NTBMFile: Set NTBMFile = fso.OpenTextFile("H:\saved.txt", ForAppend, True)
NTBMFile.WriteLine(Date & " , " & Time & vbNewLine & strUser & ", Logged On," & strPC)
NTBMFile.Close
WScript.Echo "Finished"
Didn't or did?  Looks like the Q is closed? : \
Avatar of NTTIT

ASKER

Your help was great Sirbounty, thank you so much