David Young
David Young

Reputation: 79

How to redirect output of Windows batch scripts inside a Chef Coobkook into a file?

For example in this simple cookbook recipe:

batch "Clear_OS_Agent" do
    code <<-EOH

@echo on
dir C:\
@echo off

    EOH

    action :run

When I run chef-client.bat on a Windows node, I can get the result of dir C:\

But when I redirect in into a file chef-client.bat > C:\chef_log.txt , there are only the general chef-client.bat output over there, without the result of dir C:\.

Upvotes: 0

Views: 999

Answers (1)

Display Name is missing
Display Name is missing

Reputation: 6227

Do the redirect inside your recipe, not when you run chef-client like:

code <<-EOH

@echo on
dir C:\ > C:\chef_log.txt
@echo off

EOH

You can probably see the results of your run how you were doing it originally if you crank up the debug as well like:

chef-client.bat -l debug > C:\chef_log.txt

Edit:

If you have multiple commands you want redirected to the same log file you can do the following so you don't have to explicitly redirect each line:

(
 echo one
 echo two
 echo three
 dir C:\
) > C:\test.txt

Upvotes: 1

Related Questions