Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 874
kahoots Avatar asked Nov 23 '25 01:11

kahoots


1 Answers

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 (

like image 150
Magoo Avatar answered Nov 25 '25 00:11

Magoo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!