Pass Array/List to function & iterate variable after each loop

Home Forums Scripting DOS Command Shell Pass Array/List to function & iterate variable after each loop

This topic contains 1 reply, has 2 voices, and was last updated by  Rems 11 months ago.

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts

  • CaTBuS
    Member
    #167375

    I have a WORKING script that does the following:
    1. Lists several production servers to choose from
    2. If a server has more than one app, a list of apps list is presented to choose from
    3. A command is run to query the state of the specific Windows Service associated with that server/app and the status is returned (Service_Name is
    4. Next is an option to either manage the service/restart script/or exit
    5. Selecting the “manage” option gives you the option to remotely Start/Stop/Restart the specified service
    6. Once the service status has changed, it is displayed and options to pass back to the previous screen/restart/or exit are presented.

    Here is my issue. This works fine if I’m only passing one variable (service name) to my function that checks the current service status. When I try to pass more than one service to the function from an array/list, I only get back one variable (the last in the array). Below I will post the working script bits that pertain to this issue and how I’m trying to pass multiple variables. Any assistance would be appreciated. Also, I’m trying to do this without reading/writing data/variables from a text file. I really want to figure out how to do this all internally. I don’t know why, I just do. I know it can be done I’m just missing something. Thanks in advance!!!!

    Code:
    [USER=”68995″]Echo[/USER] OFF
    SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
    SET hName=Server_1
    SET sName=Service_1
    CALL :SvcState
    ECHO %sName% Service is %sState%
    PAUSE
    EXIT /B 0

    :SvcState :: Function to query Service State
    FOR /F “tokens=3 delims=: ” %%a in (‘
    SC \%hName% query %tName% ^| FINDSTR ” STATE”
    ‘) DO ( SET sState=%%a )
    EXIT /B

    Here is the code I’m trying to use to return the corresponding sState for each sName in an Array/List:

    Code:
    [USER=”68995″]Echo[/USER] OFF
    SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION

    SET hName=Server_1
    SET sName[0]=Service_1
    SET sName[1]=Service_2
    SET sName[2]=Service_3
    FOR /F “tokens=2 delims==” %%s in (‘set sName[‘) DO (
    SET tName=%%s
    CALL :SvcState
    ECHO !sState!
    )
    PAUSE

    :SvcState
    FOR /F “tokens=3 delims=: ” %%a in (‘
    SC \%hName% query %tName% ^| FINDSTR ” STATE”
    ‘) DO (
    SET sState=%%a
    )
    EXIT /B

    I’m tried several ways to create the variable SET i=0 then in the function SET /A i+=1 then the SET the sSTate[!i!]=%%a but I cannot get it to work. I don’t know if I’m employing it wrong, creating the variables in the wrong place, etc. Anyway, there it is. I’ll have the same issue if I try to process the functions to Start/Stop/Restart services.

    Again, thanks in advance!


    Rems
    Moderator
    #228479

    Hi,

    Both batch scripts in your post should work. They are working well on my computer.

    In the script below I just added goto:EOF before the sub routines (this could be the cause that the status of the last service was only in the output). And ensured that no unnecessary spaces were added to variables. Also make sure the last value of sState is cleared before checking the next service in case a service could not be found.

    @ ECHO OFF
    SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION

    SET hName=Server_1

    SET “sName[0]=Service_1”
    SET “sName[1]=Service_2”
    SET “sName[2]=Service_3”
    SET “sName[3]=Service_4”

    FOR /F “tokens=2 delims==” %%s in (‘set sName[‘) DO (
    SET “tName=%%s”
    SET “sState=” clear previous value
    CALL :SvcState
    ECHO !tName! Service is !sState!
    )

    PAUSE

    [B]endlocal & goto:EOF ————–[/B]
    :SvcState
    FOR /F “tokens=3 delims=: ” %%a in (
    ‘SC.exe \%hName% query “%tName%” ^| FINDSTR /ic:” state “‘
    ) DO (
    SET “sState=%%a”
    )
    EXIT /B
    [/CODE][CODE]
    @ ECHO OFF
    SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION

    SET hName=Server_1

    SET “sName[0]=Service_1”
    SET “sName[1]=Service_2”
    SET “sName[2]=Service_3”
    SET “sName[3]=Service_4”

    FOR /F “tokens=2 delims==” %%s in (‘set sName[‘) DO (
    SET “tName=%%s”
    SET “sState=” clear previous value
    CALL :SvcState
    ECHO !tName! Service is !sState!
    )

    PAUSE

    endlocal & goto:EOF



    :SvcState
    FOR /F “tokens=3 delims=: ” %%a in (
    ‘SC.exe \%hName% query “%tName%” ^| FINDSTR /ic:” state “‘
    ) DO (
    SET “sState=%%a”
    )
    EXIT /B
    [/CODE]

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.