Reputation: 1400
After someone has logged off (Start button→Logoff) for the night, at a certain time in the morning I want to have the Task Scheduler automatically log into the Windows 7 user account (the opposite of Start button→Logoff) that is password protected. The machine is 64 bit. I am doing this so systems (vbs, vba, etc.) can prep files prior to the user showing up to work.
I was pretty sure this was possible, but I can't find it anywhere online, so I am wondering if it really is possible.
I have to this in a VBScript.
For security reasons I need to log him off at night, but I figured that part out (vbs from SO):
Set = wshell = Wscript.CreateObject(Wscript.Shell")
wschell.exec("shutdown.exe -L -F") 'logoff and force shutdown programs
Edit1: Using an answer from Sujith, I attempted to set this up. First I must point out that this is NOT a remote computer or connection. I'm trying to get the local computer to log back in to the user account.
I created logon.vbs with this vbs code:
Set = wshell = Wscript.CreateObject(Wscript.Shell")
wschell.exec("shutdown.exe -L -F") 'logoff and force shutdown programs
WScript.Sleep(5000)
computer = "computername"
username = "OTHERDOMAIN\user"
password = "password"
Set locator = CreateObject("WbemScripting.SWbemLocator")
Set wmi = locator.ConnectServer(computer, "root\default", username, password)
wmi.Security_.ImpersonationLevel = 3
In other words, I am logging off and right back on (avoiding the Task Scheduler so things are simplified). I also tried a lot of variations on this trying to get it to work. It logs me off just fine, but nothing happens after that. I have to login manually.
I also tried the Task Scheduler setting mentioned in Sujith's answer, but it never turned on that I could tell.
Edit2: Or please tell me what you need to provide an answer?
Upvotes: 3
Views: 2397
Reputation: 61
I don't think what you're trying to do is possible. If you can do a reboot first, you can solve it by using Windows' built-in "Auto Logon" feature.
If so, I've got some suggestions for an alternate solution for you:
Logging off users at a specified time
Follow the instructions for Assigning Logon Hours. Old article, but still valid for newer Windows Server OS.
OR
Create a script that logs off all logged on users, then create a scheduled task running as NT AUTHORITY\SYSTEM to execute it at a specified time.
Automatically rebooting and logging on with a specific username/password
Create a batch file called, say, "Enable_AutoLogon_and_Reboot.cmd" and place it in a folder, e.g. "C:\BatchFiles". Contents:
set sUsername=SOMEDOMAIN\Someusername
set sPassword=somepassword
:: Enable autologon on next boot
reg add "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\WinLogon" /v AutoAdminLogon /t REG_SZ /d 1 /f
:: Set autologon to only log on once
reg add "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\WinLogon" /v AutoLogonCount /t REG_DWORD /d 1 /f
:: Set username
reg add "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\WinLogon" /v DefaultUsername /t REG_SZ /d "%sUsername%" /f
:: Set password
reg add "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\WinLogon" /v DefaultPassword /t REG_SZ /d "%sPassword%" /f
:: Reboot
shutdown /r /t 0 /f
Replace the values of the variables at the start with the actual username and password.
Create a scheduled task running as NT AUTHORITY\SYSTEM (e.g. running daily at 06:00):
schtasks /create /tn "\CustomTasks\Enable AutoLogon and Reboot" /tr "%comspec% /c C:\BatchFiles\Enable_AutoLogon_and_Reboot.cmd" /st 06:00 /sc DAILY /ru "NT AUTHORITY\SYSTEM" /f
Upvotes: 0
Reputation: 51
Create a task scheduler to run a vbscript, this task can be triggered while the user logs off from his session by selecting the trigger option "On disconnect from user session" & Any user.
The vbscript which is scheduled on trigger may have the code for remote login on the machine. For the code the below links will help you.
connect to Remote server in vb script
Connecting to a Remote Server on a different domain -- how do I enter the username and password?
Upvotes: 0