# Power shell and chef

**URL:** <https://discourse.chef.io/t/power-shell-and-chef/2482>\
**Category:** Chef Infra (archive)\
**Created:** [February 22, 2012, 1:50am UTC](https://discourse.chef.io/t/power-shell-and-chef/2482 "2012-02-22T01:50:14Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sascha\_Bates](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/sascha_bates/32/504_2.png) [@Sascha\_Bates](https://discourse.chef.io/u/Sascha_Bates)\
**Post date:** [February 22, 2012, 1:50am UTC](https://discourse.chef.io/t/power-shell-and-chef/2482/1 "2012-02-22T01:50:14Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Madhurranjan\_Mohaan](https://avatars.discourse-cdn.com/v4/letter/m/5f9b8f/32.png) [@Madhurranjan\_Mohaan](https://discourse.chef.io/u/Madhurranjan_Mohaan)\
**Post date:** [February 22, 2012, 3:58am UTC](https://discourse.chef.io/t/power-shell-and-chef/2482/2 "2012-02-22T03:58:49Z")

</div>

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.com](mailto: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

---

<div class="post-metadata">

**Author:** ![Sascha\_Bates](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/sascha_bates/32/504_2.png) [@Sascha\_Bates](https://discourse.chef.io/u/Sascha_Bates)\
**Post date:** [February 22, 2012, 2:39pm UTC](https://discourse.chef.io/t/power-shell-and-chef/2482/3 "2012-02-22T14:39:12Z")

</div>

Thanks, this helps.

On Tue, Feb 21, 2012 at 9:58 PM, Madhurranjan Mohaan \<  
[maadhuuranjan.m@gmail.com](mailto: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.com](mailto: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

---

<div class="post-metadata">

**Author:** ![David\_Leaver](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/david_leaver/32/705_2.png) [@David\_Leaver](https://discourse.chef.io/u/David_Leaver)\
**Post date:** [February 22, 2012, 9:05pm UTC](https://discourse.chef.io/t/power-shell-and-chef/2482/4 "2012-02-22T21:05:42Z")

</div>

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](mailto:sascha.bates@gmail.com)]  
Sent: Thursday, 23 February 2012 3:39 a.m.  
To: [chef@lists.opscode.com](mailto: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.com](mailto:maadhuuranjan.m@gmail.com)[mailto:maadhuuranjan.m@gmail.com](mailto: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.com](mailto:sascha.bates@gmail.com)[mailto:sascha.bates@gmail.com](mailto: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

---

<div class="post-metadata">

**Author:** ![Paul\_Morton\_BIA](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/paul_morton_bia/32/480_2.png) [@Paul\_Morton\_BIA](https://discourse.chef.io/u/Paul_Morton_BIA)\
**Post date:** [February 22, 2012, 9:13pm UTC](https://discourse.chef.io/t/power-shell-and-chef/2482/5 "2012-02-22T21:13:05Z")

</div>

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.

> **[pmorton/windows\_service](https://github.com/pmorton/windows_service)**
>
> Windows Service Control Cookbook. Contribute to pmorton/windows\_service development by creating an account on GitHub.

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](mailto:sascha.bates@gmail.com)]  
Sent: Thursday, 23 February 2012 3:39 a.m.  
To: [chef@lists.opscode.com](mailto:chef@lists.opscode.com)[mailto:chef@lists.opscode.com](mailto: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.com](mailto:maadhuuranjan.m@gmail.com)[mailto:maadhuuranjan.m@gmail.com](mailto: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.com](mailto:sascha.bates@gmail.com)[mailto:sascha.bates@gmail.com](mailto: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
