Windows Shell Scripts in ZENworks

0 Likes
over 7 years ago
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.

CONCLUSION:
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.

Cheers!

REFERENCES:






ENVIRONMENT:
ZENworks backend:
ZCM=11.2.3.0 ZAM=11.2.3.18535 ZPM=11.2.3.21 ZESM=1.2.3.18535 ZFDE=1.2.3.18535
Windows XP SP3 x32:
Windows 7 SP1 x64:
ZENworks 11.2.3.24691 Monthly Update 1 and 11.1.0.12959

Labels:

How To-Best Practice
Comment List
Anonymous
  • 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
  • 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
  • Curious,
    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.
  • 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.
  • 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 host.domain.com') do set LOCTEST=%%%B

    Any suggestions?
  • 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...

    Thanks!!!!
Related Discussions
Recommended