How to run multiple commands in one batch file? - windows

I want to run multiple commands in one batch file.
I tried the &, &&, start, /wait, call, :begin and goto begin commands but no luck.
Here are my commands:
C:\Windows\System32\cmd.exe /k %windir%\System32\reg.exe ADD HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System /v EnableLUA /t REG_DWORD /d 0 /f
sc config remoteregistry start= auto
sc start remoteregistry
sc config Schedule start=auto
sc start Schedule
sc stop McAfeeFramework
sc configure McAfeeFramework startup= disabled
sc stop McShield
sc configure McShield startup= disabled
sc stop McTaskManager
sc configure McTaskManager startup= disabled
netsh advfirewall set AllProfiles state off
sc stop MpsSvc
sc config MpsSvc start= disabled

C:\Windows\System32\cmd.exe /k <command>
starts a new cmd context where is executed, but /k keeps that new context open. You want to close it after executing , so further commands from the original context can be executed. Use /c instead of /k to do so.
described in cmd /?

Well, batch scripts already do it by default, but i guess that your usage of /K on cmd.exe it was unnecessary and harmful, and A / C could have done the job even though, remembering, it is not necessary.
File1.bat*
C:\Windows\System32\cmd.exe /c %windir%\System32\reg.exe ADD HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System /v EnableLUA /t REG_DWORD /d 0 /f
sc config remoteregistry start= auto
sc start remoteregistry
sc config Schedule start=auto
sc start Schedule
sc stop McAfeeFramework
sc configure McAfeeFramework startup= disabled
sc stop McShield
sc configure McShield startup= disabled
sc stop McTaskManager
sc configure McTaskManager startup= disabled
netsh advfirewall set AllProfiles state off
sc stop MpsSvc
sc config MpsSvc start= disabled

Related

Run windows system server in golang

I want to use netsh in my app ,the netsh depend some windows server,so i write the bat file ,before i run app i will start these system server
bat file
regsvr32.exe /s C:\Windows\system32\Dhcp.dll
regsvr32.exe /s C:\Windows\system32\RpcSs.dll
regsvr32.exe /s C:\Windows\system32\nsi.dll
regsvr32.exe /s C:\Windows\system32\NetTcpPortSharing.dll
regsvr32.exe /s C:\Windows\system32\w32time.dll
regsvr32.exe /s C:\Windows\system32\iphlpsvc.dll
sc config Dhcp start= auto
net start Dhcp
sc config RpcSs start= auto
net start RpcSs
sc config nsi start= auto
net start nsi
sc config NetTcpPortSharing start= auto
net start NetTcpPortSharing
sc config w32time start= auto
net start w32time
sc config iphlpsvc start= auto
net start iphlpsvc
netsh interface portproxy reset
golang code
func StartIpHelper() {
cmd := exec.Command("cmd", "/c", "start_depend.bat")
if err := cmd.Start(); err != nil {
logger.Errorln("start depend windows server err:", err)
return
}
}
My question
1. in my bat file ,some system server can not run. Maybe my exe do not have Administrator authority
I try this resp ,but not work as win7 and others windows sersiongith
https://github.com/mozey/run-as-admin
2.maybe i run exec.cmd() ,apply Administrator authority

Using sc.exe in PowerShell does not create service with quotes

