Windows Shell Scripts in ZENworks

Windows Shell Scripts in ZENworks

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
Labels (1)


Some content on Community Tips & Information pages is not officially supported by Micro Focus. Please refer to our Terms of Use for more detail.
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...

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?
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.
This statement works flawlessly:
for /f "skip=2 tokens=2*" %%A in ('reg.exe query "HKLM\SOFTWARE\Novell\ZCM" /v "Version"') do (

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.
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
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
Top Contributors
Version history
Revision #:
5 of 5
Last update:
‎2020-02-18 22:44
Updated by:
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.