Coming Soon: GET:IT Endpoint Management 1-Day Conference on September 28th at 9:30 AM ET Coming Soon: GET:IT Endpoint Management 1-Day Conference on September 28th at 9:30 AM ET

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

Viewing 1 post (of 1 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!

Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.

Live Webinar: Active Directory Security: What Needs Immediate Priority!Live on Tuesday, October 12th at 1 PM ET

Attacks on Active Directory are at an all-time high. Companies that are not taking heed are being punished, both monetarily and with loss of production.

In this webinar, you will learn:

  • How to prioritize vulnerability management
  • What attackers are leveraging to breach organizations
  • Where Active Directory security needs immediate attention
  • Overall strategy to secure your environment and keep it secured

Sponsored by: