Issue running Net cmd but with no error (BAT file) - batch-file

I am trying to create a bat file that will run cmd as administrator, then execute net command as shown below:
runas /user:administrator cmd "net localgroup administrators mjordan /add"
The goal is to add whoever the current user is as a local administrator.
So the first part works like a charm, and asks for the admin password. Upon entering the admin PW however, I do not see said test user under the local admin group. My best guess is I made a syntax error. But oddly enough no errors show up and command-line exits as if it executed.
Also, how would I make this execute and add the current user to the local admin group (thinking in a %username% way)? Not sure I am using the proper terminology, but say the user logged on running this command is JSmith. How could I make it add without using his name so it works on any account rather than just JSmith
Do you guys notice any errors? ...I am fairly new to creating bat files, and am learning as much as I can, so i know i must have messed up somewhere. Also, any references or study help is appreciated! Thank you!

Related

How to run a batch file with different built-in credentials?

I want to put the following into a batch file so i can convert it to a .exe and allow another user to run it on another PC.
The user does not have local admin access and has a frequent problem with their com1 port. I have installed/copied devcon to the user's PC and have gotten this script working fine without the credential change, i just need to know how i can run the batch file as a local user.
This is what i have written:
runas.exe /profile /user:localpcname\administrator /password:12345678
Devcon disable "ACPI\PNP0501"
timeout /t 5
Devcon enable "ACPI\PNP0501"
"Runas" seems to always want to run something else instead of everything else in a batch file?
G'day Regejok, thank you very much that works perfectly. Yes i am aware of the security implications of an open text password in a batch file, and i am aware i could make the user a local administrator. However doing that would be a lot more difficult than this solution (GPO's manage local users and they are kept overseas) and i don't want to confuse the user(s) any more than i need to.
Add in the fact that the equipment attached to this one PC is flakey at the best of times has led me here.
I really like the fact that i can run additional commands within the same batch file by using the /c switch. I don't know much at all about scripting so this is a great help :)
It looks like the ampersands are used in a similar was as brackets are when writing/typing too: devcon do-this "blah"(and then do this)Devcon do-that "blah again"
I understand.

How to make batch file run from group policy?

I am trying to make a batch file that calls an executable named idlelogoff after a certain amount of idle time. I can see the process in task manager however the computer doesn't sign out.
However when I run the process as an administrator manually it works. I thought the system account was supposed to have all privileges. Very confused as to why this isn't working. Any help would be appreciated.
Below is my batch file contents.
start "IdleLogOff" /d C:\ /b idlelogoff.exe 10 logoff
I found an answer to this by using local group policy instead of domain policy . I used user configuration->windows settings-> and then logon scripts and had it run on an user logon. The script works from here but did not work from domain group policy for whatever reason
look into taskmanager- i suppose that the process runs under system-account when using domain-gpo- no matter if activated/linked in user or workstation context.
So the exe would check if the system-account is idle.
A solution could be putting the exe into autostart-folder or create a run-key into registry or with an scheduled task -> all can be done with a gpo

Task scheduler cannot open batch file when set to run whether user is logged on or not

