run serve -s build from .bat file - batch-file

I want to execute serve -s build from .bat file as admin, this is my .bat file:
#ECHO OFF
cd "C:\Users\user\Desktop\Brabender\build-20220517T114506Z-001"
serve -s build
It works if I manually run it as admin, but I need it to run from Windows Startup Folder everytime the system starts, any ideas how to do it as admin automatically?

Some minor changes to your batch file, adding /d to the cd command
#echo off
cd /d "C:\Users\user\Desktop\Brabender\build-20220517T114506Z-001"
serve -s build
Assuming we call the file something like serve-build-script.bat, run from cmd.exe
schtasks /create /tn "Serve-Build" /tr "c:\<Path-to-Batch-file>\batchfile-name.bat" /sc onstart /ru "user" /rp "password" /np /f
The correct version updated as per confirmation from the chat.

Related

Using mysql.exe in .bat file from Network Path [duplicate]

I want to execute a batch file
D:\apache-tomcat-6.0.20\apache-tomcat-7.0.30\bin\shutdown.bat
Which is on my server inidsoasrv01.
How should I write my .bat file?
Use microsoft's tool for remote commands executions: PsExec
If there isn't your bat-file on remote host, copy it first. For example:
copy D:\apache-tomcat-6.0.20\apache-tomcat-7.0.30\bin\shutdown.bat \\RemoteServerNameOrIP\d$\apache-tomcat-6.0.20\apache-tomcat-7.0.30\bin\
And then execute:
psexec \\RemoteServerNameOrIP d:\apache-tomcat-6.0.20\apache-tomcat-7.0.30\bin\shutdown.bat
Note: filepath for psexec is path to file on remote server, not your local.
You can use WMIC or SCHTASKS (which means no third party software is needed):
SCHTASKS:
SCHTASKS /s remote_machine /U username /P password /create /tn "On demand demo" /tr "C:\some.bat" /sc ONCE /sd 01/01/1910 /st 00:00
SCHTASKS /s remote_machine /U username /P password /run /TN "On demand demo"
WMIC (wmic will return the pid of the started process)
WMIC /NODE:"remote_machine" /user:user /password:password process call create "c:\some.bat","c:\exec_dir"
If you are in same WORKGROUP shutdown.exe /s /m \\<target-computer-name> should be enough shutdown /? for more, otherwise you need software to connect and control the target server.
UPDATE:
Seems shutdown.bat here is for shutting down apache-tomcat.
So, you might be interested to psexec or PuTTY: A Free Telnet/SSH Client
As native solution could be wmic
Example:
wmic /node:<target-computer-name> process call create "cmd.exe c:\\somefolder\\batch.bat"
In your example should be:
wmic /node:inidsoasrv01 process call create ^
"cmd.exe D:\\apache-tomcat-6.0.20\\apache-tomcat-7.0.30\\bin\\shutdown.bat"
wmic /? and wmic /node /? for more
With all the new security updates from Microsoft in the latest operating systems it is becoming more and more difficult to connect and execute scripts remotely. PsExec is one tool that helps you to connect a windows host from another windows host and execute command(s) or a script. Limitation of this tool is that it will execute the command(s) or a script, but it will not print the execution details. It will only return the process id.
C:\apps\tools\psexec \\%RemoteHostName% -u %Domain%\%userName% -p %userPassword% -accepteula -d -h -i 1 cmd.exe /c "cd C:\apps\test\ & echo Hello World" & call C:\apps\test\script.bat
While I would recommend against this.
But you can use shutdown as client if the target machine has remote shutdown enabled and is in the same workgroup.
Example:
shutdown.exe /s /m \\<target-computer-name> /t 00
replacing <target-computer-name> with the URI for the target machine,
Otherwise, if you want to trigger this through Apache, you'll need to configure the batch script as a CGI script by putting AddHandler cgi-script .bat and Options +ExecCGI into either a local .htaccess file or in the main configuration for your Apache install.
Then you can just call the .bat file containing the shutdown.exe command from your browser.

