Runas command to read user details from a txt file - batch-file

having an issue where a batch script of mine is changing time server REG Keys but requires admin privildges to do so, even if local user is admin.
My way around this i have found is using this line of code:
runas /noprofile /user:administrator\administrator "%UserProfile%\Desktop\Time Server.bat"
But this asks me for the admin password everytime, and i cant be doing this for 150+ users.
Can i get this command to read user details from a .txt file or .ini file?
Thank you in advanced.
OliV555

AFAIK this is not possible out of security reasons. I've spent a lot of time on this but at the end, I had to use a workaround:
Create a new task with the windows scheduler. In the interface select the admin user you want to run your script with and enter the password. Check the option to run the task with highest privileges. Let your task execute the batch file with all the code you want to run as admin. Don't enter any trigger to start your task automatically.
Now here's the trick:
To run your command with admin privileges simply execute
SCHTASKS /Run /TN <taskname>
If your local user is admin this will work straight on. Otherwise, you'll have to grant the user rights to start tasks.
EDIT: The scheduler will execute your batch file with system32 as working directory. So if you are using any relative paths in your script don't forget to adjust them or replace them with absolute paths.

Related

I am in need of a batch script that will re-map a network drive

I am in need of a script that will re-map a network drive. I will also need it to be able to enter admin credentials when prompted. The reason I need this script is that I don't want the user to know the password. Currently they have to call me and start a remote session so I can enter the password. This happens about every other day wasting time of both myself and my client. I have found a script that maps the drive (below) but lack the knowledge on how to have it enter the admin credentials.
#echo off
net use S: \Server\Sales
Thanks yall!
Open a command prompt window and enter net use /? to get help of this command or use the information provided by Microsoft on pages about
net use (best)
net use
net use
As you can read, a password as well as the user credentials can be appended also on the command line. I would have expected that an administrator knows all about that.
The batch file could contain:
#echo off
net use S: \\Server\Sales password /user:yourdomain.com\adminusername /persistent:no
But it is very unsecure to store the password and the user's / administrator's name in a batch file.
It would be definitely better to change the permissions on the shared folder so that those users who need access to this folder read-only or with write permission have access without mapping the shared folder using administrator credentials.
By the way: What is displayed in square brackets in help output is optional. This is a general rule for any Windows command on which help is output by running this command with /? as parameter.

Unable to access pscp.exe with a windows service account

I have built a windows batch file code that fetches the list of files from an FTP server and then pulls the appropriate file from that list. The batch file uses a combination of pscp.exe and psftp.exe in order to achieve this.
If I login as a member of the administrator group and execute this batch file manually (double clicking) it works perfectly, but the problem arises when I schedule a windows task to run that batch file from a windows service account (which is not in the administrator group).
The job when running under the windows service account is unable to execute the command for pscp.exe used in the code, whereas it executes everything else prior to that in the batch file perfectly normal.
Below is the line of code that I have used in my batch file :
pscp.exe -sftp -v -batch -pw %password% -ls %username%#ServerAddress:/outbox > F:\dirlist.txt
The service account is a member of BatchAccountsLG and ServiceAccountsLG groups, which is needed for the job to be able to execute the batch file code. Also, the service account has all the required access on any of the files/folders needed in the execution of the code.
I am not sure what the problem is because the batch file works perfectly fine if use my admin credentials, whereas it fails when the job is scheduled with the windows service account.
I have tried looking for an answer through google, but I have drawn a blank, any help on this would be highly appreciated. If there is anything else that I need to add to this, I would be more than glad to do so.
Thanks to all the ideas in advance.

Running a bat file from another

