user8949830
user8949830

Reputation:

How to run powershell script remotely using chef?

I have powershell script which is present on chef server to run on remote windows server, how can i run this powershell script from chef server on remote windows server.

Upvotes: 0

Views: 1388

Answers (3)

JackChance
JackChance

Reputation: 520

Here is my understanding of what you are trying to achieve. If I'm wrong then please correct me in a comment and I will update my answer.

  • You have a powershell script that you need to run on a specific server or set of servers.
  • It would be convenient to have a central management solution for running this script instead of logging into each server and running it manually.
  • Ergo you either need to run this script in many places when a condition isn't filled, such as a file is missing, or you need to run this script often, or you need this script to be run with a certain timing in regards to other processes you have going on.

Without knowing precisely what you're trying to achieve with your script the best solution I know of is to write a cookbook and do one of the following

  • If your script is complex place it in your cookbook/files folder (assuming the script will be identical on all computers it runs on) or in your cookbook/templates folder (if you will need to inject information into it at write time). You can then write the .ps file to the local computer during a Chef converge with the following code snippet. After you write it to disk you will also have to call it with one of the commands in the next bullet.

Monomorphic file:

cookbook_file '<destination>' do source '<filename.ps>' <other options> end

Options can be found at https://docs.chef.io/resource_cookbook_file.html

Polymorphic file:

template '<destination>' do
  source '<template.ps.erb>'
  variables {<hash of variables and values>}
  <other options>
end

Options can be found at https://docs.chef.io/resource_template.html

  • If your script is a simple one-liner you can instead use powershell_script, powershell_out! or execute. powershell_out! has all the same options and features as the shell_out! command and the added advantage that your converge will pause until it receives an exit status for the command, if that is desirable. The documentation on using it is a bit more spotty though so spend time experimenting with it and googling.

https://docs.chef.io/resource_powershell_script.html https://docs.chef.io/resource_execute.html

Which ever option you end up going with you will probably want to guard your resource with conditions on when it should not run, such as when a file already exists, a registry key is set or what ever else your script changes that you can use. If you truly want the script to execute every single converge then you can skip this step, but that is a code smell and I urge you to reconsider your plans.

https://docs.chef.io/resource_common.html#guards

It's important to note that this is not an exhaustive list of how to run a powershell script on your nodes, just a collection of common patterns I've seen.

Hope this helped.

Upvotes: 0

coderanger
coderanger

Reputation: 54249

Chef doesn't do anything like this. First, Chef Server can never remotely access servers directly, all it does is stores data. Second, Chef doesn't really do "run a thing in a place right now". We offer workstation tools like knife ssh and knife winrm as simplistic wrappers but they aren't made for anything complex. The Chef-y way to do this would be to make a recipe and run your script using the the powershell_script resource.

Upvotes: 1

Franck
Franck

Reputation: 2499

Does it mean chef is also running on Windows server ? If yes, why not to use psexec from Windows Ps tools ?

https://learn.microsoft.com/en-us/sysinternals/downloads/psexec

Upvotes: 0

Related Questions