Windows Shell Scripts in ZENworks

0 Likes
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
Parents
  • 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
Comment
  • 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
Children
  • 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
Related Discussions
Recommended