2

I want to add via Batch script a new service-dependency to an existing service with old dependencies, without deleting these dependencies.

I know the command sc config ServiceA depend= ServiceB/ServiceC/ServiceD can add multiple dependencies, but I don't know how to use it to - for example - add ServiceD dependency to a service that depends on ServiceB and ServiceC, with the same result and without mentioning the old dependencies (since I don't know them).

What is the way to do that? And - how to revert the change (also via Batch)?

I have no much experience with Batch, by the way.

Reflection
  • 123
  • 1
  • 4

1 Answers1

2

Find out the existing dependencies, add your new one and write the whole thing back, say you have a service called w3svc, you can use:

sc.exe  qc  w3svc

or in PowerShell:

(get-service w3svc | Select ServicesDependedOn).ServicesDependedOn

where you can loop through these and build a new sc.exe command to execute.

Example:

 $serviceName = "w3svc"
 $cmd = "config $serviceName depend= "
 (gsv $serviceName | Select ServicesDependedOn).ServicesDependedOn | % {$cmd += $_.Name + "/"}
 $cmd += "myMasterService"
 invoke-expression "sc.exe $cmd"

We are building a command string from the existing services and add your own.

To remove yours do the same thing, but in the loop exclude your service.

 $serviceName = "w3svc"
 $cmd = "config $serviceName depend= "
 (gsv $serviceName | Select ServicesDependedOn).ServicesDependedOn | % {
      if ($_.Name -ne "myMasterService")
      {
           $cmd += $_.Name + "/"
      }
 }
 # remove the last slash
 $cmd = $cmd -replace "/$", ""   
 invoke-expression "sc.exe $cmd"

I haven't tested the actual sc.exe commands, but I assume you know what you are doing.

Peter Hahndorf
  • 13,763
  • 3
  • 37
  • 58
  • Thanks! Can you write the Batch commands to run if I don't want to use PowerShell? It will really really help! – Reflection Mar 04 '15 at 18:16
  • 1
    Sorry, it's 2015, not the 1980s. I haven't done batch files in a long time and doing this in a batch would be much more difficult anyway. – Peter Hahndorf Mar 04 '15 at 18:27