I have a script that I use to document whenever a person logs on to my computer at home. I have everything working except 1 thing, when I run the code through task scheduler, it will output this:

<time>
Logged in System

Why does it say the username is 'System'? Also, I would like to know if I can make it execute this code every time my computer is unlocked.


Code:

Set objFSO = createObject("scripting.FilesystemObject")
strUser = createObject("wscript.network").username
outFile="C:\Users\[username]\Desktop\test.txt"

Set objfile = CreateObject("Scripting.FileSystemObject").OpenTextFile("C:\Users\[username]\Desktop\test.txt",8,true)
logintime = FormatDateTime(now)
objFile.write logintime & VBnewline
objFile.write "Logged in " & struser & VBnewLine
For the system, its because you are not asking for the username. If you want to get the username, you can just do
Code:
Set wshShell = CreateObject( "WScript.Shell" )
strUser = wshShell.ExpandEnvironmentStrings( "%USERNAME%" )

If you want to make the code run when you login to your computer, you can just place your script in your \AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup folder, and it should run on startup.
Hope this helps! Smile

Also, you don't need to create an object for the FSO, you can remove this line enitrely:

Code:
Set objFSO = createObject("scripting.FilesystemObject")
mr womp womp wrote:
For the system, its because you are not asking for the username. If you want to get the username, you can just do
Code:
Set wshShell = CreateObject( "WScript.Shell" )
strUser = wshShell.ExpandEnvironmentStrings( "%USERNAME%" )

If you want to make the code run when you login to your computer, you can just place your script in your \AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup folder, and it should run on startup.
Hope this helps! Smile

Also, you don't need to create an object for the FSO, you can remove this line enitrely:

Code:
Set objFSO = createObject("scripting.FilesystemObject")


Ok, so I want it to run every time that the computer is accessed, including when I am simply waking it from sleep.
BigSmoke3320 wrote:
mr womp womp wrote:
For the system, its because you are not asking for the username. If you want to get the username, you can just do
Code:
Set wshShell = CreateObject( "WScript.Shell" )
strUser = wshShell.ExpandEnvironmentStrings( "%USERNAME%" )

If you want to make the code run when you login to your computer, you can just place your script in your \AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup folder, and it should run on startup.
Hope this helps! Smile

Also, you don't need to create an object for the FSO, you can remove this line enitrely:

Code:
Set objFSO = createObject("scripting.FilesystemObject")


Ok, so I want it to run every time that the computer is accessed, including when I am simply waking it from sleep.

For that, you will have to create a new task in the windows task scheduler and tell it to run your script when you return from idle. I'm a bit fuzzy on the specifics of how this is done. Running the script when you return from sleep mode is a lot harder because you haven't necessarily disconnected from the user. However, if you're trying to make it run every time someone logs in, then I think you should be ok with just putting it in your startup folder.
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement