3

I am trying to figure out why my batch script doesn't work properly. It is calling wmic to see what server model it is and then deploy a set of drivers to that specific model number.

The 'wmic csproduct get name' outputs:

C:\>wmic csproduct get name 
Name
ProLiant DL360p Gen8

The for command outputs:

C:\>FOR /F "tokens=3" %A IN ('wmic csproduct get name') DO ( echo %A)
C:\>(echo Gen8 )
Gen8

Here is the script:

ECHO !TIME! - Determining if this is a HP Gen 8 or 9 server... >> !LOGFILE!
wmic csproduct get name | FIND /i "Gen" >NUL
if %ERRORLEVEL% EQU 1 (
SET %ERRORLEVEL%=0
ECHO !TIME! - Host doesnt appear to be a HP Gen 8 or 9 server...skipping install >> !LOGFILE!
ECHO !TIME! - ExitCode !ERRORLEVEL! >> !LOGFILE!
IF DEFINED USERNAME (EXIT /B !ERRORLEVEL!) ELSE EXIT !ERRORLEVEL!
)
FOR /F "tokens=3" %%A IN ('wmic csproduct get name') DO (
if %%A equ Gen9 (
ECHO !TIME! - Installing software and drivers for HP Gen 9>> !LOGFILE!
ECHO !TIME! - Installing HP ProLiant Gen9 Chipset Identifier for Windows (cp021663)>> !LOGFILE!
"%~dp0source\cp021663a.exe" /s /f /LOG=!MSILOGFILE!
ECHO !TIME! - Installing Headless Server Registry Update for Windows (cp016819)>> !LOGFILE!
"%~dp0source\cp016819.exe" /s /f /LOG=!MSILOGFILE!
ECHO !TIME! - Installing PFA Server Registry Update for Windows (cp022305)>> !LOGFILE!
"%~dp0source\cp022305.exe" /s /f /LOG=!MSILOGFILE!
ECHO !TIME! - Installing HP ProLiant Integrated Management Log Viewer for Windows Server x64 Editions (cp022717)>> !LOGFILE!
"%~dp0source\cp022717.exe" /s /f /LOG=!MSILOGFILE!
)
if %%A equ Gen8 (
ECHO !TIME! - Installing software and drivers for HP Gen 8>> !LOGFILE!
ECHO !TIME! - Installing Headless Server Registry Update for Windows
(cp016819)>> !LOGFILE!
"%~dp0source\cp016819.exe" /s /f /LOG=!MSILOGFILE!
ECHO !TIME! - Installing PFA Server Registry Update for Windows  
(cp022305)>> !LOGFILE!
"%~dp0source\cp022305.exe" /s /f /LOG=!MSILOGFILE!
ECHO !TIME! - Installing HP Broadcom Online Firmware Upgrade Utility for Windows Server x64 Editions (cp024029)>> !LOGFILE!
"%~dp0source\cp024029.exe" /s /f /LOG=!MSILOGFILE!
)
ELSE (ECHO !TIME! - ...model is not listed, so please add drivers >> !LOGFILE!)

The log file will show the following regardless if it is a gen 8 or gen 9 server: *note that its missing a ) at the end of the 2nd line after cp016819. Also note that it is not echoing the first line "Installing software and drivers for Gen 9"

Logfile

14:06:21.16 - Installing Headless Server Registry Update for Windows (cp016819
14:06:24.38 - Installing PFA Server Registry Update for Windows (cp022305)
14:06:27.47 - Installing HP ProLiant Integrated Management Log Viewer for Windows Server x64 Editions (cp022717)

So the problem is that the script only goes through Gen 9 if statement regardless if checks if it is a Gen 8 or 9. 2nd, it also skips the first few lines of the if statement (doesnt output to the log file as you can see above). I also know it is using Gen 9 because Gen 8 does not have the installer cp022717.

Any help will be much appreciated.

Thanks!

kahoots
  • 191
  • 2
  • 10
  • Check the round brackets. It looks as if the do-block is missing it's closing bracket – Dlanod Kcud Apr 09 '15 at 19:14
  • WMI query results are formatted in an uncommon encoding -- I think UCS-2 Little Endian, if I recall correctly. It can help to use `"tokens=2 delims=,"` and add a throwaway column to your `wmic` command -- something like `for /f "tokens=2 delims=," %%A in ('wmic csproduct get name^,UUID /format:csv ^| find "-"') do stuff` – rojo Apr 09 '15 at 20:29
  • @rojo WMI query results are formatted in UTF-16 Little Endian. Redirect it into a file, you can see typical `ÿþ` byte order mark, i.e. `0xFF` followed by `0xFE`. So command `FOR /F "tokens=3" %A IN ('wmic csproduct get name/value') DO @echo "%A"` returns `"blabla` with trailing `"` lost. Solution with an infix loop: `FOR /F "tokens=3" %G IN ('wmic csproduct get name/value') DO @for %A in (%G) do @echo "%A"` returns right `"blabla"` – JosefZ Apr 09 '15 at 21:46
  • 1. In `echo blabla n>>%logfile%`: if ` n` is a single digit preceded with a space, then `n` is considered to be a numeric handle for `>` or `>>` [redirection so output could get lost](http://ss64.com/nt/syntax-redirection.html). Use `>>%logfile% echo blabla n` syntax. 2. In `>>%logfile% echo blabla (foo)`, parentheses should be escaped as follows: `>>%logfile% echo blabla ^(foo^)`. Truly, I _do not know_ why: better to upvote rather than give an incomplete answer... – JosefZ Apr 09 '15 at 22:06
  • I removed all the parenthesis from the echo lines and it now does the checking for Gen 8 or 9. However, i still dont understand why i cant echo the first line into the log file yet i can echo the following lines? I dont see the ' n' in the echo command. Thanks for your help. – kahoots Apr 10 '15 at 17:15
  • In each `ECHO ... Gen 9>> !LOGFILE!` and `ECHO ... Gen 8>> !LOGFILE!`, there is **a single digit** denoted by `n` in my comment. – JosefZ Apr 11 '15 at 16:55

2 Answers2

2

You can't have the final "ELSE" clause on a separate line after the ). You have to write:

...
) else (
...

not

...
)
else (
...

(Maybe this was just the formatting of your post?)

Most importantly, don't use () in text you are echoing, use []. cmd is very poor at parsing (). You noted the missing ) on the second line. I suspect it interpreted this ) as the closing ) for the if %%A equ Gen9, and then another ) as the closing ) of the for statement. Then, when it gets to the if %%A equ Gen8, you are outside the for statement and %%A is no longer Gen8.

Also, in JosefZ's comment about n>>fileName, he was refering to your 9>>fileName, i.e. he was refering to a situation when n is a digit from 1-9. Put a space before the >>, so you have Gen 9 >>, or better yet is to put a period between them, as in Gen 9. >>

David I. McIntosh
  • 2,038
  • 4
  • 23
  • 45
2

Within any block statement (a parenthesised series of statements) you would need to escape any close-parenthesis that is not closing the block with a caret, thus : ^)

DO this for every close-parenthesis you have within an echo statement, check the balance of open and close-parentheses (indentation is useful for this - leading spaces are ignored) and ensure that the else is programmed as ) else (

Magoo
  • 77,302
  • 8
  • 62
  • 84