Answer the question
In order to leave comments, you need to log in
How to check for the presence of a service and if it is not there, run a batch file?
Greetings. There is a certain exe-shnik that I would like to run on all computers in the domain when turned on. At the moment, I could only implement this as a logon script through GPO in this way: the first batch file lies in the autorun and launches vbs, which, in turn, already creates its own hidden shell and launches an exe file with arguments there. To make it clear, here is the script:
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run "cmd.exe /c %path_to_folder%\start.bat", 0, false
The service is created and started manually using nssm. The automation of this process seems to me something like this -
1) Distribute the necessary files through the GPO
2) Push a script into the autorun that checks for the presence of the service, and, in case of its absence, launches nssm with the installation parameters. Question - how to do it? In PowerShelle and VBS, I'm not strong, unfortunately
Answer the question
In order to leave comments, you need to log in
From cmd, you can check the status of the service with sc and parse its output.
Or you can use tasklist to display a list of running processes and find the one you need, if it is not there, then the service is not running.
You can start any process as a service using srvany and instrvs. These utilities are included with Microsoft's Windows Server 2003 Resource Kit. They worked quite well on Win7, they don’t know further.
it's easier to terminate the service and then start than to monitor
Option 1
Approximately you can use this function.
Function ServiceExists([string] $ServiceName) {
[bool] $Return = $False
if ( Get-WmiObject -Class Win32_Service -Filter "Name='$ServiceName'" ) {
$Return = $True
}
Return $Return
}
if (!ServiceExists ServiceName)
{
run some command
}
if (!Get-Service "ServiceName" -ErrorAction SilentlyContinue)
{
run some command
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question