New to scripting

I have two commands I have to run in CMD. I also need to run them remotely. I will add the .bat file to the C:\ of the remote computer as well. This is actually a fix for Windows 7 to 10 1809 migrations that breaks protected view in Office if anyone wants to know.
icacls "C:\Windows\SysWOW64\msvcp140.dll" /grant *S-1-15-2-1:(OI)(CI)RX
icacls "C:\Windows\SysWOW64\vcruntime140.dll" /grant *S-1-15-2-1:(OI)(CI)RX
This is what I came up with as being my first attempt at creating a bat file on my own. I am so new to this it's almost embarrassing but you have to start somewhere.
c:\
cd C:\Windows\SysWOW64
start cmd.exe /k icacls "C:\Windows\SysWOW64\msvcp140.dll" /grant *S-1-15-2-1:(OI)(CI)RX
start cmd.exe /c icacls "C:\Windows\SysWOW64\vcruntime140.dll" /grant *S-1-15-2-1:(OI)(CI)RX
pause
This also has to be run as admin.
One solution I can think of is for you to use PsExec from the SysInternals Suite. You mentioned trying to run command line commands from a remote location as admin, so this is why I am suggesting it. You can use it to run the icacls command or other commands remotely.
Here are some links to some documentation about PsExec:
Microsoft Documentation
ss64 Documentation
And a link to an artice written by Mark Russinovich (Sysinternals Creator) on using PsExec:
PsExec Article
Now for the usage for your specific need:
psexec -accepteula \\[IP Address or Hostname] -u [domain\username] -p [Password] cmd.exe
Example with the options filled in:
psexec -accepteula \\192.168.1.3 -u win7\bob -p P#ssword cmd.exe
This command will will allow you to open a remote command shell on the machine you want to execute the icacls commands. From there, you just run the icacls commands as if you were sitting at the remote machine. I recommend reading up in the links provided to understand what each switch does.
Or you can try this, which combines the above with your icacls commands and executes it all at once. I have not tested the command below, but it SHOULD work. This is all one command by the way, so make sure it is copied as one line.
psexec -accepteula \\[IP Address or Hostname] -u [User] -p [Password] cmd /c "icacls "C:\Windows\SysWOW64\msvcp140.dll" /grant *S-1-15-2-1:(OI)(CI)RX & icacls "C:\Windows\SysWOW64\vcruntime140.dll" /grant *S-1-15-2-1:(OI)(CI)RX"
I know this isn't a batch script, but it could help accomplish what you want to do.

Start command does not work when running bat-file via psexec

Met same problem:
using cmd's Start command to run exe. Works locally, but not remotely
But solution does not work for me - I use full path to *.exe file
The task is install copy application installer from my computer (Win7, x64) to remote desktop (Win10, x64) with checking the installer version is newer than installed.
So, I have two *.bat, one running from another via psexec.
Part of the first .bat below. Here copy installer to remote
NET use x: \\%compname%\%sharefolder% /user:%login% %pass%
xcopy %InstallerFolder%\%InstallerFile% x:\ /s /e /d /y
NET use x: /delete
Then run InstallProcess.bat:
%InstallerFolder%\PsExec.exe \\%compname% -f -u %login% -p %pass% /c %InstallerFolder%\InstallProcess.bat
Also tried PsExec64.exe
All is fine except start command:
START "" /WAIT C:\%sharefolder%\%InstallerFile% /DIR="C:\Program Files (x86)\MyApp\" /sp- ^
/verysilent ^
/suppressmsgboxes ^
/closeapplications ^
/components="..." ^
/tasks="..." ^
/log="%~n0.log"
It does not work. Just like without this command. InstallProcess.bat error code = 0.
But if i run InstallProcess.bat locally on %compname% it works fine.
Thanks!
Solved. Missing -s. Thanks for help.
%InstallerFolder%\PsExec.exe -s \\%%c -f -u %%a -p %%b -c %InstallerFolder%\InstallProcess.bat

