[SC] EnumQueryServicesStatus:OpenService FAILED 123

2,452 views
Skip to first unread message

Carlos Tapia

unread,
Feb 12, 2013, 8:19:57 AM2/12/13
to puppet...@googlegroups.com
Hi guys!

I'm trying to perform a service creation using SC  and if this service is already created, using an "unless" we can avoid it. 
Sound very simple, but for any reason, when instructions are called by puppet they are not working instead you call directly, same instructions, using DOS, where works sweet.

Here is part of the code:

$cmd = "C:\\Windows\\System32\\cmd.exe"
$scPath = "C:\\Windows\\System32\\sc.exe"
$findPath = "C:\\Windows\\System32\\find.exe"


# Create Service
  exec{'Create Service nsc-devops':
    command => "${cmd} /c \"${scPath} create nsc-devops binPath= \"C:\\NSClient++\\nsclient++.exe\" DisplayName= \"NSClient Devops\" Start= \"auto\"\"",
    path    => 'c:\windows',
    unless  => "${scPath} query nsc-devops | ${findPath} \"nsc-devops\"",
  }

# Modify Service Description
  exec{'Modify Service nsc-devops':
    command => "${cmd} /c \"${scPath} description nsc-devops \"NSClient++ managed by DevOps\"\"",
    path    => 'c:\windows',
    unless  => "${cmd} /c \"${scPath} query nsc-devops\" | ${findPath} \"nsc-devops\"",
  }


and below the error.

