2

I'm trying to use the find command and errorlevel to evaluate the result of a command

Setlocal EnableDelayedExpansion
...
nssm status MyService | find "SERVICE_STOPPED"
if !errorlevel! equ 0 (
   echo MyService is not running
)

Since I know the command "nssm status MyService" returns "SERVICE_STOPPED" I would expect find to set the errorlevel to 0. Instead it's set to 1. Why?

Ross Ridge
  • 38,414
  • 7
  • 81
  • 112
Gep
  • 848
  • 13
  • 29
  • Have you tried `nssm status MyService | more | find "SERVICE_STOPPED"`? I think `more` has some conversion capabilities... – aschipfl Apr 12 '16 at 19:29

1 Answers1

1

A deleted answer showed the encoding of nssm output (I don't have it, so I can't verify). Every letter is encoded with two bytes (the second one being 0x00). So this (admittedly ugly) workaround should work:

nssm status MyService | findstr "S.E.R.V.I.C.E._.S.T.O.P.P.E.D"
if !errorlevel! equ 0 (
    echo MyService is not running
)
ManoDestra
  • 6,325
  • 6
  • 26
  • 50
Stephan
  • 53,940
  • 10
  • 58
  • 91