Log in to windows by .bat file - batch-file

Here's the situation: I've got .bat file that is being started by windows scheduler every 15 min. However, when system logs off user - actions are not being performed. I'd like to know, if it is possible to add some code to .bat file to make it log in to system again ?

When the user logs out, if the screen goes to a CTRL+ALT+DEL login screen, good-luck-chuck. No one has found a way to force windows to recognize CTRL+ALT+DEL from any programming language. Not even VBScript SENDKEYS will do it. However, if it goes directly to the user login to enter the name and password you can create a small .vbs sendkeys script to auto-login the user prior to running your .bat:
login.vbs
set wshShell = WScript.CreateObject("WSCript.shell")
wshShell.sendkeys "Username"
wscript.sleep 1000
wshShell.sendkeys "{Tab}"
wscript.sleep 1000
wshShell.sendkeys "Password"
wscript.sleep 1000
wshShell.sendkeys "{ENTER}"
WScript.Quit
Throw it into windows scheduler to run even if user is logged off. Or better yet, if you want to have it auto-login the user say 15 minutes after they log out, you can add it to the local Group Policy for that machine:
In RUN type gpedit.msc navigate to Local Computer Policy -> User
Configuration -> Windows Settings -> Scripts (Logon/Logoff) Double
click on Logoff and hit Add... In script name, enter the path of your
.vbs and hit ok. At the beginning of the .vbs file, add
wscript.sleep 900000
That will force it to wait 15 minutes before running the rest of the script automatically when a user logs off.

There is an option in the Tasksheduler wheter to run the task too if the user is logged off. Activate that and it should work fine.

Related

Call webservice from batch file calling in scheduled task

It's several days I search for a problem. I've create an Intranet (Authenticate mode windows allow member of domain) and I a webservice can be called by asmx file
I create a bat file to be use in scheduled task
#echo off
start /MIN iexplore.exe http://server/WS.asmx/Importation
Timeout /T 80 /NoBreak>NUL
taskkill /F /IM iexplore.exe /T
Until now it was good. But from severals days now I don't see the calling asmx correctly (I create a log file and it's empty.
When I call this WebService from my Intranet : it's Ok
I notice now when I run my batch file manually it's ask me a username and password
I look several sample but don't find the good option.
Why Ask me a password when my task have the goods credentials. And How open this asmx with a username/password from scheduler?
Thanks for your help
Finally, I found a way...
In Internet option u have a checkbox "activate the protected mode" uncheck it
And I add my Intranet to Intranet Site
Like this server isn't use directly (only resquest by Excel) and like it's inside protected struture. The risk are very limited.
I hope that can help

Batch file script that will check if the user is idle, then initiate the rest of the script

I wrote a script that will log someone out of the ticketing system I use at my company, is it possible to create a script to check if the user has been idle for about 5 minutes (i.e, no keystrokes or mouse movement) one they're idle initiate the script I wrote to log them out. Once they're logged out the script would need to be restarted. But since there won't constantly be someone on submitting a ticket can you also make so it only reactivates the idle timer once someone initiates another keystroke/mouse movement?
This is my script:
#echo off
Taskkill /im firefox.exe
CD C:\Users\username\AppData\Roaming\Mozilla\Firefox\Profiles\profilename
del *"cookies.sqlite"*
CD C:\Program Files (x86)\Mozilla Firefox
start firefox.exe http://sysaid.sdairy.com/Login.jsp
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.AppActivate("Mozilla Firefox")
WshShell.SendKeys "{ALT}", "{TAB}"
(This just closes the browser, deletes the cookies then restarts the browser in the webpage I need and alt tabs to make sure that the browser window is in front of the desktop).
Let me know if you need more information please!
Thank you in advance.

Override Lync Status using Lync SDK

I am developing a wpf application using Lync SDK that sits on a windows 2012R2 server which I access using a remote desktop session. I am trying to keep the lync account logged in to be always available, even when there is no user logged into the computer.
I set a timer to tick every 15 seconds that runs this code:
if (client.State == ClientState.SignedIn)
{
counter++;
Console.WriteLine("Account is green: " + counter);
Dictionary<PublishableContactInformationType, object> status = new Dictionary<PublishableContactInformationType, object>();
status.Add(PublishableContactInformationType.Availability, Microsoft.Lync.Controls.ContactAvailability.Free);
client.Self.BeginPublishContactInformation(status, PublicationCallback, time);
Console.WriteLine(client.Self.Contact.GetContactInformation(ContactInformationType.Activity));
}
While this does set the status of the user to be Available when someone is logged in (even if no one is using the computer), whenever I disconnect from the remote desktop session (and the server locks but is still running), the status goes to away.
Is there some way for me to get it to ignore if the computer is locked or not?
Having it ignore locking was solved by a different solution entirely which requires two different things:
Firstly, instead of just closing my remote desktop session, I had to use a command prompt script to leave. Type this into your command prompt in order to log off:
tscon [YOUR SESSION ID] /DEST:console
You can find your session ID by looking into your Windows Task Manager's User page (it might be hidden so you have to display it).
Secondly, you have to have your keyboard occasionally move. There are many scripts that do it but I found this that I liked because it toggles the numlock making the computer still usable when triggered.
Dim objResult
Set objShell = WScript.CreateObject("WScript.Shell")
i = 0
Do While i = 0
objResult = objShell.sendkeys("{NUMLOCK}{NUMLOCK}")
Wscript.Sleep (6000)
Loop
Save this in a .vbs file and run it before you leave your computer. This will prevent any sleep triggered by lack of use that might be set.

