Power shell and chef


#1

Can anyone point me to some example chef code using not_if/only_if on
windows/powershell blocks? I have some ideas but I’m not sure they’re very
good. I need to set up a service and make sure I don’t kick off a reboot
or anything crazy every time chef runs.

Learning powershell syntax at the same time I’m wrangling chef for windows
is not advised, but really, who am I kidding? When would I have bothered
otherwise?

Sascha


#2

Some samples with the “windows_batch” resources:

not_if { Registry.key_exists?('HKLM\SOFTWARE\Microsoft\ ') }
not_if { File.exists?(“C:\etc\<something.txt>”) }

I had problems with the Services bit , but with powershell you can get it
to work using a Here Doc to get over a quotes within quotes problem . Eg,
when you have to check for databases in sql server using “EXEC
sp_databases” , you might have to add this in a here doc and use that in
your only_if or not_if . What doesn’t work is not_if " sqlcmd -S Localhost
"Exec sp_databases" | grep " but using the here doc in here
would work well.

Also with powershell , in the not_if or only_if , you’ll have to use
"powershell.exe" – for eg , not_if "powershell.exe " but a
normal windows command line would work well.

Hope that helps.

On Wed, Feb 22, 2012 at 7:20 AM, Sascha Bates sascha.bates@gmail.comwrote:

Can anyone point me to some example chef code using not_if/only_if on
windows/powershell blocks? I have some ideas but I’m not sure they’re very
good. I need to set up a service and make sure I don’t kick off a reboot
or anything crazy every time chef runs.

Learning powershell syntax at the same time I’m wrangling chef for windows
is not advised, but really, who am I kidding? When would I have bothered
otherwise?

Sascha


#3

Thanks, this helps.

On Tue, Feb 21, 2012 at 9:58 PM, Madhurranjan Mohaan <
maadhuuranjan.m@gmail.com> wrote:

Some samples with the “windows_batch” resources:

not_if { Registry.key_exists?('HKLM\SOFTWARE\Microsoft\ ') }
not_if { File.exists?(“C:\etc\<something.txt>”) }

I had problems with the Services bit , but with powershell you can get it
to work using a Here Doc to get over a quotes within quotes problem . Eg,
when you have to check for databases in sql server using “EXEC
sp_databases” , you might have to add this in a here doc and use that in
your only_if or not_if . What doesn’t work is not_if " sqlcmd -S Localhost
"Exec sp_databases" | grep " but using the here doc in here
would work well.

Also with powershell , in the not_if or only_if , you’ll have to use
"powershell.exe" – for eg , not_if "powershell.exe " but a
normal windows command line would work well.

Hope that helps.

On Wed, Feb 22, 2012 at 7:20 AM, Sascha Bates sascha.bates@gmail.comwrote:

Can anyone point me to some example chef code using not_if/only_if on
windows/powershell blocks? I have some ideas but I’m not sure they’re very
good. I need to set up a service and make sure I don’t kick off a reboot
or anything crazy every time chef runs.

Learning powershell syntax at the same time I’m wrangling chef for
windows is not advised, but really, who am I kidding? When would I have
bothered otherwise?

Sascha


#4

I’ve used the win32 service rubygem like so:

gem install win32-service --no-rdoc --no-ri --verbose --platform x86-mswin32

powershell “Install Service: MyService” do
code <<-EOH
c:
cd /WindowsServices/MyService /
./Install.bat
sc.exe failure MyService reset= 0 actions= restart/60
EOH
not_if {Win32::Service.exists?(“MyService”)}
end

Which runs the powershell given if the service doesn’t exist.

Cheers,
Dave.

From: Sascha Bates [mailto:sascha.bates@gmail.com]
Sent: Thursday, 23 February 2012 3:39 a.m.
To: chef@lists.opscode.com
Subject: [chef] Re: Re: power shell and chef

