Invalid parameter in batch script

Asked

Viewed 115 times

2

I created the following batch script to prevent the user from deleting some directories within their home directory. We use Onedrive for Business and some symbolic links for the user to transparently back up to the cloud.

set LogFile=%appdata%\PermissaoArquivos.log

if not exist %LogFile% ( 
    echo Iniciando o processo em %DATE% %TIME% >> %LogFile%

    icacls %systemdrive%%homepath%\Documents /deny Todos:(de) >> %LogFile%
    icacls %systemdrive%%homepath%\Pictures /deny Todos:(de) >> %LogFile%
    icacls %systemdrive%%homepath%\Music /deny Todos:(de) >> %LogFile%
    icacls %systemdrive%%homepath%\Videos /deny Todos:(de) >> %LogFile%
    icacls %systemdrive%%homepath%\Desktop /deny Todos:(de) >> %LogFile%

    icacls "%OneDriveCommercial%\Desktop" /deny Todos:(de) >> %LogFile%
    icacls "%OneDriveCommercial%\Documents" /deny Todos:(de) >> %LogFile%
    icacls "%OneDriveCommercial%\Pictures" /deny Todos:(de) >> %LogFile%
    icacls "%OneDriveCommercial%\Music" /deny Todos:(de) >> %LogFile%
    icacls "%OneDriveCommercial%\Videos" /deny Todos:(de) >> %LogFile%
    echo Bloqueio de exclusao de diretorios do OneDrive aplicado em %DATE% as %TIME% >> %LogFile%
)

And I’m banging my head here because somehow it rejects the line "All:(of)" from the first line, generating the following output

Parâmetro inválido "Todos:(de"
Parâmetro inválido "-"
Parâmetro inválido "-"
Parâmetro inválido "-"
Parâmetro inválido "-"
Parâmetro inválido "-"

And the first icacls command is not processed while the others are. Any hint?

Grateful

3 answers

2

so in fact what was missing was @echo off...

I was trying to run the script without setting echo because I wanted to follow the execution.

At the end the file went like this:

    @echo off
    set LogFile=%appdata%\PermissaoArquivos.log
    if not exist %LogFile% ( 
        echo Iniciando o processo em %DATE% %TIME% >> %LogFile%
        icacls %systemdrive%%homepath%\Documents /deny Todos:(de) >> %LogFile%
        icacls %systemdrive%%homepath%\Pictures /deny Todos:(de) >> %LogFile%
        icacls %systemdrive%%homepath%\Music /deny Todos:(de) >> %LogFile%
        icacls %systemdrive%%homepath%\Videos /deny Todos:(de) >> %LogFile%
        icacls %systemdrive%%homepath%\Desktop /deny Todos:(de) >> %LogFile%
        icacls "%OneDriveCommercial%\Desktop" /deny Todos:(de) >> %LogFile%
        icacls "%OneDriveCommercial%\Documents" /deny Todos:(de) >> %LogFile%
        icacls "%OneDriveCommercial%\Pictures" /deny Todos:(de) >> %LogFile%
        icacls "%OneDriveCommercial%\Music" /deny Todos:(de) >> %LogFile%
        icacls "%OneDriveCommercial%\Videos" /deny Todos:(de) >> %LogFile%
        echo Bloqueio de exclusao de diretorios do OneDrive aplicado em %DATE% as %TIME% >> %LogFile%
        echo Computador %COMPUTERNAME% - Login %USERNAME% - Data %DATE% - Hora %TIME% >> \\ages9\LOGS\OneDrive\BloqueioExclusao.txt 
    )

1

If I may suggest a "Experiment" .. Try to see if inside the if, the "()" is interfering?

if exist arq (faz x) else (faz y)... what I understand comes to explain the message: "All:(from", where the ) on the first command within the if and in the following, both ()...

Another point, seen in published reply in the ONLY, would use escape in the arguments (), as in:

icacls "myfolder\dummy" /deny *S-1-1-0:^(DE,WA^) *S-1-5-7:^(DE,WA^)

Experiment ..

Using the layout of the code below, I am replacing the execution within the if by a goto followed by a double loop calling a call ...

@echo off & setlocal enabledelayedexpansion

set "LogFile=%appdata%\PermissaoArquivos.log"
set "Folders=Desktop,Documents,Pictures,Music,Videos

if not exist "!LogFile!" type nul >"!LogFile!" & goto :_apply_:

Rem Execute outros commandos, existe o arquivo: "!LogFile!"^^!
Rem Aqui entra seus outros commandos...

goto :eof

