[chef] Re: Re: RE: RE: RE: RE: RE: Re: Powershell terminate chef-run


Chronological Thread 
  • From: "Steven Murawski" < >
  • To: "" < >
  • Subject: [chef] Re: Re: RE: RE: RE: RE: RE: Re: Powershell terminate chef-run
  • Date: Thu, 14 May 2015 12:38:43 -0500

I think you guys might be tackling the wrong problem (in trying to use output from a script).  In order to accomplish the desired task (rename and join domain, then reboot - but only that once), we should go back to Galen's suggestion.

Something like the below, where you've got a powershell script that will rename a computer and join the domain.  However, rather than embedding the checks in the powershell script and relying on output, we can put the check in a guard clause.  That will prevent the PowerShell script from running when the settings match our desired state.  That also allows us to use a notification to tell the reboot resource to restart the computer when changes have been made, but only when the powershell script runs. 

reboot 'Restart Computer' do
action :nothing
end
powershell_script 'Rename and Join Domain' do
code <<-EOH
...your rename and domain join logic here...
EOH
not_if <<-EOH
$ComputerSystem = gwmi win32_computersystem
($ComputerSystem.Name -like '#{node['some_attribute_that_has_the_new_name']}') -and
$ComputerSystem.partofdomain)
EOH
notifies :reboot_now, 'reboot[Restart Computer]', :immediately
end


Steven Murawski
Community Software Development Engineer @ Chef
Microsoft MVP - PowerShell
http://stevenmurawski.com

On 5/7/2015 9:38:37 AM, Kenneth Barry < > wrote:

There is another method for getting output from powershell in chef. I can't remember what its called, but, maybe a "mixin?" In any case, you don't actually "see" powershell running in a chef run. Until you find the special "logging chef foo" stuff, you could try doing something like "The command is $command" | out-file c:\cheflog.txt -append

On Thu, May 7, 2015 at 7:28 AM, Simon Hawkins < " target="_blank"> > wrote:

Hi Chris,

 

No what I mean is, I have this in my recipe:

 

I’ve highlighted key parts of the logic so it makes sense.

 

::Chef::Resource::PowershellScript.send(:include, Chef::Mixin::PowershellOut)

include_recipe 'windows::reboot_handler'

 

reboot "reboot_machine" do

  action :nothing

  reason "reason"

  # other params as appropriate

end

 

powershell_script "rename_hostname" do

  code <<-EOH

if ($computer_name -eq $new_name) {

 

Write-host "Not changing the name as it is already set"

 

if ((gwmi win32_computersystem).partofdomain -eq $true) {

        write-host "I am domain joined, aborting!"

     …

     }

     Else {

      Add-Computer -DomainName $domain -Credential $cred -force

      $rebootNeeded = 1

      return $rebootNeeded

     }

  }

  else {

        $sysInfo = Get-WmiObject -Class Win32_ComputerSystem

        $sysInfo.Rename($new_name)

        $rebootNeeded = 1

        return $rebootNeeded

  }

EOH

cmd = powershell_out(code)

puts "This value is #{cmd}"

if cmd.stdout == 1

    notifies :request_reboot, "reboot[reboot_machine]"

end

 

end

 

It’s the code block starting “cmd cmd = powershell_out(code)” which is supposed to get the return value back, so this if statement evaluates

if cmd.stdout == 1

    notifies :request_reboot, "reboot[reboot_machine]"

end

 

But it doesn’t seem to do anything, so I added: “puts "This value is #{cmd}" to see if cmd had any contents and on the chef run it doesn’t even output “this value is” (the text itself) to the console, let alone the variable.

 

Any idea why? Do I have to have an import statement to get ruby code to work in a recipe?

 

Cheers,

Simon

 