Deleting files within a directory through remote 3rd party service

I am trying to use a service called LogMeIn to delete folders within a user profile's directory. When run locally, this obviously works. Very simple, as I am fairly new to batch files.
pushd %homepath%\Tracing
Echo Y | DEL *.*
Now, where I run into the problem is when I run it through LogMeIn. This service is for deployment and sends it to all the machines linked to it. It has me put in credentials that will work on all of the machines (network admin) so it can deploy the batch file and there is no way to work around it. Instead of going to C:\Users\tirefire, it now goes to C:\Users\Admin. Any thoughts as how to efficiently navigate to the desired user profile that is currently logged in? Every system is identical. Windows 7 Service Pack 1.
Couldn't find anything online regarding this issue directly. Thanks in advance!
Logmein will only log onto the profile that you have setup to access that computer. For example if your credentials are admin/pass you will get the admin profile, for tirefire/pass you will log on as TireFire.
Prior to logging in you will not be able to see who is logged in.
This is not the answer your looking for but it is the limitation of the software.
You could use a batch like:
CD..
FOR /D %%P IN (*.*) DO DEL /S/Q %%P\TRACING\*.*
This will look in the folders under c:\users (/D) and assign all user folders to the variable (%%P) and delete (DO DEL) anything in the tracing folder (%%P\TRACING*.*) for all users on the machine.
Source: I use logmein daily to log onto machines that we service.

login to remote using "mstsc /admin" with password

I want to use mstsc /admin to login to a server silently. My batch file reads the code as
mstsc /v:xxx.xxx.xxx.xxx /admin
But it ask me to enter password. Can anyone help me to skip this step?
I went on to Google and found this site. But I am very new to this stuff (scripting) and could not understand what to do with given code. Is it a vbscript file? Can I do the same thing with a batch file. Please elaborate and I request you to spoon feed. This is not my arena but still I am pushed to fight without weapons.
My basic need is to kick off all users from a remote desktop except mine to perform some maintainable work. Thanks.
Re-posted as an answer:
Found an alternative (Tested in Win8):
cmdkey /generic:"<server>" /user:"<user>" /pass:"<pass>"
Run that and if you run:
mstsc /v:<server>
You should not get an authentication prompt.
the command posted by Milad and Sandy did not work for me with mstsc. i had to add TERMSRV to the /generic switch. i found this information here: https://gist.github.com/jdforsythe/48a022ee22c8ec912b7e
cmdkey /generic:TERMSRV/<server> /user:<username> /pass:<password>
i could then use mstsc /v:<server> without getting prompted for the login.
to be secured, you should execute 3 commands :
cmdkey /generic:"server-address" /user:"username" /pass:"password"
mstsc /v:server-address
cmdkey /delete:server-address
first command to save the credential
second command to open remote desktop
and the third command to delete the credential for security reason
all of these commands can be saved in a batch file(bat).
(if you save these command in a batch file, third command will not be executed until you close the remote desk)
Same problem but #Angelo answer didn't work for me, because I'm using same server with different credentials. I used the approach below and tested it on Windows 10.
cmdkey /add:server01 /user:<username> /pass:<password>
Then used mstsc /v:server01 to connect to the server.
The point is to use names instead of ip addresses to avoid conflict between credentials. If you don't have a DNS server locally accessible try c:\windows\system32\drivers\etc\hosts file.
It became a popular question and I got a notification. I am sorry, I forgot to answer before which I should have done. I solved it long back.
net use \\10.100.110.120\C$ MyPassword /user:domain\username /persistent:Yes
Run it in a batch file and you should get what you are looking for.
Save your username, password and sever name in an RDP file and run the RDP file from your script
The Command mentioned above didn't work for me (Windows 11)
cmdkey /generic:"server-address" /user:"username" /pass:"password"
When i listed the cmdkeys using the commad:
cmdkey /list
I saw, that a legacy target was added. Adding a specific TERMSRV-Target worked for me:
cmdkey /add:Domain:target=TERMSRV/<SERVER> /user:<DOMAIN>\<USER> /pass:<PASSWORD>

Resources