Windows batch file execution

291 views
Skip to first unread message

Dimitri Yioulos

unread,
Jul 29, 2015, 3:48:50 PM7/29/15
to Puppet Users
Hello, all.

I'm trying to execute the following batch file (sageset.bat) on Win2k8 servers:

@ECHO OFF
REM Enable components to cleanup
REG ADD "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Active Setup Temp Folders" /v StateFlags0100 /d 2 /t REG_DWORD /f
REG ADD "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\BranchCache" /v StateFlags0100 /d 2 /t REG_DWORD /f
REG ADD "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Downloaded Program Files" /v StateFlags0100 /d 2 /t REG_DWORD /f
REG ADD "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\GameNewsFiles" /v StateFlags0100 /d 2 /t REG_DWORD /f
~

It makes changes to registry settings so that the disk cleanup utility will do a more thorough job of cleanup.  It's the equivalent of running "cleanmgr /d c: /sageset:100", which opens a dialog box that presents a list of files to clean that one can check.

The following is in my manifest:

exec { 'sageset.bat':
         path => $::path,
         command => "cmd.exe /c c:\temp\sageset.bat",
         provider => windows,
         logoutput => true,
        }

The results of the manifest run are that everything executed successfully.  However, if I run "cleanmgr /d c: /sageset:100" after that, none of the boxes is checked.  Running the batch file in a Windows CLI does work, and if I run "cleanmgr /d c: /sageset:100" after that, all of the boxes are checked.

Why am I not getting this to work?

Dimitri

Peter Kristolaitis

unread,
Jul 29, 2015, 4:21:38 PM7/29/15
to puppet...@googlegroups.com
If all your batch script does is tweak some registry settings, I would suggest ditching batch files entirely and use the puppetlabs-registry module instead.

That said, it is likely that your script is not being executed at all because you need to escape backslashes in paths, e.g.


          command => "cmd.exe /c c:\\temp\\sageset.bat",

Because backslashes are escape characters, the path is being flattened to "c:tempsageset.bat" which clearly doesn't exist;  escaping the backslashes will resolve that.

But using the registry module is much better, and you should do that to avoid much sadness.

- Peter
--
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 view this discussion on the web visit https://groups.google.com/d/msgid/puppet-users/30c76fb5-d8b6-4107-9300-6773fc15e36a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Dimitri Yioulos

unread,
Jul 29, 2015, 4:58:17 PM7/29/15
to Puppet Users, dyio...@gmail.com
Peter,

Thanks for the response.

I understand about Windows and quoting.  I've tried a number of quoting options, all with the same result.  When I applied your suggestion about escaping the backslashes, then ran the manifest, here's what I got:

