0

I want to change the serial number and product number of about 250 servers (they are not correct at the moment). I know the command that I need to use, but I need to be able to input certain options in to that command and then run it.

The options I need are:

  1. A prompt for selecting the update of the serial or the product number
  2. Multiple prompts for actual serial number, an IP address, a user name and password on having selected before the update of the serial number
  3. Multiple prompts for actual product number, an IP address, a user name and password on having selected before the update of the product number

The command I wish to run via the batch file is:

asu64 set SYSTEM_PROD_DATA.SysInfoSerialNum XXXXXXX --host XXX.XXX.XXX.XXX --user XXXX --password XXXX

The XXX is a user defined input. I'd also like for completion and then go back to the initial selection menu.

Here is what I have done so far. (Please excuse the simplicity of it. I am very new to this stuff.)

@ECHO OFF
ECHO Test Code
ECHO.
ECHO 1.Serial Number
ECHO 2.Product Name
ECHO.
CHOICE /C 12 /M "Enter your choice:"
ECHO.
IF CHOICE 1 GOTO SerialNumber
IF CHOICE 2 GOTO ProductName

:SerialNumber
ECHO Serial Number 
GOTO End

:ProductNumber
ECHO Product Number
GOTO End

PAUSE

Many thanks for any help you can offer.

aschipfl
  • 31,767
  • 12
  • 51
  • 89
AshSalhan
  • 3
  • 1

1 Answers1

0

There could be used for this task:

@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "IpAddress="
set "NameUser="
set "Password="

:MainMenu
cls
echo/
echo   1 ... Serial number
echo   2 ... Product number
echo   E ... Exit
echo/
%SystemRoot%\System32\choice.exe /C 12E /N /M "Enter your choice:"
if errorlevel 3 exit /B
if not errorlevel 1 goto MainMenu
echo/
if errorlevel 2 goto ProductNumber

set "NumberText=serial"
set "NumberOption=SYSTEM_PROD_DATA.SysInfoSerialNum"
goto NumberPrompt

:ProductNumber
set "NumberText=product"
set "NumberOption=SYSTEM_PROD_DATA.SysInfoProductNum"

:NumberPrompt
set "NumberValue="
set /P "NumberValue=Please enter %NumberText% number: " || goto NumberPrompt
set "NumberValue=%NumberValue:"=%"
if not defined NumberValue goto NumberPrompt

echo/
if defined IpAddress (set "Default= [%IpAddress%]") else set "Default="
:IpPrompt
set /P "IpAddress=Please enter IP address%Default%: "
if not defined IpAddress goto IpPrompt
set "IpAddress=%IpAddress:"=%"
if not defined IpAddress goto IpPrompt

echo/
if defined NameUser (set "Default= [%NameUser%]") else set "Default="
:NamePrompt
set /P "NameUser=Please enter user name%Default%: "
if not defined NameUser goto NamePrompt
set "NameUser=%NameUser:"=%"
if not defined NameUser goto NamePrompt

echo/
if defined Password (set "Default= [%Password%]") else set "Default="
:PasswordPrompt
set /P "Password=Please enter password%Default%: "
if not defined Password goto PasswordPrompt
set "Password=%Password:"=%"
if not defined Password goto PasswordPrompt

echo/
"%~dp0ASU64.exe" set %NumberOption% "%NumberValue%" --host "%IpAddress%" --user "%NameUser%" --password "%Password%"
echo/
if not errorlevel 1 (
       echo The update of the %NumberText% number "%NumberValue%" was successful.
) else echo ERROR: The update of the %NumberText% number "%NumberValue%" failed!
echo/
%SystemRoot%\System32\choice.exe /C CE /N /T 10 /D C /M "Press C to continue or E to exit ..."
if not errorlevel 2 goto MainMenu
endlocal

The executable ASU64.exe is referenced with full path of the batch file which means the executable must be always in the same directory as the batch file. The current directory of cmd.exe processing the batch file does not matter in this case.

Please read the following answers for the explanation of the code:

For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

  • call /? ... explains %~dp0 ... drive and path of argument 0 which is the batch file path.
  • choice /?
  • echo /?
  • endlocal /?
  • exit /?
  • goto /?
  • if /?
  • set /?
  • setlocal /?
Mofi
  • 42,677
  • 15
  • 72
  • 128
  • Thanks Mofi, I won't be able to try this out until later this week but my intention is to pack this with the ASU64.exe program and as long as they are both in the same folder it should work. I have tried this on my laptop and it works. Though it will naturally fail as it can't find the IP address that get's put in. – AshSalhan May 15 '22 at 13:56
  • @AshSalhan I updated the code and its description according to the additional information provided by you in the comment above. – Mofi May 15 '22 at 18:40
  • Thanks Mofi, I don't think the exe needs to be specified in the command as it seems to work without that. – AshSalhan May 17 '22 at 12:17
  • @AshSalhan You tested the batch file most likely only by having it stored on a local drive and double clicking on it in Windows File Explorer. In this case the Windows File Explorer starts `cmd.exe` with [CreateProcess](https://docs.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-createprocessw) with `lpCurrentDirectory` set by `explorer.exe` to the directory containing the batch file. So __by chance__ the __current directory__ is the directory containing the batch file and `ASU64.exe`. – Mofi May 17 '22 at 15:57
  • @AshSalhan Run the batch file __as administrator__ and the current directory is perhaps `C:\Windows\System32`, see [Why does 'Run as administrator' change (sometimes) batch file's current directory?](https://stackoverflow.com/a/31655249/3074564) Copy the batch file on a network storage and access the folder on network storage with using the UNC path and the current directory is `C:\Windows`. Open a command prompt window and type the fully qualified batch file name using file name completion of `cmd.exe` and press __ENTER__ and the current directory is most likely `%USERPROFILE%`. – Mofi May 17 '22 at 16:00
  • @AshSalhan I write batch files working __by design__ and not __by chance__. The batch files written by me do not depend on properties of the execution environment defined outside of the batch file as this makes it unpredictable of the batch file always works in all possible execution environments and not just __by chance__ in most common execution environment. There are daily questions on Stack Overflow by people asking why a batch file does not work in use case X (run as administrator, as scheduled task, by a service, with UNC path, ...) while it works fine on double clicking it. – Mofi May 17 '22 at 16:05
  • Thanks for your input Mofi.The run as admin does indeed change how things run. That I appreciate and understand. Would it still make a difference if the asu64.exe and your script were always in the same folder? – AshSalhan May 18 '22 at 20:21
  • @AshSalhan The __current working directory__ can be always __any__ directory on `cmd.exe` processing a batch file. There is never the guarantee that the __current working directory__ is the __batch file directory__ which in your case always contains `asu64.exe`. Therefore referencing the executable `asu64.exe` with the batch file directory path being also the executable directory path is best practice to make sure this executable is always found by `cmd.exe` independent on which directory is the __current working directory__. – Mofi May 19 '22 at 06:16