Thursday, January 27, 2011

how do i create a 1 line command using "sc start" or "net start" on services whose names match a pattern?

I am creating a batch file to start several services, instead of enumerating each "net start" with the exact service name, how can i create the script to run "net start" on any service that begins with "EED"

Thanks.

  • A FOR-IN-DO loop is one way you could do this.

    FOR %%x in ("Service 1" "Service 2" "Service ...") DO net start %%x
    

    The syntax may vary, depending on the service name and optional parameters. Of course, this doesn't meet your requirement to enumerate the list based on service names that start with "EED." You will have to list each service specifically, or use more complex code to get that done. Type FOR /? at a command prompt for more information on the extensive options that this command provides.

    From Sam Erde
  • Here is a batch file to do exactly that:

    @Echo Off
    for /f "tokens=1,2" %%i in ('sc query') do if "%%i"=="SERVICE_NAME:" call :Process %%j
    Goto :EOF
    
    :Process
    set @Name=%1
    if "%@Name:~0,3%"=="EED" (SC start %1
    Echo %1 Started)
    
    :EOF
    

    If you want to change the "prefix search" change the line that says:

    if "%@Name:~0,3%"=="EED" (SC start %1
    

    The prefix to search for is the "EED" and you have to make sure that you change the length number, which is the "~0,3" part... if you want all services that start with "Exchange" then change that number to "~0,8"

    HTH,

    Glenn

    Glenn Sullivan : I just realized that this is not a one line command... sorry. but it is possible to make this a generic batch file that would work like "StartServicesWithPrefix EED" but it would take a bit more work. Glenn
  • If you're open to using PowerShell instead of a batch file, this one liner will fix you up.

    Get-Service EED* | Start-Service

    : this worked perfectly
    tony roth : or wmic service where "name like 'eed$'" call startservice
    From Ben

0 comments:

Post a Comment