Info: Caching catalog for 1pointmailer.netatlantic.com
Info: Applying configuration version '1438202725'
Notice: /Stage[main]/Win_autodiskclean/Exec[sageset.bat]/returns: The operation
completed successfully.
Notice: /Stage[main]/Win_autodiskclean/Exec[sageset.bat]/returns: The operation
completed successfully.
Notice: /Stage[main]/Win_autodiskclean/Exec[sageset.bat]/returns: The operation
completed successfully.
Notice: /Stage[main]/Win_autodiskclean/Exec[sageset.bat]/returns: The operation
completed successfully.
Notice: /Stage[main]/Win_autodiskclean/Exec[sageset.bat]/returns: The operation
completed successfully.
Notice: /Stage[main]/Win_autodiskclean/Exec[sageset.bat]/returns: The operation
completed successfully.
Notice: /Stage[main]/Win_autodiskclean/Exec[sageset.bat]/returns: The operation
completed successfully.
Notice: /Stage[main]/Win_autodiskclean/Exec[sageset.bat]/returns: The operation
completed successfully.
Notice: /Stage[main]/Win_autodiskclean/Exec[sageset.bat]/returns: The operation
completed successfully.
Notice: /Stage[main]/Win_autodiskclean/Exec[sageset.bat]/returns: The operation
completed successfully.
Notice: /Stage[main]/Win_autodiskclean/Exec[sageset.bat]/returns: The operation
completed successfully.
Notice: /Stage[main]/Win_autodiskclean/Exec[sageset.bat]/returns: The operation
completed successfully.
Notice: /Stage[main]/Win_autodiskclean/Exec[sageset.bat]/returns: The operation
completed successfully.
Notice: /Stage[main]/Win_autodiskclean/Exec[sageset.bat]/returns: The operation
completed successfully.
Notice: /Stage[main]/Win_autodiskclean/Exec[sageset.bat]/returns: The operation
completed successfully.
Notice: /Stage[main]/Win_autodiskclean/Exec[sageset.bat]/returns: The operation
completed successfully.
Notice: /Stage[main]/Win_autodiskclean/Exec[sageset.bat]/returns: The operation
completed successfully.
Notice: /Stage[main]/Win_autodiskclean/Exec[sageset.bat]/returns: The operation
completed successfully.
Notice: /Stage[main]/Win_autodiskclean/Exec[sageset.bat]/returns: The operation
completed successfully.
Notice: /Stage[main]/Win_autodiskclean/Exec[sageset.bat]/returns: The operation
completed successfully.
Notice: /Stage[main]/Win_autodiskclean/Exec[sageset.bat]/returns: The operation
completed successfully.
Notice: /Stage[main]/Win_autodiskclean/Exec[sageset.bat]/returns: The operation
completed successfully.
Notice: /Stage[main]/Win_autodiskclean/Exec[sageset.bat]/returns: The operation
completed successfully.
Notice: /Stage[main]/Win_autodiskclean/Exec[sageset.bat]/returns: The operation
completed successfully.
Notice: /Stage[main]/Win_autodiskclean/Exec[sageset.bat]/returns: The operation
completed successfully.
Notice: /Stage[main]/Win_autodiskclean/Exec[sageset.bat]/returns: The operation
completed successfully.
Notice: /Stage[main]/Win_autodiskclean/Exec[sageset.bat]/returns: The operation
completed successfully.
Notice: /Stage[main]/Win_autodiskclean/Exec[sageset.bat]/returns: The operation
completed successfully.
Notice: /Stage[main]/Win_autodiskclean/Exec[sageset.bat]/returns: The operation
completed successfully.
Notice: /Stage[main]/Win_autodiskclean/Exec[sageset.bat]/returns: The operation
completed successfully.
Notice: /Stage[main]/Win_autodiskclean/Exec[sageset.bat]/returns: executed succe
ssfully
Notice: Finished catalog run in 1.51 seconds

That's a success for each registry change in the batch file.  Nonetheless, "cleanmgr /d c: /sageset:100" still shows none of the boxes checked.  I've gotten the same results regardless of how I've used quotes (really misused quotes produce errors where flattening is very obvious).

I appreciate the suggestion about using the registry module, but it seems to me that being able to run simple, quickly created, batch files via Puppet should be easy to do.   It's as much a matter of getting batch files to run as it is making this particular Disk Cleanup solution working.

Any other ideas?

Dimitri

Rob Reynolds

unread,
Jul 29, 2015, 5:12:23 PM7/29/15
to puppet...@googlegroups.com, dyio...@gmail.com
Indeed it should be. Quick and dirty is one way to achieve what you want with batch files, but given that your scripts are easily converted to puppet manifests, you should definitely consider going that route. It already automatically handles Windows File System / Registry Redirection, which is likely what you are currently running into.

It's likely that you are using a 32 bit version of Puppet on a 64bit version of Windows. When you call for c:\windows\cmd.exe and Windows File System Redirector gets involved, Windows will automatically redirect you to a 32bit cmd.exe in C:\Windows\SysWOW64\cmd.exe, which has only a 32 bit view of the registry.

Let's take a look at one of your examples - REG ADD "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Active Setup Temp Folders" /v StateFlags0100 /d 2 /t REG_DWORD /f

Do me a favor and look at "HKLM\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Active Setup Temp Folders" and tell me if you see what you are looking for there.


Once you've determined that is the issue, we can go into how to fix it.
 

Any other ideas?

Dimitri

On Wednesday, July 29, 2015 at 3:48:50 PM UTC-4, Dimitri Yioulos wrote:
Hello, all.

I'm trying to execute the following batch file (sageset.bat) on Win2k8 servers:

@ECHO OFF
REM Enable components to cleanup
REG ADD "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Active Setup Temp Folders" /v StateFlags0100 /d 2 /t REG_DWORD /f
REG ADD "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\BranchCache" /v StateFlags0100 /d 2 /t REG_DWORD /f
REG ADD "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Downloaded Program Files" /v StateFlags0100 /d 2 /t REG_DWORD /f
REG ADD "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\GameNewsFiles" /v StateFlags0100 /d 2 /t REG_DWORD /f
~

