How to disable admin prompt in start-up .bat-file (Windows 10)? - windows

So I'm running a check to see if my laptop's docked or not, if it is I'll disable my iGPU by running the following batch command:
#echo off
echo *** Disabling NVIDIA GeForce GTX 1650 with Max-Q Design ***
devmanview.exe /disable "NVIDIA GeForce GTX 1650 with Max-Q Design"
echo *** Done ***
The issue is, that as this'll run every time at start-up, I don't want to have to respond to the UAC prompt (for devmanview.exe). Is there a way I can either prevent this prompt from triggering, or auto-answer it?
Thanks!
(Here's the full script for anyone interested:)
:: See if NAS is connected locally
Dir \\169.254.1.2\Docker\CheckFile.txt
If %ErrorLevel% EQU 0 GoTo FileSpecifiedIsHere
GoTo ItIsntHere
Pause This line is never reached
:FileSpecifiedIsHere
::Do whatever I want to if the one specified is detected
Start "Starting the application" /MIN "C:\DisableGTX1650.bat"
GoTo EndThisBatch
:ItIsntHere
::Do whatever I want to if the one specified is not detected
Start "Starting the application" /MIN "C:\Program Files (x86)\ASUSTOR\EZ Connect\EasyConnect.exe"
Start "Starting the application" /MIN "C:\EnableGTX1650.bat"
GoTo EndThisBatch
:EndThisBatch

Related

How to execute multiple programs in the same time with a .bat file

I made 2 bat files to start apps with examples below:
My expectation is to execute them simultaneously, meaning after double click bat file, then 3 programs will pop up.
With the 1st example, the behavior is to execute outlook first, then both Mircrosoft Edge and OneNote still not pop up, until I stop Outlook.
Example 1
#echo off
"C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Outlook.lnk"
"C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Microsoft Edge.lnk"
"C:\ProgramData\Microsoft\Windows\Start Menu\Programs\OneNote.lnk"
exit
With the 2nd example, both Mrcrosoft Edge and OneNote were executed simultaneously, however Outlook not until I stop OneNote.
Example 2
#echo off
"C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Microsoft Edge.lnk"
"C:\ProgramData\Microsoft\Windows\Start Menu\Programs\OneNote.lnk"
"C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Outlook.lnk"
exit
My questions is why it behaves like this way and how to make these 3 programs start up in the same time ?
Shown below is the Windows config:
Edition Windows 10 Enterprise
Version 21H2
Installed on ‎10/‎27/‎2021
OS build 19044.1826
Experience Windows Feature Experience Pack 120.2212.4180.0
1)Run the first program using the start command.
2)Check the task list in a loop to see if the program has appeared there.
3)Impose some time limitation to the said loop.
4)Run the next program in case of success, exit with notification otherwise.
#ECHO
OFF START program1.exe
FOR /L %%i IN (1,1,100) DO (
(TASKLIST | FIND /I "program.exe") && GOTO :startnext
:: you might add here some delaying
)
ECHO Timeout waiting for program1.exe to start
GOTO :EOF
:startnext
program2.exe
:: or START
program2.exe
Remember that the timing is not precise, especially if you are going to insert delays between the task list checks.
Normally to run tasks parallel, you should add start /b before the command to run.
The start command allows the command to be executed in another process, while /b prevents the opening of a new window.
In this specific case start /b does not work, for reasons unknown to me, but you can always use cmd /c.

How to close the window of an executable launched from CMD without using TASKILL [BATCH]