Execute a batch file on a remote PC using a batch file on local PC

I want to execute a batch file
D:\apache-tomcat-6.0.20\apache-tomcat-7.0.30\bin\shutdown.bat
Which is on my server inidsoasrv01.
How should I write my .bat file?
Use microsoft's tool for remote commands executions: PsExec
If there isn't your bat-file on remote host, copy it first. For example:
copy D:\apache-tomcat-6.0.20\apache-tomcat-7.0.30\bin\shutdown.bat \\RemoteServerNameOrIP\d$\apache-tomcat-6.0.20\apache-tomcat-7.0.30\bin\
And then execute:
psexec \\RemoteServerNameOrIP d:\apache-tomcat-6.0.20\apache-tomcat-7.0.30\bin\shutdown.bat
Note: filepath for psexec is path to file on remote server, not your local.
You can use WMIC or SCHTASKS (which means no third party software is needed):
SCHTASKS:
SCHTASKS /s remote_machine /U username /P password /create /tn "On demand demo" /tr "C:\some.bat" /sc ONCE /sd 01/01/1910 /st 00:00
SCHTASKS /s remote_machine /U username /P password /run /TN "On demand demo"
WMIC (wmic will return the pid of the started process)
WMIC /NODE:"remote_machine" /user:user /password:password process call create "c:\some.bat","c:\exec_dir"
If you are in same WORKGROUP shutdown.exe /s /m \\<target-computer-name> should be enough shutdown /? for more, otherwise you need software to connect and control the target server.
UPDATE:
Seems shutdown.bat here is for shutting down apache-tomcat.
So, you might be interested to psexec or PuTTY: A Free Telnet/SSH Client
As native solution could be wmic
Example:
wmic /node:<target-computer-name> process call create "cmd.exe c:\\somefolder\\batch.bat"
In your example should be:
wmic /node:inidsoasrv01 process call create ^
"cmd.exe D:\\apache-tomcat-6.0.20\\apache-tomcat-7.0.30\\bin\\shutdown.bat"
wmic /? and wmic /node /? for more
With all the new security updates from Microsoft in the latest operating systems it is becoming more and more difficult to connect and execute scripts remotely. PsExec is one tool that helps you to connect a windows host from another windows host and execute command(s) or a script. Limitation of this tool is that it will execute the command(s) or a script, but it will not print the execution details. It will only return the process id.
C:\apps\tools\psexec \\%RemoteHostName% -u %Domain%\%userName% -p %userPassword% -accepteula -d -h -i 1 cmd.exe /c "cd C:\apps\test\ & echo Hello World" & call C:\apps\test\script.bat
While I would recommend against this.
But you can use shutdown as client if the target machine has remote shutdown enabled and is in the same workgroup.
Example:
shutdown.exe /s /m \\<target-computer-name> /t 00
replacing <target-computer-name> with the URI for the target machine,
Otherwise, if you want to trigger this through Apache, you'll need to configure the batch script as a CGI script by putting AddHandler cgi-script .bat and Options +ExecCGI into either a local .htaccess file or in the main configuration for your Apache install.
Then you can just call the .bat file containing the shutdown.exe command from your browser.

Multiple commands in batch file

I have a batch file that I have scheduled to run in Task Scheduler, but it doesn't seem to run the second command "forfiles" when the Task runs. Here is my batch file:
sqlcmd -S WEB01\SQLEXPRESS -U username -P password -i "D:\BackupPrograms\cslogsbackup.sql"
forfiles /P "Z:\Logs" /S /M *.bak /D -7 /C "cmd /c del #file"
Is there something that needs to go between the sqlcmd and the forfiles lines?
If I click on the batch file and run it manually it works fine, just does not delete the older files in the "forfiles" line when the batch files runs in the Task Scheduler.

Resources