It makes changes to registry settings so that the disk cleanup utility will do a more thorough job of cleanup.  It's the equivalent of running "cleanmgr /d c: /sageset:100", which opens a dialog box that presents a list of files to clean that one can check.

The following is in my manifest:

exec { 'sageset.bat':
         path => $::path,
         command => "cmd.exe /c c:\temp\sageset.bat",
         provider => windows,
         logoutput => true,
        }

The results of the manifest run are that everything executed successfully.  However, if I run "cleanmgr /d c: /sageset:100" after that, none of the boxes is checked.  Running the batch file in a Windows CLI does work, and if I run "cleanmgr /d c: /sageset:100" after that, all of the boxes are checked.

Why am I not getting this to work?

Dimitri

--
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.

For more options, visit https://groups.google.com/d/optout.



--
Rob Reynolds
Developer, Puppet Labs

PuppetConf 2015 is coming to Portland, Oregon! Join us October 5-9.
Register now to take advantage of the Early Bird discount —save $249!

Dimitri Yioulos

unread,
Jul 29, 2015, 5:28:34 PM7/29/15
to Puppet Users, dyio...@gmail.com
Bob,

I installed puppet-3.8.1-x64 on my 64-bit Win2k8 hosts.  As you asked, I looked in the registry location, and saw "StateFlags0100  REG_DWORD  0x00000002 (2)".  That's what I'd expect.  However, in ""HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Active Setup Temp Folders", where the batch file is supposed to make the change, I see ""StateFlags0100  REG_DWORD  0x00000000 (0)", meaning it isn't changed when I run the manifest.  Great catch!  How do I remediate?


Dimitri

On Wednesday, July 29, 2015 at 3:48:50 PM UTC-4, Dimitri Yioulos wrote:

Dimitri Yioulos

unread,
Jul 29, 2015, 5:34:55 PM7/29/15
to Puppet Users, dyio...@gmail.com
Oops, apologies.  I mean Rob.


On Wednesday, July 29, 2015 at 3:48:50 PM UTC-4, Dimitri Yioulos wrote:

Rob Reynolds

unread,
Jul 29, 2015, 7:39:03 PM7/29/15
to puppet...@googlegroups.com, dyio...@gmail.com
On Wed, Jul 29, 2015 at 4:28 PM, Dimitri Yioulos <dyio...@gmail.com> wrote:
Bob,

I installed puppet-3.8.1-x64 on my 64-bit Win2k8 hosts.  As you asked, I looked in the registry location, and saw "StateFlags0100  REG_DWORD  0x00000002 (2)".  That's what I'd expect.  However, in ""HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Active Setup Temp Folders", where the batch file is supposed to make the change, I see ""StateFlags0100  REG_DWORD  0x00000000 (0)", meaning it isn't changed when I run the manifest.  Great catch!  How do I remediate?

I did a write up on this recently - take a look at Compensating for Redirection[1] and changing the path appropriately.

 


Dimitri

On Wednesday, July 29, 2015 at 3:48:50 PM UTC-4, Dimitri Yioulos wrote:
Hello, all.

I'm trying to execute the following batch file (sageset.bat) on Win2k8 servers:

@ECHO OFF
REM Enable components to cleanup
REG ADD "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Active Setup Temp Folders" /v StateFlags0100 /d 2 /t REG_DWORD /f
REG ADD "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\BranchCache" /v StateFlags0100 /d 2 /t REG_DWORD /f
REG ADD "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Downloaded Program Files" /v StateFlags0100 /d 2 /t REG_DWORD /f
REG ADD "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\GameNewsFiles" /v StateFlags0100 /d 2 /t REG_DWORD /f
~

It makes changes to registry settings so that the disk cleanup utility will do a more thorough job of cleanup.  It's the equivalent of running "cleanmgr /d c: /sageset:100", which opens a dialog box that presents a list of files to clean that one can check.

The following is in my manifest:

exec { 'sageset.bat':
         path => $::path,
         command => "cmd.exe /c c:\temp\sageset.bat",
         provider => windows,
         logoutput => true,
        }

The results of the manifest run are that everything executed successfully.  However, if I run "cleanmgr /d c: /sageset:100" after that, none of the boxes is checked.  Running the batch file in a Windows CLI does work, and if I run "cleanmgr /d c: /sageset:100" after that, all of the boxes are checked.

Why am I not getting this to work?

Dimitri

--
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.

