1

So I am writing a script that will be deployed through a KACE device in order to install dell updates to our machines. I have the update portion to the point it will work but when I try to input the logic to determine if it is the 2nd Tuesday of the month in order to apply hardware updates along with the logic to determine if a log file is greater than 45 KB in order to determine if anything was installed and reboot the system.

When I combine everything where it is intended to go, I end up getting the error that ( is unexpected. I have looked throughout my code and I am not seeing where the issue is coming from. Can anyone tell me where I have messed up my code so I am able to get this up and running and into production?

@echo off
:: Formatting Date and Time
for /f "tokens=1-4 delims=/ " %%d in ('echo %date%') do (
set dow=%%d
set month=%%e
set day=%%f
set year=%%g
)

pause
:: Picking the 2nd Tuesday
if "%dow%"=="Tue" (
if %day% geq 8 if %day% leq 14 (
    cd %windir%\Sysnative
    manage-bde.exe -protectors -disable c:
    cd "%PROGRAMFILES(X86)%\Dell\CommandUpdate\"
    dcu-cli.exe -policy \\server\dell_updates\Policy.xml -log C:\Temp\%COMPUTERNAME%
    pause
    net use O: \\server\Logs$
    cd O:
    mkdir O:\%COMPUTERNAME%
    del O:\%COMPUTERNAME%\*.xml /s /q
    copy C:\Temp\%COMPUTERNAME%\*.* O:\%COMPUTERNAME%
    setlocal
    set maxbytesize=40000
    set file="C:\Temp\%COMPUTERNAME%\ActivityLog.xml"
    FOR /F "usebackq" %%A IN ('%file%') DO set size=%%~zA

    if %size% LSS %maxbytesize% (
        rmdir c:\temp\%COMPUTERNAME% /s /q
        net use o: /delete
        endlocal
        goto end
        )
        ELSE (
        rmdir c:\temp\%COMPUTERNAME% /s /q
        net use o: /delete
        cd %windir%\Sysnative
        msg.exe * /w /time:0 "Do not press the OK button until you have closed all of your open programs and saved your work.  Pressing OK WILL reboot your computer."
        c:\windows\system32\shutdown.exe -r -f
        endlocal
        goto end
        )
    )
)
:: add the command lines that you want to run on any other day than the 2nd and 4th Monday
pause
:end
TBingeman
  • 11
  • 2
  • 1
    `)` and `else (` shall be on the same line like `) else (`, with the spaces in between! Type `if /?` into a command prompt window and read the help text very carefully! – aschipfl Apr 17 '18 at 18:50
  • 1
    and you are sure, all of those machines have the same date format? You better should use a method [independent of locale settings](https://stackoverflow.com/a/18024049/2152082) – Stephan Apr 17 '18 at 19:25
  • Why wouldn't you just create a Windows Scheduled Task? – Squashman Apr 18 '18 at 01:50
  • You are also setting and using the var `%maxbytesize%` inside a (code block) without [DelayedExpansion](https://ss64.com/nt/delayedexpansion.html) –  Apr 18 '18 at 14:03
  • OK I have added in the DelayedExpansion along with changing to ) Else (. I am not running this as a scheduled task as I would have to go to each computer in the domain to import the task as this is in an enterprise environment with many remote systems. All of these systems do use the same date format as they are all Windows 10 or Windows 7 machines on the local domain. After these changes, I am still getting "( was unexpected at this time." when I run my script – TBingeman Apr 18 '18 at 16:50
  • 1
    did you just *enable* [delayed expansion](https://stackoverflow.com/a/30284028/2152082), or do you also actually *use* it (with `!` instead of `%`)? – Stephan Apr 18 '18 at 17:45
  • I ended up putting things a little differently since it seems that batch scripting does not like too many nested If statements. I now have separated the file size verification and reboot to it's own little function and am calling it by using goto statements, even though I hate using goto statements. I am now able to get my script working without any issues. – TBingeman Apr 18 '18 at 18:22
  • The whole point of being in a domain environment is too make it easier to manage the computers in the domain. There should be no reason why you can't push the scheduled task to each computer using Group Policy. – Squashman Apr 18 '18 at 18:22
  • You also needed to use DelayedExpansion with the variable `size`. That is why you were getting the error: `( was unexpected at this time`. – Squashman Apr 18 '18 at 18:28
  • Also, need to use delayed expansion with the `file` variable. And you do not need to use a `FOR /F` command to get the file size. A regular for command would have been fine: `FOR %%A IN ("%file%") DO set size=%%~zA` – Squashman Apr 18 '18 at 18:40
  • if your update portion has a comparable quality, you really should let someone review all of your code before rollout. No offense intended, just advice. Bad code isn't a problem in your lab, but it is, once it gets rolled out. – Stephan Apr 19 '18 at 17:53

0 Answers0