I have made a basic BAT script to download updates from Avast virus database and then apply them by running the downloaded file.
#ECHO OFF
set downloadFolder=C:\Users\myuser\Downloads\Avast_updates
set downloadUrl=https://install.avcdn.net/vps18/vpsupd.exe
bitsadmin /transfer myAvastUpdates /download /priority normal ^
"%downloadUrl%" "%downloadFolder%\vpsupd.exe"
start /min "Update..." "%downloadFolder%\vpsupd.exe"
exit
Also, I have created a Windows task to run BAT every x hours.
Everything works correctly, but I want to know if there is any way to automatically close the executable window after the update process is finished.
It occurred to me to use TASKILL after x seconds, but that doesn't assure me that the update process finished in x seconds, sometimes it can take longer and sometimes less, plus I don't want to use that command in an program security installer.
Then it occurred to me to send an "Enter" through WshShell.SendKeys:
set SendKeys=CScript //nologo //E:JScript "%~F0"
cls
timeout /t 5 >nul
%SendKeys% "{ENTER}"
#end
var WshShell = WScript.CreateObject("WScript.Shell");
WshShell.SendKeys(WScript.Arguments(0));
But it did not work because that window does not close with "Enter" nor "ALT F4", it closes only if we click on "Done" or on the cross "x" to close the window (if it worked it would have the problem of setting the time again).
Is there a way to automatically close that window once the update process finishes?
vpsupd.exe supports a /silent switch to suppress user interactions.
start /min isn't needed, as it just opens another cmd window, which in turn runs the executable. So just do:
vpsupd.exe /silent
With the help of #Stephan and #Gerhard, the code to download and update Avast was like this:
#ECHO OFF
set "downloadFolder=%userprofile%\Downloads\avast_updates"
set "downloadUrl=https://install.avcdn.net/vps18/vpsupd.exe"
bitsadmin /transfer myAvastUpdates /download /priority normal ^
"%downloadUrl%" "%downloadFolder%\vpsupd.exe"
"%downloadFolder%\vpsupd.exe" /silent
exit

How to check via command line if a VirtualBox VM is not running on Windows

I am working on a Jenkins project to remotely startup and shutdown a specific VM with user-supplied parameters.
I have managed to find a way to determine that the specified VM is started up and running and ready for further instructions, but I have yet to find a way to determine that the specified VM has powered off.
The current code used in the build step is this:
#ECHO OFF
SETLOCAL EnableDelayedExpansion
ECHO Shutting down VM %VM% on Host %computername%...
"C:\Program Files\Oracle\VirtualBox\VBoxManage.exe" controlvm %VM% acpipowerbutton
ECHO %ERRORLEVEL%
IF ERRORLEVEL 1 EXIT /B 1
:LOOP
WAITFOR /T 10 VMtoShutDown 2>NUL
FOR /f %%a IN ('VBoxManage list runningvms ^| FINDSTR %VM%') DO SET VM_State=%%a
IF [!VM_State!] EQU [] (
ECHO %VM% is not running.
EXIT /b 0
)
GOTO LOOP
The logic is this:
Send controlvm command to perform ACPI shutdown of target VM.
Use FOR to iterate through a list of Running VMs to see if target VM is still running.
If target VM is still running, don't do anything, and go back to the beginning of :LOOP and re-check until result changes.
If target VM is no longer seen in the list of Running VMs, consider this build step a success.
The main problem is the FOR statement in the :LOOP clause. In the Startup portion of the build step, I can use VboxManage list runningvms to determine if the specified VM is listed, and thus process it as such and indicate that the Jenkins build to startup the specified VM is successful.
However, I noticed that once the target VM is taken off the list of running VMs by way of the earlier controlvm command, the entire DO part of the FOR statement simply doesn't do anything. It doesn't set the VM_State=%%a, for one, and it doesn't do anything else I had thrown into the FOR statement. This causes the :LOOP to keep looping infinitely as the only condition checker doesn't fire.
Question: Is there any way to determine if a VM of a particular name is powered off?
Notes:
The VirtualBox plugin for Jenkins is incompatible with the current VirtualBox version (5.1.30).
The Jenkins version is 2.122.
The code above is tested to behave the same when running as a standalone .bat file on the target machine and on my own machine.
All systems involved are on Windows 10 v1803.
This works for me on Windows 10, hope it helps!
#ECHO OFF
SETLOCAL EnableDelayedExpansion
ECHO Shutting down VM %VM% on Host %computername%...
"C:\Program Files\Oracle\VirtualBox\VBoxManage.exe" controlvm %VM% acpipowerbutton
ECHO VirtualBox command return code: %ERRORLEVEL%
IF ERRORLEVEL 1 EXIT /B 1
:LOOP
REM Sleep for 1 second by using a ping hack :)
ping -n 2 127.0.0.1 > nul
"C:\Program Files\Oracle\VirtualBox\VBoxManage.exe" showvminfo %VM% | findstr /c:"powered off" >NUL
IF %ERRORLEVEL% EQU 0 GOTO END
echo VM %VM% not powered off yet...
GOTO LOOP
:END
echo VM %VM% powered off.

