You are not logged in.

#1 29 Dec 2015 14:32

ImAhNoBoDy
Member
Registered: 29 Dec 2015
Posts: 12

Match computer name from list

I am trying to install a software with a license depending on the computer name. This script works:

::Installs the software with the first serial number
SET SERIAL=0
setlocal enabledelayedexpansion
for %%a in (COMP1 COMP2 COMP3 COMP4 COMP5) do (
	SET MACHINENAME=%%a
         if !MACHINENAME! equ %computername% SET SERIAL=1
       ) 
ECHO SERIAL IS %SERIAL%
IF %SERIAL% NEQ 1 GOTO SERIAL2

ECHO INSTALLING
PROGRAM.EXE ....... SERIALNUMBER1
ECHO IT IS INSTALLED
goto end

::Installs the software with the second serial number
:SERIAL2

for %%a in (COMP6 COMP7 COMP8 COMP9 COMP10) do (
	SET MACHINENAME=%%a
         if !MACHINENAME! equ %computername% SET SERIAL=2
	)
ECHO SERIAL IS %SERIAL%
IF %SERIAL% NEQ 2 GOTO NOTINSTALLED

ECHO INSTALLING THIS SERIAL
PROGRAM.EXE ...... SERIALNUMBER2
ECHO IT IS NOW INSTALLED
GOTO END

::Does not install any software if not listed
:NOTINSTALLED

MSIEXEC ......
ECHO PROUCT IS NOT INSTALLED

:END 

But I'm trying to make it into 1 big one for/if statement, but am failing miserably. This is what I have so far, but I don't know how to expand the list correctly:

	SET LIST1=COMP1 COMP2 COMP3 COMP4 COMP5
	SET LIST2=COMP6 COMP7 COMP8 COMP9 COMP10
	SET SERIAL1=/SN=XXXX1
	SET SERIAL2=/SN=XXXX2
	SET INSTALL=PROGRAM.EXE /S
	SETLOCAL ENABLEDELAYEDEXPANSION
	FOR %%a IN (%COMPUTERNAME%) DO (
	SET MACHINENAME=%%a
		IF !MACHINENAME! EQU !LIST1! (ECHO %COMPUTERNAME% is listed to have SERIAL1 license
			ECHO .
			ECHO Installing with SERIAL1 license . . .
	%INSTALLNEW% %SERIAL1%
	GOTO END) ELSE (IF !MACHINENAME! EQU !LIST2! (ECHO %COMPUTERNAME% is listed to have SERIAL2 license
			ECHO .
			ECHO Installing with SERIAL2 license
		%INSTALL% %SERIAL2%
		GOTO CAPTURE) ELSE (
			ECHO %COMPUTERNAME% is not listed to have SERIAL1 or SERIAL2 license
			ECHO .
			ECHO Failed to install)
				) 
			)

	ENDLOCAL

:END
ECHO Software installed

Can someone please let me know how to do this? Thanks.

Offline

#2 29 Dec 2015 15:42

ImAhNoBoDy
Member
Registered: 29 Dec 2015
Posts: 12

Re: Match computer name from list

Err...this is the correct code. I can't edit my own post cause I'm new:

	SET LIST1=COMP1 COMP2 COMP3 COMP4 COMP5
	SET LIST2=COMP6 COMP7 COMP8 COMP9 COMP10
	SET SERIAL1=/SN=XXXX1
	SET SERIAL2=/SN=XXXX2
	SET INSTALL=PROGRAM.EXE /S
	SETLOCAL ENABLEDELAYEDEXPANSION
	FOR %%a IN (%COMPUTERNAME%) DO (
	SET MACHINENAME=%%a
		IF !MACHINENAME! EQU !LIST1! (ECHO %COMPUTERNAME% is listed to have SERIAL1 license
			ECHO .
			ECHO Installing with SERIAL1 license . . .
	%INSTALL% %SERIAL1%
	GOTO END) ELSE (IF !MACHINENAME! EQU !LIST2! (ECHO %COMPUTERNAME% is listed to have SERIAL2 license
			ECHO .
			ECHO Installing with SERIAL2 license
		%INSTALL% %SERIAL2%
		GOTO END) ELSE (
			ECHO %COMPUTERNAME% is not listed to have SERIAL1 or SERIAL2 license
			ECHO .
			ECHO Failed to install)
				) 
			)

	ENDLOCAL

:END
ECHO Software installed

Offline

#3 29 Dec 2015 16:25

bluesxman
Member
From: UK
Registered: 29 Dec 2006
Posts: 1,129

Re: Match computer name from list

The main problem here is that you're looping around a single item (the COMPUTERNAME) and trying to compare it to the string of computer names in LIST1 and LIST2.  You need to be looping through the items in LIST1/2 instead (which is how your original "working" code looks to be doing things).  Try this (untested):

