Cool Solutions

Windows Shell Scripts in ZENworks



May 5, 2014 4:21 pm





This is probably nothing to most but may be helpful to someone else who also is/was struggling.

Debugging, why a script I have written, when run from a file via the Windows shell, it works just great but when run via ZENworks bundle, “Run Script=>Define your own script” section, it yields the message:

“The syntax of the command is incorrect.”

It turns out that within the nested ‘IF’ statement, ZENworks is dropping one of the FOR loop variable’s double ‘%’ characters, which causes ‘IF’, to throw the syntax error.

I managed to slow the processor down enough by putting another batch file into an endless loop.

I was then able to make a copy of the script that ZENworks is writing to the %TEMP% directory, before ZENworks executes and deletes it.

That way I could then see what ZENworks was doing in the background.

I used the Sysinternals Process Monitor utility to discover where ZENworks was writing this file.

This snippit of code works, when manually run from the Windows shell, but fails as a bundle:

for /f "tokens=1* delims==" %%A in ('wmic baseboard get /format:list ^| find "="') do (
if "%%A"=="SerialNumber" set __BASE_SN=%%B

This is the batch file written by ZENworks into %TEMP% when the bundle is launched. It is virtually the same, except within the ‘IF’ statement, it should have %%A and %%B, which is the reason, ‘IF’ is throwing the syntax error.

for /f "tokens=1* delims==" %%A in ('wmic baseboard get /format:list ^| find "="') do (
if "%A"=="SerialNumber" set __BASE_SN=%B

The work around for the problem is to define the batch to look like this:

for /f "tokens=1* delims==" %%A in ('wmic baseboard get /format:list ^| find "="') do (
if "%%%A"=="SerialNumber" set __BASE_SN=%%%B

The bundle writes this code to appear correctly ( with just %% ) and therefore executes it without syntax errors. You need only ‘escape’ the ‘%’ within the ‘IF’ statement.

Unfortunately, this code fails with the same syntax error when run from a file via the Windows shell.



ZENworks backend:
Windows XP SP3 x32:
Windows 7 SP1 x64:
ZENworks Monthly Update 1 and

1 vote, average: 5.00 out of 51 vote, average: 5.00 out of 51 vote, average: 5.00 out of 51 vote, average: 5.00 out of 51 vote, average: 5.00 out of 5 (1 votes, average: 5.00 out of 5)
You need to be a registered member to rate this post.

Categories: Cool Tools, General, Technical, ZENworks


Disclaimer: This content is not supported by Micro Focus. It was contributed by a community member and is published "as is." It seems to have worked for at least one person, and might work for you. But please be sure to test it thoroughly before using it in a production environment.


  1. By:hnewman

    You just saved me from going crazy – spent most of the afternoon troubleshooting the same thing. Finally googled “for loops zenworks script action” and landed here.

    Has this been entered as a bug? Because I’m about to edit 30 or so bundles and I don’t want to do that if it’s going to get fixed…


  2. By:hnewman

    Hm, spoke too soon. This fix didn’t work for me. Here is my corrected statement and it still fails with “B was unexpected at this time”.

    for /f “tokens=2 delims=[]” %%B in (‘ping -n 1’) do set LOCTEST=%%%B

    Any suggestions?

    • By:hnewman

      Solved. It whacks the extra % everywhere the variable appears. BTW, same thing happens if you try to create a local file using the Edit Text File action.

      • By:eradke

        This statement works flawlessly:
        for /f “skip=2 tokens=2*” %%A in (‘reg.exe query “HKLM\SOFTWARE\Novell\ZCM” /v “Version”‘) do (
        set __ZEN_VERSION=%%B

        Could it have something to do with using the delims option?

        You can still always upload the batch then install it and then launch executable.

        Even though I stumbled on this work-around. For smaller scripts it is fine but larger ones’s I’ll continue to use the method I just mentioned.

        I really need to learn VBS or Power Shell a little better, it’s just these old habits y’ know.

  3. By:jgrundman

    I am having similar issues I think. My script is simple and is just using an XCOPY command, but I think the %USERNAME% variable is having issues. Any ideas why this is not working? Thanks!

    XCOPY “C:\Users\%USERNAME%\Desktop” “H:\DeskFavs\Desktop” /D /S /Y
    XCOPY “C:\Users\%USERNAME%\Favorites” “H:\DeskFavs\Favorites” /D /S /Y

    • By:eradke

      Very late but . . .

      It is possible for a user profile path to be different than “\users\username”, the user profile could be a duplicate, then Windows will append characters after the username like “eradke.domain, eradke.computername, eradke.001 …”

      To avoid this, write the script like this:

      XCOPY “%USERPROFILE%\Desktop” “H:\DeskFavs\Desktop” /D /S /Y
      XCOPY “%USERPROFILE%\Favorites” “H:\DeskFavs\Favorites” /D /S /Y