OS: Windows Server 2008 R2 Enterprise
I am unable to get the Task Scheduler to run .bat files while I am logged off. I have a a production .bat file I want to use, but for my troubleshooting purposes I am using C:\Users\Administrator\Desktop\test.bat
test.bat is very simple. All it contains is:
taskkill /im notepad.exe
It runs successfully on its own. If I have Notepad open and I double-click test.bat then the cmd window flashes and Notepad dies. The .bat file is not the problem.
The task I created in Task Scheduler is "Test taskkill" (without the quotes). I can see its location in Windows Explorer: C:\Windows\System32\Tasks\Test taskkill
If the task is set to run only when the user is logged on then the task works. With this setting, I can right-click on it > run and I get the same behavior as if I double-click the .bat file. That means the task is configured correctly.
If I set it to run whether or not the user is logged on then when I right-click > run nothing obvious happens. The job is reported as having been run successfully in the history, but Notepad survives. I know that I am using the proper credentials for the account that I am configuring to run the task. That account is the local administrator.
This issue appears similar to issues other have had in the past:
Running a .bat file in Scheduled Task
http://social.technet.microsoft.com/Forums/windowsserver/en-US/d47d116e-10b9-44f0-9a30-7406c86c2fbe/scheduled-task-wont-run-bat-file?forum=winservermanager
The thread in the second link seemed to resolve an identical issue for many people with the suggestion that the account used to run the task requires explicit permission to the .bat file and all files that the .bat file modifies. This was very promising (if an annoying requirement). However, I have confirmed or assigned explicit permissions for the local administrator to the following areas, and I am still not successful:
C:\Users\Administrator\Desktop\
C:\Users\Administrator\Desktop\test.bat
C:\Windows\System32\Tasks\
C:\Windows\System32\Tasks\Test taskkill
Additionally, I have confirmed that the local administrator account is in the local administrators group.
Am I missing some other permissions that needs to be set? Is there something else I should be looking at? Thanks!
I have resolved this problem with help from a contractor who was doing some other work for my institution.
In the properties window for the task, on the Action tab, when creating or editing the action that opens the batch file (in my case test.bat), there is the "Program/script:" field and there is also a "Start in (optional):" field. I previously had the "Program/script:" field containing the full path to the file, ending with the file name, and I had the "Start in (optional):" field null. This configuration appears to work without issue when the task is set to "Run only when user is logged on" in the General tab of the task's properties window. However, this configuration does not work when the task is set to "Run whether user is logged on or not".
In order to resolve the problem, I changed the action so that the "Program/script:" field contained only the file name, not the file path. I put the file path in the "Start in (optional):" field. This configuration works when the task is set to "Run only when user is logged on"! I have tested with the task running on a trigger and also with running it manually.
Not sure if this helps. My issue was using a interactive program (in particular Excel) as well.
Creating the 2 empty folders (Desktop) solved it. Create both even if you are running on x64.
C:\Windows\System32\config\systemprofile\Desktop
C:\Windows\SysWOW64\config\systemprofile\Desktop
I debugged my code and realised the code exited at this line
ExcelFile excelFile = new ExcelFile(directory, filename);
I read this solution somewhere and I cannot find the URL now.
Try this as well..
Resolved the "Run whether user is logged on or not" by setting the “Log on as Batch Job” security policy.
Here is details on the setting… https://danblee.com/log-on-as-batch-job-rights-for-task-scheduler/
You may take another approach, but here is the steps…
1) I updated the default domain group policy by adding my admin account (that I fire the task with) to the “Log on as Batch Job” security policy. (Even though ADMIN group is listed, and I am using an admin account)
2) I forced GPUPDATE on the client
3) Rebooted the client
4) Looked at the policy on the client to make sure it made it over from the DC
5) Set the task "Run whether user is logged on or not"
6) Ran the task on demand and it worked creating a PDF file.
7) Logged out and ran the task at scheduled time and it worked creating a PDF file.
I had similar problem, but it was related to the fact that there was a space in the path of my executable batch file. I have removed the space and now it works fine.
What work for me was make sure that the path is available.
i used net use Z: \computername\folder /persistent:yes
then the xxcopy /s /c /d /e /i /y D:*.* z:\BackupBat
I notice all my other .bat work fine only the one with the reference to a map folder was the one no running, so that need to be the problem. with these change the bat file runs good.
Verifying the following has worked for me:
1) 'Run if user is not logged in' option in Task Scheduler does not work for programs that is interactive mode. Example notepad, clock. Tasks will not run interactively. Make sure your program does not requires any interactive actions.
2)Check the “Start in (optional) “ If your program path is not one of the system paths. You have better to add you program path in it. On the "Program/Script" enter only the name of the program, and enter the path of your script in "Start in".
3)The user account you setup for your program must have permission not only to run your program itself and but also to run all other program that related to your program.
Here is a sample program:
On C:\myprogram\folderOne\test.cmd
date /t >> c:\Temp\testAuto.log
date /t >> c:\temp\testAuto.log
echo ----->> c:\temp\testAuto.log
cls
exit
-Create a Tasks, selecting on General>SecurityOptions "Run whether user is logged on or not"
-Enter on Action>Program/script: test.cmd
on "Start in" C:\myprogram\folderOne
-Run, you might see the cmd windows open and closes very quickly. The testAuto.log file should be in c:\tmp folder.
It might be because you don't have permission. I was facing the same problem and i found the solution like this -
Open Task Scheduler right click on your task and than select properties option.
In properties click on General tab and then click on 'User Group or User' and select the appropriate user.

Why doesn't my batch file execute automatically despite being placed in "Startup" folder?

This must sound too naive because I am not a programmer. I need help with a batch file execution.
There are two user accounts on my Windows XP enabled PC and one of these accounts is used by my brother. Sometimes I need to leave messages for him to read as a reminder of something, for example, to back up his personal folders when he logs in next time.
So, I created a simple batch file with the following code:
#echo off
msg * It's time to backup your folders!
When executed by double clicking, the file works fine and shows the expected message. I placed a copy of the batch file in "C:\Documents and Settings\Brother\Start Menu\Programs\Startup" but couldn't make it execute automatically when my brother logged in.
Could somebody please show me where I went wrong?
According to this answer on Superuser, the startup folders for XP are actually in
%AllUsersProfile%\Start Menu\Startup
%UserProfile%\Start Menu\Startup
So the Programs\Startup may be used for something else.
If you are using Windows 10, there are 2 things that may be doing this:
Your file needs Administrator privileges (When i copy any file to startup with Admin privileges it will not work). - My experience
When you boot your computer wait a bit (30 - 50 seconds) to load all files make sure to don't do anything.
This is my experience in Windows 10 please correct me if im wrong in anything

How to find the username in application launched as Administrator

I have a problem today. I have a program that starts with Windows as Administrator. The probleme is I want to retreive the current UserName of the current user and getUserNameEx (NameDisplay) give me the Administrator NameDisplay...
I have also try to retreive this information by browsing Windows Logon Sessions but I have about 16 sessions each time and it'is difficult to know the good one.
Thanks you very much,
JM.
The program launched as Administrator will still run in the user's logon session, so you can enumerate the logon sessions until you find the matching one and then obtain the user name from that.
I've seen suggestions that you can do this using WMI - you enumerate Win32_Process instances and then call the GetOwner method to find who is the owner of 'explorer.exe'.
Not sure how well this would work in Terminal Services configurations though. You'd also have to make sure that explorer.exe is really the system process and not some other local program that just happens to have the same EXE name.
You should still be able to tell who the 'owning' user is - I did a little test here: Logged into a Win2k8 terminal server with my non-admin account and launched a command prompt (cmd.exe) as my admin user.
Using Process Explorer to get details on each process, I could see the User associated with the cmd.exe process was my admin account, the Parent process was explorer.exe (PID 5008), and when I look up the User associated with PID 5008, it was my non-admin account.
If you can replicate this in your code you should be able to track down who started any given process running as your administrator account.

Resources