Ask Your Question
0

Exec resource with reg.exe query and findstr.exe

asked 2015-07-29 03:41:32 -0500

Yeayu gravatar image

Hello all,

Its been a while since I have been facing an issue trying to query a set of registry keys to be able to configure SNMP on some legacy systems (XP, 2003...)

class snmp {
  if $::kernel == 'windows'{
    exec {'permMgmt':
      command   => "C:\\Windows\\System32\\reg.exe delete HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Services\\SNMP\\Parameters\\PermittedManagers /f /v 1",
      onlyif    => "C:\\Windows\\System32\\reg.exe query HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Services\\SNMP\\Parameters\\PermittedManagers /v 1 | findstr.exe localhost",}
    exec {'valComm':
      command   => "C:\\Windows\\System32\\reg.exe add HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Services\\SNMP\\Parameters\\ValidCommunities /f /v ${snmpCommName} /t REG_DWORD /d 0x0000004",
      unless    => "C:\\Windows\\System32\\reg.exe query HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Services\\SNMP\\Parameters\\ValidCommunities /v communityName | findstr.exe 0x0000004",}
  }      
}

The reg.exe query command works as expected, however when its combined with findstr.exe, it fails.

I have tried combining both commands using single/double quotes, invoking cmd.exe /c, defining the command paths, changing the logic (ifonly/unless) and even trying replicate similar issues: http://ask.puppetlabs.com/question/23..., but with not joy.

Any suggestion?

Thanks in advance.

edit retag flag offensive close merge delete

Comments

Have you tried putting the absolute path for findstr.exe?

johnnyb04 gravatar imagejohnnyb04 ( 2015-07-29 11:38:04 -0500 )edit

Yes, I tried that with no joy... It seems like everything I define after the pipe "|" is not picked up

Yeayu gravatar imageYeayu ( 2015-08-01 14:14:46 -0500 )edit

With each command, what if you use cmd to invoke a shell. Your exec attributes would look something like... command => "C:\Windows\System32\cmd.exe /c C:\Windows\System32\reg.exe You might need to invoke a shell for each command, unless, and onlyif.

johnnyb04 gravatar imagejohnnyb04 ( 2015-08-03 07:20:58 -0500 )edit

Thanks Johnyb04, although that did not work, you definitely helped me out here.

Yeayu gravatar imageYeayu ( 2015-08-06 01:27:46 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted
0

answered 2015-08-06 01:26:04 -0500

Yeayu gravatar image

updated 2015-08-06 01:29:50 -0500

This is how I got it working at the end...

class snmp {
  if $::kernel == 'windows'{
    exec {'permMgmt':
    command   => "C:\\Windows\\System32\\reg.exe delete HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Services\\SNMP\\Parameters\\PermittedManagers /f /v 1",
    onlyif    => "C:\\Windows\\System32\\cmd.exe /c reg.exe query HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Services\\SNMP\\Parameters\\PermittedManagers /v 1 | findstr.exe localhost",}
    exec {'valComm':
    command   => "C:\\Windows\\System32\\reg.exe add HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Services\\SNMP\\Parameters\\ValidCommunities /f /v ${snmpCommName} /t REG_DWORD /d 0x00000004",
    unless    => "C:\\Windows\\System32\\cmd.exe /c reg.exe query HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Services\\SNMP\\Parameters\\ValidCommunities /v ${snmpCommName} | findstr.exe 0x4",}
  }      
}
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2015-07-29 03:41:32 -0500

Seen: 313 times

Last updated: Aug 06 '15