:_apply_:

echo Iniciando o processo em !DATE! !TIME! >>"!LogFile!"
for %%i in (%systemdrive%%homepath%\ %OneDriveCommercial%\ ) do (
     for %%I in (!Folders!) do set "_Path_Folder=%%~i%%~I"
     call :_deny_:
    )

echo/Bloqueio de exclusao de diretorios do OneDrive aplicado em !DATE! as !TIME!>>"!LogFile!" 
goto :eof

:_deny_:
icacls "!_Path_Folder!" /deny Todos:(de) >>"!LogFile!" && exit /b
echo/ error: "!_Path_Folder!" & timeout /t -1 & goto :eof

Experiment using Escape

@echo off & setlocal enabledelayedexpansion

set "LogFile=%appdata%\PermissaoArquivos.log"
set "Folders=Desktop,Documents,Pictures,Music,Videos

if not exist "!LogFile!" type nul >"!LogFile!" & goto :_apply_:

Rem Execute outros commandos, existe o arquivo: "!LogFile!"^^!
Rem Aqui entra seus outros commandos...

goto :eof

:_apply_:

echo Iniciando o processo em !DATE! !TIME! >>"!LogFile!"
for %%i in (%systemdrive%%homepath%\ %OneDriveCommercial%\ ) do (
     for %%I in (!Folders!) do set "_Path_Folder=%%~I%%~I"& call :_deny_:
    )

echo/Bloqueio de exclusao de diretorios do OneDrive aplicado em !DATE! as !TIME!>>"!LogFile!" 
goto :eof

:_deny_:
icacls "!_Path_Folder!" /deny Todos:^(de^) >>"!LogFile!" && exit /b
echo/ error: "!_Path_Folder!" & timeout /t -1 & goto :eof

Your Code using Escape

@echo off & setlocal enabledelayedexpansion

set LogFile=%appdata%\PermissaoArquivos.log

if not exist %LogFile% ( 
echo Iniciando o processo em %DATE% %TIME% >> %LogFile%

icacls %systemdrive%%homepath%\Documents /deny Todos:^(de^) >> %LogFile%
icacls %systemdrive%%homepath%\Pictures /deny Todos:^(de^) >> %LogFile%
icacls %systemdrive%%homepath%\Music /deny Todos:^(de^) >> %LogFile%
icacls %systemdrive%%homepath%\Videos /deny Todos:^(de^) >> %LogFile%
icacls %systemdrive%%homepath%\Desktop /deny Todos:^(de^) >> %LogFile%

icacls "%OneDriveCommercial%\Desktop" /deny Todos:^(de^) >> %LogFile%
icacls "%OneDriveCommercial%\Documents" /deny Todos:^(de^) >> %LogFile%
icacls "%OneDriveCommercial%\Pictures" /deny Todos:^(de^) >> %LogFile%
icacls "%OneDriveCommercial%\Music" /deny Todos:^(de^) >> %LogFile%
icacls "%OneDriveCommercial%\Videos" /deny Todos:^(de^) >> %LogFile%
echo Bloqueio de exclusao de diretorios do OneDrive aplicado em %DATE% as %TIME% >> %LogFile%

)

1


Fatou include @echo off at first

@echo off
set LogFile=%appdata%\PermissaoArquivos.log

if not exist %LogFile% ( 
    echo Iniciando o processo em %DATE% %TIME% >> %LogFile%

    icacls %systemdrive%%homepath%\Documents /deny Todos:(de) >> %LogFile%
    icacls %systemdrive%%homepath%\Pictures /deny Todos:(de) >> %LogFile%
    icacls %systemdrive%%homepath%\Music /deny Todos:(de) >> %LogFile%
    icacls %systemdrive%%homepath%\Videos /deny Todos:(de) >> %LogFile%
    icacls %systemdrive%%homepath%\Desktop /deny Todos:(de) >> %LogFile%

    icacls "%OneDriveCommercial%\Desktop" /deny Todos:(de) >> %LogFile%
    icacls "%OneDriveCommercial%\Documents" /deny Todos:(de) >> %LogFile%
    icacls "%OneDriveCommercial%\Pictures" /deny Todos:(de) >> %LogFile%
    icacls "%OneDriveCommercial%\Music" /deny Todos:(de) >> %LogFile%
    icacls "%OneDriveCommercial%\Videos" /deny Todos:(de) >> %LogFile%
    echo Bloqueio de exclusao de diretorios do OneDrive aplicado em %DATE% as %TIME% >> %LogFile%
)

Browser other questions tagged

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