Batch to check if process exists

I'd like a batch that will check if the process firefox.exe exists (after it has been started by the start command).
If the process exists, it will go to the label :fullscreen,
else the batch will go the the label :timeout. Then, it will check again if the process firefox.exe exists and if not, it will go again to the label :fullscreen until the process exists.
Here is my batch:
#echo off
start "C:\Program Files (x86)\Mozilla Firefox\firefox.exe"
:timeout
timeout /t 5
:fullscreen
nircmd sendkeypress F11
exit
How can I do this check ?
You can also use QUERY PROCESS:
#Echo Off
If Not Exist "%ProgramFiles(x86)%\Mozilla Firefox\firefox.exe" Exit/B
Start "" "%ProgramFiles(x86)%\Mozilla Firefox\firefox.exe"
:Loop
Timeout 5 /NoBreak>Nul
QProcess firefox.exe>Nul 2>&1||GoTo :Loop
NirCmd SendKeyPress F11
I suggest for this task the batch file:
#echo off
start "" /max firefox.exe
if errorlevel 1 goto :EOF
set LoopCount=0
:WaitLoop
%SystemRoot%\System32\timeout.exe /T 5
%SystemRoot%\System32\tasklist.exe /FI "IMAGENAME eq firefox.exe" 2>nul | %SystemRoot%\System32\find.exe /I "firefox.exe" >nul
if not errorlevel 1 nircmd.exe sendkeypress F11 & goto :EOF
set /A LoopCount+=1
if not %LoopCount% == 6 goto WaitLoop
Let me explain the few command lines used here.
1. Starting Firefox
The command START being an internal command of cmd.exe interprets the first double quoted string as optional title for the console window. Therefore the command line
start "C:\Program Files (x86)\Mozilla Firefox\firefox.exe"
results just in opening a new console window with the window title:
C:\Program Files (x86)\Mozilla Firefox\firefox.exe
For that reason "" is specified as first START argument to define an empty title. Firefox is a GUI application. So no console window is opened which means an empty window title is really enough.
The parameter /max would not be really necessary, but the goal is to get Firefox into full screen mode after starting. So why not starting it already maximized?
32-bit version of Firefox is by default installed in directory %ProgramFiles% on 32-bit Windows and in %ProgramFiles(x86)% on 64-bit Windows. But it is possible during the installation to install Firefox into any other folder. But Firefox installer is well coded and registers firefox.exe in Windows registry under key:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths
This is recommended by Microsoft as written in MSDN article Application Registration.
The command START searches also in Windows registry under this key for an executable specified as argument without path as explained in answer on Where is “START” searching for executables?
This is the reason for using just firefox.exe on START command line because that starts an installed Firefox independent on installation location.
START displays an appropriate message box if firefox.exe could not be started and exits in this case with a return code greater 0 (9059 in my test on one computer).
The help output on running if /? in a command prompt window explains how to evaluate the exit code of a previous command or application without usage of immediate or delayed environment variable expansion and therefore working anywhere in a batch file from MS-DOS (really!) to currently latest Windows 10.
The command line if errorlevel 1 goto :EOF means IF start failed to start firefox.exe indicated by an exit code greater or equal 1 THEN exit execution of this batch file. For details on exiting batch file execution see answer on Where does GOTO :EOF return to?
2. Checking for running Firefox
The command TASKLIST being an external command, i.e. a console application in system directory of Windows, outputs a list of running processes. This list can be already filtered by TASKLIST itself for a specific process as done in batch file with /FI "IMAGENAME eq firefox.exe".
But TASKLIST is designed for just printing a list of processes. It is not designed for checking if a specific process is running and returning the result to the calling process via exit code. TASKLIST always exits with 0.
But an error message is output to handle STDERR on using a filter and no process can be found in process list matching this filter. For that reason 2>nul is used to suppress this error message by redirecting it to device NUL. Read the Microsoft article about Using Command Redirection Operators for more information about redirection.
A simple method to get a simple false/true respectively 0/1 result on checking for running Firefox is filtering output of TASKLIST with external command FIND which exits with 0 if the string to find was indeed found or with 1 if the searched string could not be found in the text read in this case from STDIN. The output of FIND is of no interest and therefore suppressed with redirection to device NUL using >nul.
Instead of using TASKLIST and FIND it is also possible to use QPROCESS:
%SystemRoot%\System32\qprocess.exe firefox.exe >nul 2>&1
QPROCESS exits with exit code 1 if firefox.exe could not be found in list of running processes. Otherwise the exit code is 0 on firefox.exe is running.
3. Evaluating Firefox process checking result
if not errorlevel 1 nircmd.exe sendkeypress F11 & goto :EOF
The IF command checks if exit code of FIND is NOT greater or equal 1 which means if exit code is lower than 1. Command FIND exits never with a negative value. So if this condition is true then it is time to execute nircmd.exe to send key press F11 to application in foreground hopefully being Firefox (not guaranteed by this code) and exit batch file processing.
Otherwise the batch file should wait once again 5 seconds and then do the check again. This can very easily result in an endless running batch file in case of started Firefox is immediately closed by the user before the 5 seconds wait timed out. For that reason it is counted how often the wait loop is already executed. After 6 loop runs, or 30 seconds, it is really time to no longer wait for Firefox and exit the batch file.
4. Getting more information about used commands
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
echo /?
find /?
if /?
qprocess /?
set /?
start /?
tasklist /?
timeout /?
And Single line with multiple commands using Windows batch file should be also read explaining operator & in addition to all other web pages referenced already above.
You can show a list of opened programs like this:
tasklist
To check if firefox exists:
EDIT: Code edited to show a fully working example
#echo off
start "C:\Program Files (x86)\Mozilla Firefox\firefox.exe"
goto :checkloop
:checkloop
tasklist|find "firefox.exe" > NUL
if %ERRORLEVEL% == 0 (
call :fullscreen
exit
) else (
call :timeout
goto :checkloop
)
:fullscreen
nircmd sendkeypress F11
goto :EOF
:timeout
timeout /t 5
goto :EOF

New console windows crash Explorer.exe and Windows desktop

Recently I am interested in programming a windows batch file.
In this process, I found a critical problem in programming CMD batch.
Many CMD batch windows cause crashing explorer.exe and Windows desktop.
Many odd symptoms on Window desktop occurred.
Thus, I must kill explorer.exe and restart explorer.exe
Example
A.cmd:
#echo off
set /a a=0
:loop
set /a a=%a%+1
echo %a%
timeout /t 2 > nul
start B.cmd
goto loop
B.cmd
#echo off
echo Hello, WORLD !!!
timeout /t 60 > nul
exit
I executed A.cmd.
After a while, windows screen crashed.
Someone said that's hardware spec problem.
Someone said that's loop batch problem.
Someone said that's malware problem.
I don't think that's a computer, malware and hardware spec problem.
I think Microsoft windows itself problem.
The batch file that I am think of must need many (1~30) new CMD windows that will closed after a while.
Thus I made a CMD batch file.
No problem.
Working well.
The only problem is crashing Explorer.exe with crashing Windows desktop.
What's the solution?

Resources