From: Fouts, Chris [mailto: " target="_blank"> ]
Sent: 07 May 2015 15:15
To: " target="_blank">
Subject: [chef] RE: RE: RE: RE: Re: Powershell terminate chef-run

 

For powershell

 

Write-Host “This value is #{cmd}”

 

Chris

 

 

Also, if I put:

 

puts "This value is #{cmd}"

 

It doesn’t write anything to the screen on the Chef run either?

 

Cheers,

Simon.

 

 

Hi,

 

I managed to fix it by doing:

 

::Chef::Resource::PowershellScript.send(:include, Chef::Mixin::PowershellOut)

 

However, the following block doesn’t seem to work:

 

EOH

cmd = powershell_out(code).stdout.chop

if cmd == 1

    notifies :request_reboot, "reboot[reboot_machine]"

end

 

I know the powershell script is returning 1, but the reboot doesn’t seem to happen:

 

[2015-05-07T10:30:54+01:00] INFO: Enabling chef_handler[WindowsRebootHandler] as a report handler

[2015-05-07T10:30:54+01:00] INFO: Processing reboot[reboot_machine] action nothing (windows::rename_hostname line 4)

[2015-05-07T10:30:54+01:00] INFO: Processing powershell_script[rename_hostname] action run (windows::rename_hostname line 10)

[2015-05-07T10:31:52+01:00] INFO: powershell_script[rename_hostname] ran successfully

[2015-05-07T10:31:52+01:00] INFO: Chef Run complete in 61.98407 seconds

[2015-05-07T10:31:52+01:00] INFO: Running report handlers

[2015-05-07T10:31:52+01:00] WARN: chef_handler[WindowsRebootHandler] no reboot requested or pending

[2015-05-07T10:31:52+01:00] INFO: Report handlers complete

 

The powershell variable is called $rebootNeeded and that returns 1 in the code logic.

 

Maybe my if statement is wrong?

 

Cheers,

Simon.

 

 

Hi All,

 

I get the following error:

 

79:          return $rebootNeeded

80:

81:       #Restart-Computer -force

82:

83:    }

84:

85:   EOH

86>>  cmd = powershell_out(code)

87:   if cmd == 1

88:      notifies :request_reboot, "reboot[reboot_machine]"

89:   end

90:

91:  end

92:

93:

94:

 

ERROR: Running exception handlers

ERROR: Exception handlers complete

FATAL: Stacktrace dumped to c:/chef/cache/chef-stacktrace.out

FATAL: NoMethodError: undefined method `powershell_out' for Chef::Resource::PowershellScript

 

I have:  ::Chef::Recipe.send(:include, Chef::Mixin::PowershellOut) in the recipe.

 

Any idea how I load it?

 

Cheers,

Simon.

 

From: Galen Emery " target="_blank">[mailto:
Sent: 06 May 2015 18:53
To: " target="_blank">
Subject: [chef] Re: Powershell terminate chef-run

 

I would use the chef reboot resource.

 

In short, put a guard on the powershell_script resource similar to your if statement[0]

And then I'd use a notifies[1] for the reboot resource[2]


[0]https://docs.chef.io/resource_common.html#guards

 

This will end the chef run cleanly, rather than throwing an error at the end of the powershell.

 

 

 

On Wed, May 6, 2015 at 1:37 AM, Simon Hawkins < " target="_blank"> > wrote:

Hi All,

 

I have a “Restart-computer -force” command as part of a powershell script inside a chef recipe, how do I terminate the chef-run at this point?

 

I have something like this:

 

  if ($computer_name -eq $new_name) {

 

     Write-host "Not changing the name as it is already set"

 

                … code here….

 

    

if ((gwmi win32_computersystem).partofdomain -eq $true) {

        write-host "I am domain joined, aborting!"

 

     }

 

     Else {

 

      Add-Computer -DomainName $domain -Credential $cred -force

 

      Restart-Computer -force --- how do I terminate the chef-run here?

 

     }

 

  }

 

  else {

 

      code here…..              

       

        Restart-Computer –force -- how do I terminate the chef-run here?

 

  }

 

EOH

 

Cheers,

Simon


Disclaimer

This message is intended only for the use of the person(s) ("Intended Recipient") to whom it is addressed. It may contain information which is privileged and confidential. Accordingly any dissemination, distribution, copying or other use of this message or any of its content by any person other than the Intended Recipient may constitute a breach of civil or criminal law and is strictly prohibited. If you are not the Intended Recipient, please contact the sender as soon as possible.

Totaljobs Group Limited Registered Office: Bluefin Building, 110 Southwark Street, London, SE1 0TA, UK Registered in England and Wales under company no. 4269861




 




--

Kenneth Barry 

TuneIn | Build and Release Engineer
M: 409-673-0544



Archive powered by MHonArc 2.6.16.

§