1

Here is the bat command which uses for checking service status and according to condition either stop the service or start service.

//Bat Command

for /F "tokens=3 delims=: " %%H in ('sc query wuauserv ^| findstr "STATE"') do ( if /I "%%H" NEQ "Running" ( net start wuauserv ) else (echo "Hello World"))

//JenkinsFile

bat 'for /F "tokens=3 delims=: " %%H in ('sc query wuauserv ^| findstr "STATE"') do ( if /I "%%H" NEQ "Running" ( net start wuauserv ) else (echo "Hello World"))'

when I put the batch command in the Jenkins file showing a syntax error. in the end when I remove the syntax Error but now command did not work properly

//Without Syntax Error Jenkins file

bat 'for /F "tokens=3 delims=: " %%H in ("sc query wuauserv ^| findstr "STATE"") do ( if /I "%%H" NEQ "Running" ( net start wuauserv ) else (echo "Hello World"))'

I just want that batch command to fit in the Jenkins file and work properly.

bitee
  • 57
  • 7
  • Why are you using a `for` loop at all? – Compo Jun 23 '22 at 16:20
  • @Compo I get this command from StackOverflow and its works fine for me I just need to put this command in the Jenkins file without syntax error – bitee Jun 23 '22 at 16:33
  • There can be used the command line: `%SystemRoot%\System32\sc.exe query wuauserv | %SystemRoot%\System32\findstr.exe /I /R /C:"^ *STATE *: .*RUNNING" >nul && echo Windows Update Service is already running.|| %SystemRoot%\System32\sc.exe start wuauserv` That is faster as all executables are specified with their fully qualified file names. There is not started one more `cmd.exe` in background as `for /F` does. There is used `sc.exe` to start the service instead of `net.exe`. See also [single line with multiple commands using Windows batch file](https://stackoverflow.com/a/25344009/3074564). – Mofi Jun 23 '22 at 16:49
  • By the way: What do you think how Jenkins does know where the command line to write into a batch file created in the directory for temporary files begins and where it ends? Yes, the beginning is marked with `'` and the end is marked with `'`. But do you see now the problem? There are two more `'` inside the command line to write into the temporarily created and executed batch file. So Jenkins interprets the string with four instead of just two `'` different to you. – Mofi Jun 23 '22 at 16:54

2 Answers2

1

The command you are using in your batch file is unnecessary for the purposes you are using it.

Perhaps the following batch file command would serve you better:

@%SystemRoot%\System32\sc.exe Query wuauserv | %SystemRoot%\System32\findstr.exe /R /C:"STATE  *: 1  *" 1>NUL && (Start %SystemRoot%\System32\sc.exe Start wuauserv) || Echo "Hello World"

Please note however that || Echo "Hello World" is probably not necessary as is, because the host would have exited before you ever have time to have read that.

@%SystemRoot%\System32\sc.exe Query wuauserv | %SystemRoot%\System32\findstr.exe /R /C:"STATE  *: 1  *" 1>NUL && Start %SystemRoot%\System32\sc.exe Start wuauserv

You may note that I've used 1, (STOPPED), instead of RUNNING for my check. Not only does that keep the command non language dependent, it is possible that 2 (START_PENDING), 3 (STOP_PENDING), and 4 (RUNNING), are returned, non of which are in a current state to be started. Your method treats anything which is not RUNNING as if it can be started, but as you can see above, that is not the case.

Compo
  • 36,585
  • 5
  • 27
  • 39
  • Just for your information: *Jenkins* captures all output to `stdout` and `stderr` by any executable or script on running a job and writes them into a log file. This log file can be viewed by the user later at any time via the web interface Jenkins offers for the users. So the output of a positive message for a step of a job is often quite useful on viewing later the log to know what worked until something failed during the job execution. – Mofi Jun 23 '22 at 17:08
  • Thanks for the information @Mofi, I'll leave both versions in place, as the single line batch file command can be used equally as well by non jenkins users. And for Command Prompt users, just removing the harmless `@` from the beginning of the line is an option. – Compo Jun 23 '22 at 17:12
  • @Compo I ran your command exactly what you posted above but it always gives me hello world not stopping or starting the Service. – bitee Jun 23 '22 at 17:52
  • @Compo Where I can put condition if running stop the service or already stopped if stopped start service or already start. – bitee Jun 23 '22 at 17:55
  • If it always produces that message, then the service is clearly not in a stopped state. As for you second comment, I've got no intention of writing code for you outside of that which you initially submitted in your question. My answer was to show you how to do it without a `For` loop, and therefore the single quoting issue you were falling foul of. If you have a new question please submit one, however please first use the search facility. Your specific task has been asked and answered under the [[tag:cmd]] and [[tag:batch-file]] tags many times before; and duplicate questions will be closed. – Compo Jun 23 '22 at 18:10
  • @Compo I didn't ask in the first place to remove 'For' from the command I just want to fit that command in the Jenkins file like you can see in my Question heading. Please, Compo, help me I spend almost 2 days on this and still 0 progress. – bitee Jun 23 '22 at 19:30
  • No you didn't, I implied that a `For` loop wasn't needed, and you questioned it in the comments, telling me it was working code from this site. The issue was with the single quote characters used in that command, so I offered you a better alternative without those characters. If you wanted to instead change your own code to use `"usebackq tokens=3 delims=: "`, then replace the `('` and `')` with `(\`` and `\`)` feel free, but it will still only do what my code does, only much less robustly or efficiently. It will not perform the task in your new question, will it? – Compo Jun 23 '22 at 20:02
  • @Compo I answered my question you can verify it. Thanks for your time. – bitee Jun 24 '22 at 15:48
  • @bitee, this particular area is not where you should ask such things. As I said, not only does my answer work better and more efficiently, I've already given you a solution to modify your original code so that it would still work from a normal batch file or from Jenkins. What you've done, will work, but is simply escaping the problem characters we've already highlighted for you. Feel free to continue to ignore the advice you are given, and go with your own workaround, that is after all your choice, but to ask me to verify your workaround, when both my answers are better is just a little rude. – Compo Jun 24 '22 at 16:01
1

I figure it out by simply skipping the ('') because these are used again in the command causing the syntax error so I skip them by using \ in the command. the command now works perfectly fine without groovy syntax Error.

//Here is the Example

bat 'for /F "tokens=3 delims=: " %%H in (\'sc query wuauserv ^| findstr "STATE"\') do ( if /I "%%H" EQU "Running" ( net stop wuauserv ) else (echo "Service already stopped"))'
bitee
  • 57
  • 7