this is the structure of a batch file
SET CURDATE=%date%
REM run some code bellow
.....
.....
.....
.....
"C:\CADMAC2k\SYS\CADMAC.exe"
this batch file reads the current date, does some modifications and then starts cadmac.exe (a cad program).
this batch file needs to be run as administrator right, but cadmac.exe should be run as current logged-in account.
when I right click this batch file and select run as administrator, cadmac.exe is run with admin rights too.
Is there any way to make cadmac.exe run with current user rights (logged in account) not administrator rights?
any kind of advice is much appreciated.
If there are specific commands in your batch file that need to be run as Administrator you can use the runas command.
Then, you can start the batch file as the logged-in user. runas prompts for Administrator credentials when necessary.
You can use Run AS for exe.
Here is possible solution
Running .exe's with Run As in a .bat file

Batch runs manually but not in scheduled task

I don't generally write batches, but I currently have a batch that uses forfiles to copy my FLVs from one folder to another. When I run the batch manually it works every time, but from a scheduled task, it throws a (0x1) error.
forfiles -p "C:\Program Files\Adobe\Flash Media Server 4.5\applications\name\streams" -m *.flv -s -d -1 -c "cmd /c copy #file ^0x22C:\Program^ Files\Adobe\Flash^ Media^ Server^ 4.5\applications\name\output\"
Not sure what syntax the scheduled task doesn't like.
Update
Under my scheduled task, Actions I have the following:
Program/script: name.bat
Start in (optional): \\servername\file\to\batch
Hi might be this is helpful,
I also face the same issue.
Just set the startin path like:
Here start in path is the path of batch file:
like you have enter in program script:
"E:\program related files\demo.bat"
then in startin just pass:
E:\program related files & done!
When my Start in (optional): path was a UNC path, it wouldn't work. So I moved my batch on the server and everything worked correctly.
UNC in Windows Batch Files
forfiles with UNC path
On Server 2008 R2 when running the batch file under domain user credentials, with confirmed "log on as a batch job" security in the Local Security Policy>Local Settings>User Rights Assignment,
even then my batch (copying a log file to a network share) would not run as scheduled task, until I selected in tab General the option "Run with highest privileges" (default NOT checked!)
The option Run whether user is logged on or not was also selected, with radio-button, but I guess this is quite standard, when selecting to run the task using a domain user account.
For the tab Actions : specifying the entire batch file name including its path, directly in "Program/script:" works fine (with Server 2008 R2)
Using double quotes inside the batch file causes no problems.
See the screen shot bellow.
You need to change the user to system
Most common reason for such problems is permissions: scheduled tasks does NOT always run with your user credentials. If you want scheduled task to run as you you will have to set it up as you or alternative user.
Besides that I hope that your line of code is a content of your batch file, you are not trying to run this command directly. Or are you?
P.S. What are these ^0x22 and ^ doing in your code?
I know this is an old question, but just wanted to share some info.
The (0x01) error code can also refer to resources that are not found. Therefore:
all files/folders referd from within the batch file should be accessible to the user which account is being used to run that scheduled task;
pay attention when using network locations in combination with "Run whether user is logged on or not" option;
the above-mentioned option can be tricky to use because some resources may be available only after log on.
For .bat files to run inside your scheduled task, you need to specify your .bat file path inside the start option - despite the fact that your .bat file is at the same directory as your .exe. Also, I flagged it to run with highest privilege. After I have done those two things, the task suddenly takes off without any problem!

How can I use a batch file to launch an application as a specific user?

I'm trying to figure out how I can use a batch file to launch Internet Explorer as a local user on the system hosting the batch file. Ideally I'd like to be able to double click the batch file and not have to enter user credentials. To break it down a bit:
User is logged onto the host system with a domain user (Server 2008 R2 Standard 64bit)
User can then double click on a batch file that will launch Internet Explorer as a local user on the host system, without prompting for the local user credentials
All I can find is information about NET USE, any help would be greatly appreciated!
"runas" command was intentionally designed to prompt for password. So, that is not your solution.
One of option is to create a scheduled task where you can store the password (safely) and run the scheduled task from batch file.
But if the batch file is distributable and not going to be run on same machine.. you may have to investigate on how to create a schedule task from batch file and then run it.
you may find this reference helpful

Resources