Debug: Exec[Create Service nsc-devops](provider=windows): Executing check 'C:\Windows\System32\cmd.exe /c "C:\Windows\System32\sc.exe query nsc-devops" | C:\Windows\System32\find.exe "nsc-devops"'
Debug: Executing 'C:\Windows\System32\cmd.exe /c "C:\Windows\System32\sc.exe query nsc-devops" | C:\Windows\System32\find.exe "nsc-devops"'
Debug: /Stage[main]/Nagios-devops/Exec[Create Service nsc-devops]/unless: [SC] EnumQueryServicesStatus:OpenService FAILED 123:
Debug: /Stage[main]/Nagios-devops/Exec[Create Service nsc-devops]/unless:
Debug: /Stage[main]/Nagios-devops/Exec[Create Service nsc-devops]/unless: The filename, directory name, or volume label syntax is incorrect.
Debug: Exec[Create Service nsc-devops](provider=windows): Executing 'C:\Windows\System32\cmd.exe /c "C:\Windows\System32\sc.exe create nsc-devops binPath= "C:\NSClient++\nsclient++.exe" DisplayName= "NSClient Devops" Sta
rt= "auto""'
Debug: Executing 'C:\Windows\System32\cmd.exe /c "C:\Windows\System32\sc.exe create nsc-devops binPath= "C:\NSClient++\nsclient++.exe" DisplayName= "NSClient Devops" Start= "auto""'
Notice: /Stage[main]/Nagios-devops/Exec[Create Service nsc-devops]/returns: [SC] CreateService FAILED 1073:
Notice: /Stage[main]/Nagios-devops/Exec[Create Service nsc-devops]/returns:
Notice: /Stage[main]/Nagios-devops/Exec[Create Service nsc-devops]/returns: The specified service already exists.
Error: C:\Windows\System32\cmd.exe /c "C:\Windows\System32\sc.exe create nsc-devops binPath= "C:\NSClient++\nsclient++.exe" DisplayName= "NSClient Devops" Start= "auto"" returned 49 instead of one of [0]
C:/Program Files (x86)/Puppet Labs/Puppet/puppet/lib/puppet/util/errors.rb:60:in `fail'

If I run the line that puppet marks as faulted, this is windows' answer:
C:\>C:\Windows\System32\cmd.exe /c "C:\Windows\System32\sc.exe query nsc-devops" | C:\Windows\System32\find.exe "nsc-devops"
SERVICE_NAME: nsc-devops

Any idea???
Thanks for your help!

BR,
Carlos!

Josh Cooper

unread,
Feb 12, 2013, 11:58:03 AM2/12/13
to puppet...@googlegroups.com
On Tue, Feb 12, 2013 at 5:19 AM, Carlos Tapia <carlos.tap...@gmail.com> wrote:
Hi guys!

I'm trying to perform a service creation using SC  and if this service is already created, using an "unless" we can avoid it. 

This is the resource I use to install the jenkins agent service:

  exec { 'install-service':
    command   => "sc.exe create ${jenkins::servicename} start= demand binpath= ${jenkins::serviceexe} displayname= ${jenkins::servicename}",
    path      => "${path}",
    logoutput => true,
    unless    => "sc.exe query ${jenkins::servicename}",
}

Note that `sc.exe query <servicename>` will return zero if the service exists, preventing the service from being reinstalled.

 
Sound very simple, but for any reason, when instructions are called by puppet they are not working instead you call directly, same instructions, using DOS, where works sweet.

Here is part of the code:

$cmd = "C:\\Windows\\System32\\cmd.exe"

It's not necessary to wrap the sc.exe command in cmd.exe. That sort of thing is only necessary when trying to execute built-ins like echo, mkdir, etc. So for example, to echo, you have to do: cmd.exe /c echo 'foo' 

$scPath = "C:\\Windows\\System32\\sc.exe"
$findPath = "C:\\Windows\\System32\\find.exe"

Notice in my exec resource above, I set path => ${path} and then executed 'sc.exe'. This tells the exec resource to use the `path` fact, so that I don't have to specify where `sc.exe` lives. For example, if SYSTEMDRIVE is 'D:'

# Create Service
  exec{'Create Service nsc-devops':
    command => "${cmd} /c \"${scPath} create nsc-devops binPath= \"C:\\NSClient++\\nsclient++.exe\" DisplayName= \"NSClient Devops\" Start= \"auto\"\"",
    path    => 'c:\windows',
    unless  => "${scPath} query nsc-devops | ${findPath} \"nsc-devops\"",
  }

# Modify Service Description
  exec{'Modify Service nsc-devops':
    command => "${cmd} /c \"${scPath} description nsc-devops \"NSClient++ managed by DevOps\"\"",
    path    => 'c:\windows',
    unless  => "${cmd} /c \"${scPath} query nsc-devops\" | ${findPath} \"nsc-devops\"",
  }


and below the error.

Debug: Exec[Create Service nsc-devops](provider=windows): Executing check 'C:\Windows\System32\cmd.exe /c "C:\Windows\System32\sc.exe query nsc-devops" | C:\Windows\System32\find.exe "nsc-devops"'
Debug: Executing 'C:\Windows\System32\cmd.exe /c "C:\Windows\System32\sc.exe query nsc-devops" | C:\Windows\System32\find.exe "nsc-devops"'
Debug: /Stage[main]/Nagios-devops/Exec[Create Service nsc-devops]/unless: [SC] EnumQueryServicesStatus:OpenService FAILED 123:
Debug: /Stage[main]/Nagios-devops/Exec[Create Service nsc-devops]/unless:
Debug: /Stage[main]/Nagios-devops/Exec[Create Service nsc-devops]/unless: The filename, directory name, or volume label syntax is incorrect.

So error 123 is "The filename, directory name, ... is incorrect". That's because cmd.exe is trying to execute "C:\Windows\System32\sc.exe query nsc-devops", instead of executing C:\Windows\System32\sc.exe with the arguments 'query' and 'nsc-devops'

Debug: Exec[Create Service nsc-devops](provider=windows): Executing 'C:\Windows\System32\cmd.exe /c "C:\Windows\System32\sc.exe create nsc-devops binPath= "C:\NSClient++\nsclient++.exe" DisplayName= "NSClient Devops" Sta
rt= "auto""'
Debug: Executing 'C:\Windows\System32\cmd.exe /c "C:\Windows\System32\sc.exe create nsc-devops binPath= "C:\NSClient++\nsclient++.exe" DisplayName= "NSClient Devops" Start= "auto""'
Notice: /Stage[main]/Nagios-devops/Exec[Create Service nsc-devops]/returns: [SC] CreateService FAILED 1073:
Notice: /Stage[main]/Nagios-devops/Exec[Create Service nsc-devops]/returns:
Notice: /Stage[main]/Nagios-devops/Exec[Create Service nsc-devops]/returns: The specified service already exists.
Error: C:\Windows\System32\cmd.exe /c "C:\Windows\System32\sc.exe create nsc-devops binPath= "C:\NSClient++\nsclient++.exe" DisplayName= "NSClient Devops" Start= "auto"" returned 49 instead of one of [0]
C:/Program Files (x86)/Puppet Labs/Puppet/puppet/lib/puppet/util/errors.rb:60:in `fail'

I think you need to use something other than 'create' to modify the description for a service that already exists.

If I run the line that puppet marks as faulted, this is windows' answer:
C:\>C:\Windows\System32\cmd.exe /c "C:\Windows\System32\sc.exe query nsc-devops" | C:\Windows\System32\find.exe "nsc-devops"
SERVICE_NAME: nsc-devops


Just do `c:\windows\system32\sc.exe query nsc-devops`, or just `sc.exe query nsc-devops` with the path trick above. If the service exists, it will return exit code 0, otherwise non-zero.
 
Any idea???
Thanks for your help!

BR,
Carlos!

--
You received this message because you are subscribed to the Google Groups "Puppet Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users...@googlegroups.com.
To post to this group, send email to puppet...@googlegroups.com.
Visit this group at http://groups.google.com/group/puppet-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Josh

--
Josh Cooper
Developer, Puppet Labs
Reply all
Reply to author
Forward
0 new messages