SET LIST1=COMP1 COMP2 COMP3 COMP4 COMP5
SET LIST2=COMP6 COMP7 COMP8 COMP9 COMP10
SET SERIAL1=/SN=XXXX1
SET SERIAL2=/SN=XXXX2
SET INSTALL=PROGRAM.EXE /S
SET "SERIAL="
SETLOCAL ENABLEDELAYEDEXPANSION
FOR %%a IN (1 2) DO (
  if not defined SERIAL (
    for %%b (!LIST%%a!) do (
    if not defined SERIAL IF %COMPUTERNAME% EQU %%b (ECHO %COMPUTERNAME% is listed to have SERIAL%%a license
      ECHO .
      ECHO Installing with SERIAL%%a license . . .
      set SERIAL=!SERIAL%%a!
    )
  )
)
if defined SERIAL
  %INSTALL% %SERIAL%
  if not errorlevel 1 ECHO Software installed
) else (
  ECHO %COMPUTERNAME% is not listed to have SERIAL1 or SERIAL2 license
  ECHO Failed to install
)
ENDLOCAL

I have reused your code where possible; mine additions are in lower case, as that's my coding style.

Last edited by bluesxman (29 Dec 2015 16:28)


cmd | *sh | ruby | chef

Offline

#4 29 Dec 2015 19:33

ImAhNoBoDy
Member
Registered: 29 Dec 2015
Posts: 12

Re: Match computer name from list

Sorry, I remembered that I had SERIAL1 and SERIAL2 variables changed to DESKTOP and LAPTOP in my other script. The %%a wouldn't work anymore. Would it be possible remove the SERIAL variable (NOT SERIAL1 OR SERIAL2) altogether or is it necessary?

Offline

#5 30 Dec 2015 01:23

bluesxman
Member
From: UK
Registered: 29 Dec 2006
Posts: 1,129

Re: Match computer name from list

SERIAL is used to remove duplication in the code.

If I understand what you're saying correctly -- you are setting the values for the serials as two variables called DESKTOP and LAPTOP somewhere else, but you want to re-use those values in this script -- then this change should be all you need (assuming that this script is being called by the other script):

SET LIST1=COMP1 COMP2 COMP3 COMP4 COMP5
SET LIST2=COMP6 COMP7 COMP8 COMP9 COMP10
SET "SERIAL1=%DESKTOP%"
SET "SERIAL2=%LAPTOP%"

If this doesn't work and/or I have misunderstood then please post all the relevant code here.

Last edited by bluesxman (30 Dec 2015 01:39)


cmd | *sh | ruby | chef

Offline

#6 30 Dec 2015 13:16

ImAhNoBoDy
Member
Registered: 29 Dec 2015
Posts: 12

Re: Match computer name from list

I renamed SERIAL1 and SERIAL2 to DESKTOP AND LAPTOP like this:

SET LIST1=COMP1 COMP2 COMP3 COMP4 COMP5
SET LIST2=COMP6 COMP7 COMP8 COMP9 COMP10
SET "DESKTOP=/SN=XXXX1"
SET "LAPTOP=/SN=XXXX2"

Offline

#7 30 Dec 2015 18:17

bluesxman
Member
From: UK
Registered: 29 Dec 2006
Posts: 1,129

Re: Match computer name from list

Alright, can I suggest you do this then:

SET "LIST_DESKTOP=COMP1 COMP2 COMP3 COMP4 COMP5"
SET "LIST_LAPTOP=COMP6 COMP7 COMP8 COMP9 COMP10"
SET "DESKTOP=/SN=XXXX1"
SET "LAPTOP=/SN=XXXX2"
SET INSTALL=PROGRAM.EXE /S
SET "SERIAL="
SETLOCAL ENABLEDELAYEDEXPANSION
FOR %%a IN (DESKTOP LAPTOP) DO (
  if not defined SERIAL (
    for %%b (!LIST_%%a!) do (
    if not defined SERIAL IF %COMPUTERNAME% EQU %%b (ECHO %COMPUTERNAME% is listed to have %%a license
      ECHO .
      ECHO Installing with %%a license . . .
      set SERIAL=!%%a!
    )
  )
)
if defined SERIAL
  %INSTALL% %SERIAL%
  if not errorlevel 1 (ECHO Software installed) else (echo Error during installation)
) else (
  ECHO %COMPUTERNAME% is not listed to have DESKTOP or LAPTOP license
  ECHO Failed to install
)
ENDLOCAL

Last edited by bluesxman (30 Dec 2015 21:47)


cmd | *sh | ruby | chef

Offline

Board footer

Powered by