kahoots
kahoots

Reputation: 191

Batch if statement issue

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!

Upvotes: 3

Views: 303

Answers (2)

David I. McIntosh
David I. McIntosh

Reputation: 2130

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

Upvotes: 2

Magoo
Magoo

Reputation: 80113

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 (

Upvotes: 2

Related Questions