Thanks, this helps.
On Tue, Feb 21, 2012 at 9:58 PM, Madhurranjan Mohaan <maadhuuranjan.m@gmail.commailto:maadhuuranjan.m@gmail.com> wrote:
Some samples with the “windows_batch” resources:

not_if { Registry.key_exists?('HKLM\SOFTWARE\Microsoft\ ') }
not_if { File.exists?(“C:\etc\<something.txt>”) }

I had problems with the Services bit , but with powershell you can get it to work using a Here Doc to get over a quotes within quotes problem . Eg, when you have to check for databases in sql server using “EXEC sp_databases” , you might have to add this in a here doc and use that in your only_if or not_if . What doesn’t work is not_if " sqlcmd -S Localhost “Exec sp_databases” | grep " but using the here doc in here would work well.

Also with powershell , in the not_if or only_if , you’ll have to use “powershell.exe” – for eg , not_if "powershell.exe " but a normal windows command line would work well.

Hope that helps.

On Wed, Feb 22, 2012 at 7:20 AM, Sascha Bates <sascha.bates@gmail.commailto:sascha.bates@gmail.com> wrote:
Can anyone point me to some example chef code using not_if/only_if on windows/powershell blocks? I have some ideas but I’m not sure they’re very good. I need to set up a service and make sure I don’t kick off a reboot or anything crazy every time chef runs.

Learning powershell syntax at the same time I’m wrangling chef for windows is not advised, but really, who am I kidding? When would I have bothered otherwise?

Sascha


#5

I wish that I had the gumption to figure out how to hack the core of chef, however I am a coward. So instead I use a windows_service lwrp…

Check it out. Make installing, configuring and controller windows services super easy. It too uses the win32_service gem.

On Feb 22, 2012, at 1:05 PM, David Leaver wrote:

I’ve used the win32 service rubygem like so:

gem install win32-service --no-rdoc --no-ri --verbose --platform x86-mswin32

powershell “Install Service: MyService” do
code <<-EOH
c:
cd /WindowsServices/MyService /
./Install.bat
sc.exe failure MyService reset= 0 actions= restart/60
EOH
not_if {Win32::Service.exists?(“MyService”)}
end

Which runs the powershell given if the service doesn’t exist.

Cheers,
Dave.

From: Sascha Bates [mailto:sascha.bates@gmail.com]
Sent: Thursday, 23 February 2012 3:39 a.m.
To: chef@lists.opscode.commailto:chef@lists.opscode.com
Subject: [chef] Re: Re: power shell and chef

Thanks, this helps.
On Tue, Feb 21, 2012 at 9:58 PM, Madhurranjan Mohaan <maadhuuranjan.m@gmail.commailto:maadhuuranjan.m@gmail.com> wrote:
Some samples with the “windows_batch” resources:

not_if { Registry.key_exists?('HKLM\SOFTWARE\Microsoft\ ') }
not_if { File.exists?(“C:\etc\<something.txt><smb://etc//<something.txt>>”) }

I had problems with the Services bit , but with powershell you can get it to work using a Here Doc to get over a quotes within quotes problem . Eg, when you have to check for databases in sql server using “EXEC sp_databases” , you might have to add this in a here doc and use that in your only_if or not_if . What doesn’t work is not_if " sqlcmd -S Localhost “Exec sp_databases” | grep " but using the here doc in here would work well.

Also with powershell , in the not_if or only_if , you’ll have to use “powershell.exe” – for eg , not_if "powershell.exe " but a normal windows command line would work well.

Hope that helps.

On Wed, Feb 22, 2012 at 7:20 AM, Sascha Bates <sascha.bates@gmail.commailto:sascha.bates@gmail.com> wrote:
Can anyone point me to some example chef code using not_if/only_if on windows/powershell blocks? I have some ideas but I’m not sure they’re very good. I need to set up a service and make sure I don’t kick off a reboot or anything crazy every time chef runs.

Learning powershell syntax at the same time I’m wrangling chef for windows is not advised, but really, who am I kidding? When would I have bothered otherwise?

Sascha