Start windows service with parameters

Start-Service

Starts one or more stopped services.

Syntax

Description

The Start-Service cmdlet sends a start message to the Windows Service Controller for each of the specified services. If a service is already running, the message is ignored without error. You can specify the services by their service names or display names, or you can use the InputObject parameter to supply a service object that represents the services that you want to start.

Examples

Example 1: Start a service by using its name

This example starts the EventLog service on the local computer. The Name parameter identifies the service by its service name.

Example 2: Display information without starting a service

This example shows what would occur if you started the services that have a display name that includes «remote».

The DisplayName parameter identifies the services by their display name instead of their service name. The WhatIf parameter causes the cmdlet to display what would happen when you run the command but does not make changes.

Example 3: Start a service and record the action in a text file

This example starts the Windows Management Instrumentation (WMI) service on the computer and adds a record of the action to the services.txt file.

First we use Get-Service to get an object that represent the WMI service and store it in the $s variable. Next, we start the service. Without the PassThru parameter, Start-Service does not create any output. The pipeline operator (|) passes the object output by Start-Service to the Format-List cmdlet to format the object as a list of its properties. The append redirection operator (>>) redirects the output to the services.txt file. The output is added to the end of the existing file.

Example 4: Start a disabled service

This example shows how to start a service when the start type of the service is Disabled.

The first attempt to start the Telnet service (tlntsvr) fails. The Get-CimInstance command shows that the StartMode property of the Tlntsvr service is Disabled. The Set-Service cmdlet changes the start type to Manual. Now, we can resubmit the Start-Service command. This time, the command succeeds. To verify that the command succeeded, run Get-Service .

Читайте также:  Mac os icons xfce

Parameters

Prompts you for confirmation before running the cmdlet.

Type: SwitchParameter
Aliases: cf
Position: Named
Default value: False
Accept pipeline input: False
Accept wildcard characters: False

Specifies the display names of the services to start. Wildcard characters are permitted.

Type: String [ ]
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: True

Specifies services that this cmdlet omits. The value of this parameter qualifies the Name parameter. Enter a name element or pattern, such as s* . Wildcard characters are permitted.

Type: String [ ]
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: True

Specifies services that this cmdlet starts. The value of this parameter qualifies the Name parameter. Enter a name element or pattern, such as s* . Wildcard characters are permitted.

Type: String [ ]
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: True

Specifies ServiceController objects representing the services to be started. Enter a variable that contains the objects, or type a command or expression that gets the objects.

Type: ServiceController [ ]
Position: 0
Default value: None
Accept pipeline input: True
Accept wildcard characters: False

Specifies the service names for the service to be started.

The parameter name is optional. You can use Name or its alias, ServiceName, or you can omit the parameter name.

Type: String [ ]
Aliases: ServiceName
Position: 0
Default value: None
Accept pipeline input: True
Accept wildcard characters: False

Returns an object that represents the service. By default, this cmdlet does not generate any output.

Type: SwitchParameter
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False

Shows what would happen if the cmdlet runs. The cmdlet is not run.

Type: SwitchParameter
Aliases: wi
Position: Named
Default value: False
Accept pipeline input: False
Accept wildcard characters: False

Inputs

System.ServiceProcess.ServiceController, System.String

You can pipe objects that represent the services or strings that contain the service names to this cmdlet.

Outputs

None, System.ServiceProcess.ServiceController

This cmdlet generates a System.ServiceProcess.ServiceController object that represents the service, if you specify PassThru. Otherwise, this cmdlet does not generate any output.

Notes

This cmdlet is only available on Windows platforms.

  • You can also refer to Start-Service by its built-in alias, sasv . For more information, see about_Aliases.
  • Start-Service can control services only if the current user has permission to do this. If a command does not work correctly, you might not have the required permissions.
  • To find the service names and display names of the services on your system, type Get-Service . The service names appear in the Name column, and the display names appear in the DisplayName column.
  • You can start only the services that have a start type of Manual, Automatic, or Automatic (Delayed Start). You cannot start the services that have a start type of Disabled. If a Start-Service command fails with the message Cannot start service \ on computer , use Get-CimInstance to find the start type of the service and, if you have to, use the Set-Service cmdlet to change the start type of the service.
  • Some services, such as Performance Logs and Alerts (SysmonLog) stop automatically if they have no work to do. When PowerShell starts a service that stops itself almost immediately, it displays the following message: Service \ start failed.
Читайте также:  Линукс чем открыть pdf

Pass an argument to a Windows Service at automatic startup

I found some similar questions, yet the answers doesn’t seem to help in my case. I wish to configure my service for automatic startup with 1 argument.

My service OnStart method looks like this:

So I registered my service with the int parameter (8081) after the service file name like in the screenshot below (as suggested in other answers to similar questions).

When I start the service, I always get the error message «The service must be started. «.

If I type an integer in the «Start parameters:» field, the service starts normally.

How can I have Windows automatically start my service with one argument (8081)?

Edit:

I did some more tests. Added logging of the args[] parameter. It is empty. Also I tried to add extra parameters like in this image:

I tried both with and without double-quotes around the arguments, but they are not passed to the service.

2 Answers 2

When a service is started, there are two distinct argument lists.

The first is taken from the command line, as shown in «path to executable» in the Services administrative tool. This is where you have put the argument 8081 as shown in the screenshot.

In a .NET service, these arguments are passed to the Main() function.

The second is the service start parameters, which are provided when the service is manually started. If you use the Services administrative tool to start the service, this argument list is taken from the «start parameters» field, which in your screenshot is empty.

In a .NET service, these arguments are passed to the OnStart() function.

Читайте также:  Windows portable devices что будет если удалить

In your scenario, therefore, you should modify Main() so that it passes the command-line arguments on to your service class. Typically you would provide these in the constructor, although you could use a global variable if you preferred.

(See also this answer for a more detailed description of a service startup.)

How to start a service with certain start parameters on Windows

How do I start a service with certain parameters? In another question I have found

but if I try this, net throws a syntax error at me.

What am I doing wrong?

Edit: To clarify net start servicename works just fine, but I need to pass parameters to the service. I can do this manually in services.msc by filling in a start parameter before starting the service. But how can I do this from a script?

Another edit: Sorry, but my question was misleading. In my tests, I had many more parameters and it’s not the /blah net start complains about. In fact, anything starting with a slash is fine. So net start servicename /blah works, net start servicename blah doesn’t work. Since I need net start servicename /foo bar , it’s the bar that is the problem.

6 Answers 6

NET START won’t allow you to pass arbitrary parameters as far as I know (it appears they have to be prefixed with a /), I believe the correct way is to modify the appropriate registry key and stop/start the service. Assuming your custom service is called MyService:

Really though, a better way is to store your configuration somewhere the service knows about, like perhaps the registry or file system, and read whatever values you need. As you can see customizing the command line is a bit more painful than it should be.

If this is by chance a .NET developed service, you could create n copies of the executable on your file system, and have each automatically get the parameters from the appropriate app.config file.

It sounds like you may have stepped too far outside the Win32 box. Since this sounds like custom development any of the other offered solutions, while perhaps not your ideal, will work correctly and are a safer way to go.

Оцените статью