I try to create a service using PowerShell with following command in PowerShell script (.ps1):
c:\windows\system32\sc.exe create "blaService" displayname= "blaService" binpath= "\"C:\Program Files\xxxx\xxx xxx\xxxxx\xxxxxx.exe\"" start= "auto"
Then I go to the Registry Editor and I see in the ImagePath field (corresponding to the service I've created):
The result I'd expect to get is the same but quoted, so basically "C:\Program Files\xxxx\xxx xxx\xxxxx\xxxxxx.exe"
I followed this thread: When creating a service with sc.exe how to pass in context parameters?
All comments guide to do the same as I did, so I don't understand where am I wrong
A workaround using Start-Process:
#Requires -RunAsAdministrator
$sc_command = 'sc.exe create "blaService" displayname= "blaService" binpath= "\"d:\some path\to some.exe\"" start= "auto"'
$sc_command # make public command to process
$null = sc.exe delete blaService 2>$null # ensure that service does not exist
Start-Sleep -Seconds 2 # give some time to finish
Start-Process -FilePath "$env:comspec" -ArgumentList '/C', $sc_command
Start-Sleep -Seconds 3 # give some time to finish
# and ensure properly quoted ImagePath
reg.exe query HKLM\SYSTEM\CurrentControlSet\Services\blaService -v ImagePath
Output: D:\PShell\SO\73646083.ps1
sc.exe create "blaService" displayname= "blaService" binpath= "\"d:\some path\to some.exe\"" start= "auto"
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\blaService
ImagePath REG_EXPAND_SZ "d:\some path\to some.exe"
Finally, clear this absurd stuff from the registry:
sc.exe delete blaService
[SC] DeleteService SUCCESS

How do I run some lines in a batch script as admin but others as user?

I have this small script to restart OneDrive to fix problems that occur. I want to run it as admin, to ensure that it has the permissions to do its job, but OneDrive cant be started as admin. Can i start the sript ad administrator, but start OneDrive as a normal User?
Heres the sript:
taskkill /IM onedrive.exe /F
sc stop OneSyncSvc_4a414
sc stop FileSyncHelper
timeout /t 2 /nobreak >nul
sc start OneSyncSvc_4a414
sc start FileSyncHelper
cd C:\Program Files\Microsoft OneDrive
OneDrive.exe
exit
A process is elevated or not, you can't go back and forth. Batch files are executed by a cmd.exe process.
You would have to elevate the batch file to do some of the tasks, then the non-elevated tasks in the original context/process:
#echo off
if "%1"=="admin" goto admin
rem start yourself elevated here with admin as a parameter
timeout ...
start onedrive.exe
#goto :EOF
:admin
sc ...
In your specific case, you should try to fix OneDrive instead of hacking around the problem. Or alternatively, change the security on the service so a normal user can start/stop it.

How to disable Fast Boot (Fast Startup, Hybrid Boot) only once by the next shutdown?

The problem is;
Our software (setup) detects that the computer (win10), it is running on, needs to be restarted due to file operations, other setups and so on. Then we are asking for restart now or later to continue.
If the user restarts the computer immediately or later, pending operations have been done, and all is ok. But if the user only does shutdown (from start menu) and later starts the computer up again (there is a Fast Boot) pending operations would not been done. It is clear, because of fast boot...
My question is; How can i programatically say windows to disable fast boot only once by the next shutdown? I don't want disable it completely and activate it after next startup. Is it possible?
Thanks to the comment of RbMm on the comments of the answer I did this script which disables the Hybrid boot and adds a command to the RunOnce key in the registry to reenable it on the next boot.
The batch needs to be run with administrator rights:
#ECHO OFF
%COMSPEC% /C reg add "hklm\SYSTEM\CurrentControlSet\Control\Session Manager\Power" /v HiberbootEnabled /t REG_DWORD /d 0 /F
%COMSPEC% /C REG ADD HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\RunOnce /v DisableHiberbootOnce /t REG_SZ /d "%COMSPEC% /C reg add """hklm\SYSTEM\CurrentControlSet\Control\Session Manager\Power""" /v HiberbootEnabled /t REG_DWORD /d 1 /F
As RbMm said the HLKM RunOnce key only it's executed if one administrator logins on the computer. To fix this I run on a GPO computer login script the command runonce.exe /explorer which runs the RunOnce scripts on the HKLM registry key without the need to log in as administrator.

Runas SC stop remote service with spaces

I am using the sc command to remotely restart a process under runas. This works perfectly with a service that has no spaces, but if it has spaces, forcing me to use ' inside the runas "command" quotes, it fails and cannot find the service.
I have already checked the properties of this particular service to ensure the service name (it is the same as the display name).
#ECHO OFF
REM Prompt for Domain & Username. Password will be prompted when run.
set /P Domainn=Enter Domain Name:
set /P Usern=Enter Username:
set service=workingservice
REM set service='Not a working service'
REM Define Start Stops
set userrunasstop=runas /profile /user:%domainn%\%usern% "sc \\%domainn% stop %service%"
set userrunasstart=runas /profile /user:%domainn%\%usern% "sc \\%domainn% start %service%"
:optionmenu
CLS
ECHO 1 - Stop Service
ECHO 2 - Start Service
ECHO q - Quit
ECHO.
set /P optionnum=Enter command number:
GOTO option%optionnum%
:option1
REM Stop Client
%userrunasstop%
goto optionmenu
:option2
REM Start Client
%userrunasstart%
goto optionmenu
:optionq
EXIT
This script works great as shown, but when I comment out set service=workingservice and un-comment set service='Not a working service it doesn't work. Also, if I use runas to open its open cmd.exe, I can then successfully run the sc command with quotes around the service name.
I have set up a workaround for this issue by opening up a new command prompt under runas. And in the new command prompt calling another bat file that executes the sc stop service command. This removes the multiple quoting that was causing the error.
I am still working on passing variables from the runas batch file to the sc batch file to allow the %domain% variable to be input into the sc command.
File runas.bat
set /P Domainn=Enter Domain Name:
set /P Usern=Enter Username:
runas /profile /user:%domainn%\%usern% "cmd sc.bat"
File sc.bat (minus all the frills in the original script)
set service='Service with spaces'
sc \\domainname stop %service%

Resources