For more options, visit https://groups.google.com/d/optout.

Dimitri Yioulos

unread,
Jul 30, 2015, 12:59:53 PM7/30/15
to Puppet Users, dyio...@gmail.com
Rob,

Thanks so much for pointing out the write up!  Here's what's happening now:

I updated the manifest to the following:

exec { 'sageset.bat':
        path => "c:\\windows\\sysnative;c:\\windows\\system32;$::path",

        command => "cmd.exe /c c:\\temp\\sageset.bat",
        provider => windows,
        logoutput => true,
     }

If I run the Puppet agent interactively from the node, I get the results I expect (all Disk Cleanup boxes are checked when I run "cleanmgr /d c: /sageset:100").  However, if I let manifest run normally from the puppetmaster, the boxes remain unchecked.  Why is that, and what steps do I have to take so that the manifest will create successful results when run from the master?

Dimitri

On Wednesday, July 29, 2015 at 3:48:50 PM UTC-4, Dimitri Yioulos wrote:

Dimitri Yioulos

unread,
Aug 3, 2015, 9:47:40 AM8/3/15
to Puppet Users
Again, I'm close on this.  Using the correct path, as per Rob's write-up, did help.  If I run the manifest interactively from the node, it works.  But, running it on schedule from the puppetmaster doesn't.  Any additional help would be greatly appreciated.


On Wednesday, July 29, 2015 at 3:48:50 PM UTC-4, Dimitri Yioulos wrote:

Rob Reynolds

unread,
Aug 4, 2015, 6:07:46 PM8/4/15
to puppet...@googlegroups.com
On Mon, Aug 3, 2015 at 8:47 AM, Dimitri Yioulos <dyio...@gmail.com> wrote:
Again, I'm close on this.  Using the correct path, as per Rob's write-up, did help.  If I run the manifest interactively from the node, it works.  But, running it on schedule from the puppetmaster doesn't.  Any additional help would be greatly appreciated.

It might be time to explore shifting that quick script over to puppet manifests so that Puppet can better handle the settings.

puppet module install puppetlabs/windows ;)


Here is what your first one should look like:

registry_key { 'HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Active Setup Temp Folders':
  ensure => present,
}

registry_value { 'HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Active Setup Temp Folders\StateFlags0100':
  ensure => present,
  data   => 2,
  type => 'dword',
}


Plus it doesn't have the issue of always running if there is nothing to do. The exec you have will run every time and report on it.


 


On Wednesday, July 29, 2015 at 3:48:50 PM UTC-4, Dimitri Yioulos wrote:
Hello, all.

I'm trying to execute the following batch file (sageset.bat) on Win2k8 servers:

@ECHO OFF
REM Enable components to cleanup
REG ADD "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Active Setup Temp Folders" /v StateFlags0100 /d 2 /t REG_DWORD /f
REG ADD "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\BranchCache" /v StateFlags0100 /d 2 /t REG_DWORD /f
REG ADD "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\Downloaded Program Files" /v StateFlags0100 /d 2 /t REG_DWORD /f
REG ADD "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\VolumeCaches\GameNewsFiles" /v StateFlags0100 /d 2 /t REG_DWORD /f
~

It makes changes to registry settings so that the disk cleanup utility will do a more thorough job of cleanup.  It's the equivalent of running "cleanmgr /d c: /sageset:100", which opens a dialog box that presents a list of files to clean that one can check.

The following is in my manifest:

exec { 'sageset.bat':
         path => $::path,
         command => "cmd.exe /c c:\temp\sageset.bat",
         provider => windows,
         logoutput => true,
        }

The results of the manifest run are that everything executed successfully.  However, if I run "cleanmgr /d c: /sageset:100" after that, none of the boxes is checked.  Running the batch file in a Windows CLI does work, and if I run "cleanmgr /d c: /sageset:100" after that, all of the boxes are checked.

Why am I not getting this to work?

Dimitri

--
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.

For more options, visit https://groups.google.com/d/optout.

Dimitri Yioulos

unread,
Aug 7, 2015, 11:24:50 AM8/7/15
to Puppet Users
Rob,

Thanks a million for getting me started on how to add the registry values.  I'll give that a go.  Still, I'm disappointed that I can't get my batch file to run remotely.

Again, thanks.

Dimitri


On Wednesday, July 29, 2015 at 3:48:50 PM UTC-4, Dimitri Yioulos wrote:
Reply all
Reply to author
Forward
0 new messages