File . bat does not run

Asked

Viewed 2,225 times

2

I have a file with the following command to disconnect some machines from the network.

FOR /F %i IN (C:\estacoes.txt) DO SHUTDOWN /s /t 01 /m %i

where estacoes.txt is a file with the names of the machines to be shut down.

When I open cmd and execute this command in hand it works correctly. But when I save to a file . bat and try to execute it does not execute.

I tried to run as administrator and also did not work.

  • Puts a PAUSE at the end of the document and then you can look at the errors that occur.

3 answers

2

I did it this way and it worked

echo off
cls
for /F %%i in (%homedrive%\estacoes.txt) do (
SHUTDOWN /s /t 01 /m %%i
)

Example of the list I made:

windowsxp
portaria
novowindows

I tested on a Windows x64 (Windows 8.1) to turn off other machines and then directly and at the end itself, then tested on a x32 machine (Windows XP SP2), also was correctly.

I just could not turn off the machine with Windows 8.1 because you need to authenticate by a user of it.

If you use ping can check if the machine is connected or connected to the network before trying to shut down, this avoids the long and unnecessary waiting of the FOR.

0


  • To avoid errors, you can go checking if the station is UP (suggestion from @Oldschool), this before sending the command to turn off, another important thing, would be to send some warning to the user to prevent job loss.
@echo off && setlocal enabledelayedexpansion 

color 0A && title ..\%~dpnx0 && mode 100,10  && chcp 1252 >nul 

for /f %%i IN (C:\estacoes.txt)do "%__APPDIR__%ping.exe" %%~i -n 2 -4 >nul && (
"%__APPDIR__%ShutDown.exe" /m \\%%~i /s /f /d p:1:2 /c "Salve seus trabalhos, Estação será desligada em 300 segundos!!" /t 300 2>nul
) || ( Echo/ & echo/ Estação %%~i nao responde Pings ou o Agendamento de Desligamento jah esta em curso) 

endlocal && goto :EOF

  • Also quoted in that reply Q426377 - Pretty much the same theme/response

Remote access has to be enabled on the station by a configuration that is done locally, not just send an access request and/or a command, the other stations where (in another domain or not) your command has worked, is the result of a configuration applied when previously required.

This restriction came by default from Windows Vista, allowing remote (filtered) access to only admin group members local.

Via perform a reg query in the key below and check if it returns to you:

EnableLUA REG_DWORD 0x1     or     EnableLUA REG_DWORD 0x0


reg query "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System" | findstr "EnableLUA"
Rem :: EnableLUA    REG_DWORD    0x1

rem ::  Então adicione a entrada/valor 0
reg add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System" /v EnableLUA /t REG_DWORD /d 0 /f

In short:

Check in the destination station, if this key exists and if it is enabled(0x1):


reg query "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\system" | find "LocalAccountTokenFilterPolicy" | find "0x1"

If it exists and is enabled, the command returns to you:


LocalAccountTokenFilterPolicy    REG_DWORD    0x1

In case it does not exist or the value differs from 0x1, use this command to add/enable:


reg add "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\system" /v LocalAccountTokenFilterPolicy /t REG_DWORD /d 1 /f

0

The syntax of FOR for a single-line command is only directed to when you will use the Console directly, for a BATCH file no directive is required /F.

@echo off
FOR %%i IN (C:\estacoes.txt) DO SHUTDOWN /s /t 01 /m %%i
pause

Also, you will need to use the %%i instead of %i.

Note: Remove the pause end of line if not debugging this code.

  • I changed the remote to the way you said it, but it still doesn’t work. I left it with pause to see if Purge, but while running the file it tries to open the prompt, it gets less than 1s open and already closes and does not perform the task. I’m using windows 10

  • Strange, here worked properly. Windows 8.1.

  • Do the following, open the CMD, take the . bat and drag it in the CMD and release. Tell us what will appear.

  • Good morning. I did it. Only the name of the aquivo appeared and nothing else. Att, Mauritius.

  • It is probably a matter of security, IE, need to authenticate before sending the command, which the Windows version of each machine?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.