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

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.

Related

run serve -s build from .bat 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.

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.

windows batch file not exiting

I am not good at windows scripting. (I am linux guy)
I have to delete files which they are older than n days in two directories.
I am using 2 forfiles command in script. When I run the script manually there is no problem so it's running and finishing its job properly and it takes about 30 seconds to finish
So I created a task to run this batch file everyday in task scheduler
When Task Scheduler executes the script, the script could not end and I could see a running forfiles command in task manager and following day task manager creates a log the script/command is already running. So I am terminating script and run "manually" again, it successfully run and finishes its job in 30-35 seconds again
My script is like that
y:
cd \<path1>
forfiles /s /d -120 /m *.* /c "cmd /c del #path"
cd \<path2>
forfiles /s /d -120 /m *.* /c "cmd /c del #path"
EXIT
How can I run this batch file from task manager properly ?
Thanks
To me it sounds like rights issue. How do you execute your script (directly via start a program or via batch file)? Which windows you are running?
On Windows 7 and Task Scheduler:
Have correct user filled out - who has the rights to run the task and access the files.
Try running your script with checkbox Run with highest privileges checked.
Edit
Stefan is right. If you are running it from within batch file you have to map the network drive with correct credentials:
net use <driver letter> "\\<servername>\<sharename>" /user:<username> <password>
You could do it the following way:
IF NOT EXIST y:\ (
ECHO "The y: drive does not exist. Mapping"
net use y: "\\<servername>\<sharename>" /user:<username>
)

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.

sql server installation errors regarding WMI service

I am trying to install SQL Server 2008 and during the pre requisite check its always fails on
WMI service "Failed".
I went to the event viewer and found this error in there.
"Failed to Initialize WMI Core or Provider SubSystem or Event SubSystem with error number 0x80040154. This could be due to a badly installed version of WMI, WMI repository upgrade failure, insufficient disk space or insufficient memory."
I know there is a lot of space available in my hard drive. Also i tried a few things after googling like WMIFIX.bat file. The file ran fine but did not fix the problem.
Has anyone had this problem? If so do you have a solution?
if you have a domain so Run this Command with administrator Privilege
you can copy this Command and Past in Notbat with .bat extention. and run them.
Rundll32 setupapi,InstallHinfSection Ndi-Steelhead 132 %windir%\inf\netrass.inf
Netsh firewall reset
sc config SharedAccess obj= LocalSystem password= "" type= interact type= own
sc config RpcSs obj= LocalSystem password= "" type= interact type= own
sc config RpcLocator obj= LocalSystem password= "" type= interact type= own
sc config winmgmt obj= LocalSystem password= "" type= interact type= own
sc config Wmi obj= LocalSystem password= "" type= interact type= own
net start winmgmt
net start Wmi
net start RpcSs
net start RpcLocator
net start WmiApSrv
netsh firewall add portopening TCP 135 "Open Port 135"
netsh firewall add portopening TCP 445 "Open Port 445"
netsh firewall add portopening TCP 139 "Open Port 139"
netsh firewall set opmode mode=DISABLE
shutdown /r
if your problem did not solve you Can execute this Command in Command Prompt with admin privilege.. you can just copy and paste them in notepad and rename it with .bat extention an run the file..
net stop winmgmt
C:
cd %systemroot%\system32\wbem
rd /S /Q repository
regsvr32 /s %systemroot%\system32\scecli.dll
regsvr32 /s %systemroot%\system32\userenv.dll
mofcomp cimwin32.mof
mofcomp cimwin32.mfl
mofcomp rsop.mof
mofcomp rsop.mfl
for /f %%s in ('dir /b /s *.dll') do regsvr32 /s %%s
for /f %%s in ('dir /b *.mof') do mofcomp %%s
for /f %%s in ('dir /b *.mfl') do mofcomp %%s
echo DONE reboot
pause
just it and have a good day !!!!!
Run the WMI Diag Utility. Here are the instructions how to do this:
Download WMIDiag.
To run the WMIDiag tool:
1. Open a command prompt window.
(Use "Run As Administrator", if applies to your Operating System version)
2. Navigate to the wmidiag folder that was created when you ran Wmidiag.exe.
3. Type cscript wmidiag.vbs.
View what the output of that is and post what it says. That'll give you a better indication of what's happening.
Here is a reference for the above instructions.
I faced this issue when I tried to install SQL Express
For me, following steps worked out which I referred from http://mikeymurph.me/fix-wmi-service-error/
Run the following in Powershell in Administrator mode
PS C:\Windows\system32> winmgmt /verifyrepository
WMI repository verification failed
Error code: 0x80041002
Facility: WMI
Description: Not found
PS C:\Windows\system32> Winmgmt /resetrepository
WMI repository has been reset
Now try to install SQL Server again.

Resources