I'm trying to write a power shell script to install a service but the service requires an extra command line paramiter passed to it. Im having trouble getting this passed over.
Here is the service installer that uses the parameter;
this.serviceInstaller.ServiceName = string.Format("My brill service {0}",this.Context.Parameters["environment"])
And I have tried passing the paramiter in two ways;
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\InstallUtil.exe "C:\foo\bar.exe /environment:tomtest"
(this gives the error "invalid directory on url")
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\InstallUtil.exe /environment:tomtest "C:\foo\bar.exe"
(this just dosent change the service name)
Any ideas? Thanks
I was very close. An equals sign = has to be used to assign the value of the parameter (not a colon :):
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\InstallUtil.exe /environment=tomtest "C:\foo\bar.exe"
Related
I'm trying to launch a web page inside a vitual android device. The address of the page takes multiple querystring parameters. For some reason when passing the url in all parameters after the first & are missing (including the &).
I have a very simple C# WinForm app to test this with. I am using MadBee NuGet package to send the commands to the android VM.
When I send the command I see the url loaded but as I described, it is missing the parameters that come after he first &
Below is a snippet of the code I am calling:
command = "am start -a android.intent.action.VIEW -d http://w18299:8009/Assignment/manage?assigner=57072352&unitID=6443&secret=asdasdasdasdasd&assignee=57072352";
ConsoleOutputReceiver creciever = new ConsoleOutputReceiver();
device.ExecuteShellCommand(command, creciever);
Does anyone have any ideas as to why the parameters would not make it across to Android?
Your parameters "make it across to Android" just fine. What you failed to realize is that your command is getting parsed by the Android shell on the device side and & has a special meaning for it. To stop the shell from treating & as special symbol use quotes like this:
command = "am start -a android.intent.action.VIEW -d 'http://w18299:8009/Assignment/manage?assigner=57072352&unitID=6443&secret=asdasdasdasdasd&assignee=57072352'";
I was wondering if it were possible to find out which windows service has run an executable?
I've got two different services running from the same exe, doing different things. The main method of the program detects a command line parameter and will either start the console app (if running in Environment.UserInteractive), or start one of the two possible services. I can't find a good way to discern which service is running so I can choose the correct service to start in the code. Passing in .exe parameters in the image path of the windows service doesn't seem to work. The services are running on a server and starting automatically, so doing it manually isn't really an option.
I'd really like to avoid having to have two different projects with different executables, so any way I can notify the program of which service to run would be great.
You can pass arguments in the ImagePath. I know I have done it at some point using a .net windows service, but as I recall, I had to install the service using something other than the standard .Net installer.
The .Net installer adds quotes around whatever you pass, which makes ImagePath go from C:\test\test.exe -arguments to "C:\test\test.exe -arguments" when it should be "C:\test\test.exe" -arguments.
Check out WiX, sc.exe or CreateService to get the correct registry value.
To test, install your service as usual and browse in regedit to HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\yourServiceName and edit ImagePath. For an example of how it should look, check out HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\seclogon.
You could create a Mutex in your service at the point it starts, using a name unique to the version it is running. If the mutex is obtained then you know it isn't running. If it can't be obtained then the service is already running.
You could then start your services through a new process that first tries to obtain the mutex for the first service and if it can't obtain it it starts the second.
So, you start ServiceRunner.exe -foo. A mutex called "foo" is obtained, so you release the Mutex and ServiceRunner.exe starts Service.exe -foo.
If the mutex is not obtained you then try to obtain a mutex called "bar" and follow the same process.
This is a nasty solution, and would require your to create a new exe that simply tries to start the services.
Have your service share its start state( temp file, registry key or other method, it could even write this as html to a web server... )
ServiceController yourService = new ServiceController( "YourServiceName" , "YourMachine" );
if( yourService.Status == ServiceControllerStatus.Stopped )
{
yourService.Start();
}
I'm looking for a way to figure out the command-line arguments of any Windows service.
For a non-service process, the command-line arguments can be found in the Windows Task Manager, or programmatically by using WMI as shown in this post.
Unfortunately, these two solutions don't work for a Windows service that is started by the ServiceController.Start(String[] args) method. Both of them show only the executable file path on the command-line, even though some arguments were passed in.
What is the difference
between two scenarios (a service vs.
a non-service process)?
Is there a
way to figure out the arguments of the
Windows service?
I also tried creating a simple service that just logs any command-line arguments it has to the event log. I started it using "sc.exe start <my service> <arg1>" and verified that <arg1> was written to the event log.
However, none of the solutions has worked for me. I still only saw the path to the executable file. My OS version is Windows Server 2008 R2 SP1 x64 Enterprise.
There are two types of arguments for services:
Arguments that were passed on the process start command line. You can get to those easily using Process Explorer, etc.
Arguments that were passed to the ServiceMain function. This is the WIndows API that a service is supposed to implement. The .NET equivalent is ServiceBase.OnStart. This is what is used when you do an SC START \[arguments\]. This has nothing to do with "command line process arguments".
The second type of parameters is probably only known by the service itself, if the implementation makes any use of it which is not the case for many services. I don't think Windows keep track of this when we look at low level Windows structures like the PEB: Process and Thread Structures (MSDN), even the undocumented parts of it, Undocumented functions of NTDLL.
You can find the service EXE file details and edit or just see the commandline options in the registry entry for the service. You'll find that under
HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\services
Be sure to restart the Services window if you decide to change this as it won't reread it live.
Try the Process Explorer application from Sysinternals
It is like Task Manager, only it lists all the running processes. Select your service and see its properties.
A service process is not started as a usual EXE file. Even more, a service process could be just a .dll file. See: Windows service (Wikipedia).
Many appear in the processes list in the Windows Task Manager, most often with a username of SYSTEM, LOCAL SERVICE or NETWORK SERVICE, though not all processes with the SYSTEM username are services. The remaining services run through svchost.exe as DLLs loaded into memory.
Just override the ServiceBase.OnStart(string[] args) method. See more:
ServiceBase.OnStart(String[]) Method (MSDN)
Using Powershell you can call
(Get-CimInstance Win32_Service -Filter 'Name = "<my service>"').PathName
to get the full command line of the service (it returns file and arguments)
Just replace <my service> with the name of the desired service.
For example:
(Get-CimInstance Win32_Service -Filter 'Name = "Dnscache"').PathName
returns "C:\WINDOWS\system32\svchost.exe -k NetworkService -p"
What is the best way to install a windows service written in C# (in the standard way) on a remote machine, where I need to provide the username and password it should run as?
I am going to run it from MSBuild as part of integration tests.
EDIT: I don't have an msi and I don't want to create one.
You can use the SC command.
sc.exe \\remotecomputer create newservice binpath= C:\Windows\System32\Newserv.exe start= auto obj= DOMAIN\username password= pwd
(Note the spaces after the equals signs are important)
Creates a service entry in the registry and Service Database.
SYNTAX:
sc create [service name] [binPath= ] <option1> <option2>...
CREATE OPTIONS:
NOTE: The option name includes the equal sign.
type= <own|share|interact|kernel|filesys|rec>
(default = own)
start= <boot|system|auto|demand|disabled>
(default = demand)
error= <normal|severe|critical|ignore>
(default = normal)
binPath= <BinaryPathName>
group= <LoadOrderGroup>
tag= <yes|no>
depend= <Dependencies(separated by / (forward slash))>
obj= <AccountName|ObjectName>
(default = LocalSystem)
DisplayName= <display name>
password= <password>
Installutil called from WMI invoked from Powershell is one way to go.
We used to use PsExec to do everything on remote machine.
At this time I saw naother solution called PowerShell Remoting, but haven't tried myself.
It might be worth you checking out a utility I wrote which lets you install/uninstall/start/stop windows services on a local or remote machine. If you have a feature you need added let me know (comment/github issues or PM and I'll happily oblige).
ServiceConfigurator on GitHub
As #Raj More suggested, it uses WMI invoked from .NET.
I am working on an application which starts as a service but only if a commandline switch tells it to (otherwise a standard form is opened). So when the service is started by Windows at bootup, it must pass this commandline option or the service fails to start.
I would like to have the installer (ServiceProcessInstaller) add a commandline option so that when the service is started it adds the commandline option to the command.
Example:
MyService.exe -commandlineoption
I thought this was what the ServiceProcessorInstaller.Context property was for, but that is for the arguments that were executed on InstallUtil.
Any suggestions?
When I've added command-line options to services, I've always defaulted to running as a service. However, I know that the opposite is possible because it's how SvcHost works: it's an EXE that's always configured to load DLL's as services.
A service is only installed once per release. It sounds like you are talking about passing a command line argument to the service when it's started.
You can pass command line arguments to the service when you start it using the ServiceController.Start method:
using (var controller = new ServiceController("servicename")) {
controller.Start(arg0, arg1);
}