sc.exe alternative to find deactivated Windows service? - windows

I use sc.exe to stop/start services on a remote \server in a pre/post build batch. Unfortunately sc does not seem to deliver any information about the service being deactivated or not which leads to an accumulating timeout when using the sc start command on deactivated services. Does anyone know an alternative to check the deactivated state on a remote service in the command line?

This is what you are looking for..
How to test whether a service is running from the command line
look down for the WMI/WMIC options. You will need to modify the command line slightly to attach to a remote machine.
If you need to know the start mode property, add it to the command line like this:
wmic /locale:ms_409 service where (name="RemoteRegistry") get state, StartMode /Value
This produces:
StartMode=Disabled
State=Stopped
I am not marking as duplicate because your wording and needs are a little bit different.

Related

Creating service with short Name and DisplayName breaks WMI for services

Lately I was having problem with WMI on one of the systems (win server 2019). Service list in Task Manager was empty and running Get-WmiObject Win32_Service from powershell was returning Generic failure. After fruitless hours of searching and trying to repair system I gave up and was ready to reinstall system. Then error was reproduced on another machine by accident and I was able to narrow down cause of the problem. After creating windows service with short name and short display name WMI brakes after system restart but only if that service is first (in alphanumeric order) on services list. To reproduce this effect you only need to run
sc create "A1" binpath="D:\foobar.exe" DisplayName="A1" start=disabled
binpath is irrelevant, service doesn't need to be started. DisplayName doesn't need to be identical to name. After that command you need to restart (before restart everything works). After that if you go to services list in task manager it will be empty (probably using WMI to query services list). Now you can run sc delete "A1" and reopen task manager. Everything is back to normal.
Problem was reproduced on Windows Server 2019 and Windows 10 (didn't tried on other versions).
Is this a known bug or what is happening here?
EDIT
I'm not asking about how to use sc. It's doesn't matter. I provided commands that someone can run to reproduce problem. You can create service with that parameters however you want.
You are using the sc command line arguments in the incorrect format.
From MSDN:
Optionvalue
Specifies the value for the parameter named by Optionname. See the Optionname reference for a list of supported values. When a string is to be input, the use of empty quotes means that an empty string is passed in. Note that there is a space between OptionValue and the equal sign.
The correct command should be:
sc create "A1" binpath= "D:\foobar.exe" DisplayName= "A1" start= disabled

Do windows services log why they wont start?

I have a windows service that will not start on a brand new windows server 2012 installation.
When I attempt to start the service, I get this error.
The Foobar service on Local Computer started and then stopped. Some services stop automatically if they are not in use by other services or programs.
I come from a linux background, and don't use windows much. In my troubleshooting attempts, I have been able to gather the following logs.
The requested Performance Counter is not a custom counter, it has to be initialized as ReadOnly.\u000d\u000a at System.Diagnostics.PerformanceCounter.InitializeImpl()\u000d\u000a at System.Diagnostics.PerformanceCounter..ctor(String categoryName, String counterName, String instanceName, Boolean readOnly)\u000d\u000a at System.Diagnostics.PerformanceCounter..ctor(String categoryName, String counterName, Boolean readOnly)\u000d\u000a at foobar.sage.OnStart(String[]
Things that I have tried from the front page of google.
https://stackoverflow.com/a/2081976/1626687
PS C:\Users\sowen> lodctr C:\Windows\Microsoft.NET\Framework\v4.0.30319\CORPerfMonExt.dll
PS C:\Users\sowen> lodctr C:\Windows\Microsoft.NET\Framework64\v4.0.30319\CORPerfMonExt.dll
https://stackoverflow.com/a/14513897/1626687
PS C:\Users\sowen> unlodctr .NETFramework
Removing counter names and explain text for .NETFramework
Updating text for language 009PS
PS C:\Users\sowen> lodctr 'C:\Windows\Inf\.NETFramework\corperfmonsymbols.ini'
https://social.technet.microsoft.com/Forums/windowsserver/en-US/46a21b31-f5fc-4a44-bd4c-c9884a923943/performance-monitor-in-windows-server-2012
Modify HKLM/\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Perflib
Disable Performance Counters = 0
loadctl /r
None of these attempts have resolved the issue. Is there a place where windows shows additional logging why this service wont start?
There doesn't appear to be much more you can do for logging besides looking at the event viewer.
I finally got this running by using InstallUtil instead of sc.exe
sc.exe create foobar obj= "example.com\\someuser" password= "correct-horse-battery-staple" binPath= c:\foobar.exe start= auto depends= "MSMQ"
sc.exe delete foobar
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\InstallUtil.exe /user="example.com\someuser" /password="correct-horse-battery-staple" c:\foobar.exe

Can a Windows service install another Windows service?

I am having trouble when I have one Windows service try to install another Windows service.
Specifically, I have a TeamCity agent running tests for me on a Windows 2008 AWS instance. The tests are written in Java, which shell out to a .bat script to install a service (let's call it Service A), giving it a unique name each time.
The offending line is in the .bat script: sc create "%serviceName%" binPath= %binPath% DisplayName= "%serviceDisplayName:"=%" start= %serviceStartType%. I believe as long as the service name is unique that should work.
And indeed it does work if I run the tests manually on the command line, using an administrator account. Service A is installed, the test completes and Service A is uninstalled at the end.
I have tried running the TeamCity agent as LocalSystem, as Administrator, and as another user that is member of the administrators group. I have also tried disabling UAC completely.
Presumably the problem is access denied type errors, although that is not clear at this point. There are a few avenues to explore still, but it is a simple question really: are processes running as services forbidden from installing other services? Are there special things I have to do to configure the machine/ account to allow it to do this?
The point of the test it to install and use Service A, so workarounds are not relevant - Service A must be operated as a black box.
Thanks!
There are no restrictions on creating services with regards to how the creating process can execute, as long as the process has the appropriate permissions. That is to say, a process could be running as a service and create another service -- the only consideration here is the appropriate permission level.
The problem that often occurs with running batch scripts from within processes (as opposed to directly through user input on the command line) is that the environment expected isn't always the environment that is loaded. In this case, it appears that the env variables referred to in the batch script weren't properly set when running as a service, which of course then caused the service install failure. Correcting the environment loaded when the batch script is shelled out is the correct solution here.

Running a dll using rundll32.exe as a windows service

I was able to run a dll using rundll32.exe .
Now I want to run it as a windows service but it doesn't seem to start and gets timeout.
I am not sure how I could pass the parameters.
Trying something along the lines of ..
sc.exe create service binPath= "c:\windows\system32\rundll32.exe -test.dll -Main"
Trying the suggestions from
creating a service with sc.exe; how to pass in context parameters
but it doesnt seem to work.
Any suggestions?
Thanks,
Karthik
rundll32.exe can't act as service! You need helper like srvany.exe (from old Resource Kit).

Check if a scheduled task is running using vbscript

Is it possible to determine if a scheduled task is running on a local or remote machine using vbscript?
There's a WMI class called Win32_ScheduledJob that has some status fields that might be useful. Especially the ElapsedTime field looks like it might be what you're looking for, assuming that it gets reset when the task stops.
Here's some sample code for looking at the statuses. Just set strComputer to the name of the computer you want to look at (. means the local computer).
Otherwise, if that doesn't work, you might be able to just look at the Schedlgu.txt file in the Windows directory and see if it's started but not yet stopped.
You might be able to get this information with the command-line command schtasks /query... but you'd probably have to grep the output to find only the jobs with a status of "Running".
Details on usage can be found here.

Resources