# NOT\_IF and ONLY\_IF validation problem

**URL:** <https://discourse.chef.io/t/not-if-and-only-if-validation-problem/5654>\
**Category:** Chef Infra (archive)\
**Created:** [August 20, 2014, 12:16pm UTC](https://discourse.chef.io/t/not-if-and-only-if-validation-problem/5654 "2014-08-20T12:16:29Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Carlos\_Camacho](https://avatars.discourse-cdn.com/v4/letter/c/d26b3c/32.png) [@Carlos\_Camacho](https://discourse.chef.io/u/Carlos_Camacho)\
**Post date:** [August 20, 2014, 12:16pm UTC](https://discourse.chef.io/t/not-if-and-only-if-validation-problem/5654/1 "2014-08-20T12:16:29Z")

</div>

Hello!

Im runing this simple recipe block to create a web app in IIS

powershell\_script “create\_site\_my\_site” do  
code "New-webapppool -name ‘My\_Web\_App’; New-Website -Name  
’My\_Web\_App’ -applicationpool ‘My\_Web\_App’ -Port ‘80’ -IPAddress \*  
-PhysicalPath ‘c:\webs\My\_Web\_App’ "  
action :run  
not\_if "get-website | where-object { $\_.name -eq ‘My\_Web\_App’ }"  
end

The problem here its that the NOT\_IF part its allways True

PS C:\Users\carlos\>  
PS C:\Users\carlos\> get-website | where-object { $_.name -eq ‘asdfasdfasdf’ }  
PS C:\Users\carlos\> echo lastexitcode 1 PS C:\Users\carlos\> get-website | where-object {_ .name -eq ‘My\_Web\_App’ }

Name ID State Physical Path Bindings

* * *

```
            --------

```

My\_Web\_App 6 Stopped c:\webs\My\_Web\_App http \*:80:

```
            https *:443:

```

PS C:\Users\carlos.camacho\> echo $lastexitcode  
1

Now, my question is about how to return True or False in the NOT\_IF  
depending on my code return value ??

Thanks.

---

<div class="post-metadata">

**Author:** ![jdunn](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/jdunn/32/1800_2.png) [@jdunn](https://discourse.chef.io/u/jdunn)\
**Post date:** [August 20, 2014, 2:40pm UTC](https://discourse.chef.io/t/not-if-and-only-if-validation-problem/5654/2 "2014-08-20T14:40:24Z")

</div>

On Wed, Aug 20, 2014 at 8:16 AM, Carlos Camacho  
[carlos.camacho@frontiersin.org](mailto:carlos.camacho@frontiersin.org) wrote:

> Hello!
> 
> Im runing this simple recipe block to create a web app in IIS
> 
> powershell\_script "create\_site\_my\_site" do  
> code "New-webapppool -name 'My\_Web\_App'; New-Website -Name  
> 'My\_Web\_App' -applicationpool 'My\_Web\_App' -Port '80' -IPAddress \*  
> -PhysicalPath 'c:\webs\My\_Web\_App' "  
> action :run  
> not\_if "get-website | where-object { $\_.name -eq 'My\_Web\_App' }"  
> end
> 
> The problem here its that the NOT\_IF part its allways True
> 
> PS C:\Users\carlos\>  
> PS C:\Users\carlos\> get-website | where-object { $_.name -eq 'asdfasdfasdf' }  
> PS C:\Users\carlos\> echo $lastexitcode  
> 1  
> PS C:\Users\carlos\> get-website | where-object { $_.name -eq 'My\_Web\_App' }
> 
> Name ID State Physical Path Bindings
> 
> * * *
> 
> ```
> --------
> 
> ```
> 
> My\_Web\_App 6 Stopped c:\webs\My\_Web\_App http \*:80:
> 
> ```
> https *:443:
> 
> ```
> 
> PS C:\Users\carlos.camacho\> echo $lastexitcode  
> 1
> 
> Now, my question is about how to return True or False in the NOT\_IF  
> depending on my code return value ??

Use the convert\_boolean\_return attribute in the powershell\_script  
resource to do the right thing. See the documentation for information  
about what that does & why.

> **[powershell\_script Resource](https://docs.chef.io/resources/powershell_script/)**
>
> Use the powershell\_script resource to execute a script using the Windows PowerShell interpreter, much like how the script and script-based resources bash, csh, perl, python, and ruby are used. The powershell\_script resource is specific to the...

I imagine that this will become the default when :guard\_interpreter  
becomes :powershell by default for powershell\_script.

- Julian

--  
[Julian C. Dunn [jdunn@aquezada.com](mailto:jdunn@aquezada.com) \* Sorry, I'm ]  
[WWW: [Julian Dunn's Blog - Commentary on media, technology, and everything in between.](http://www.aquezada.com/staff/julian) \* only Web 1.0 ]  
[gopher://sdf.org/1/users/keymaker/ \* compliant!]  
[PGP: 91B3 7A9D 683C 7C16 715F 442C 6065 D533 FDC2 05B9]

---

<div class="post-metadata">

**Author:** ![Seth\_Vargo2](https://avatars.discourse-cdn.com/v4/letter/s/ccd318/32.png) [@Seth\_Vargo2](https://discourse.chef.io/u/Seth_Vargo2)\
**Post date:** [August 20, 2014, 2:55pm UTC](https://discourse.chef.io/t/not-if-and-only-if-validation-problem/5654/3 "2014-08-20T14:55:09Z")

</div>

Hi Carlos,

You also posted this question on StackOverflow: [ruby - Chef NOT\_IF and ONLY\_IF validation issue in Windows recipes - Stack Overflow](http://stackoverflow.com/questions/25406353/chef-not-if-and-only-if-validation-issue-in-windows-recipes).

I understand that you are trying to get the fastest answer possible, but posting the same question in multiple channels, you may discourage the chances of getting an answer. It gives a wry impression. Here's what I would recommend:

- If you have a question and you need an answer NOW, IRC is the best bet. You will get real-time communication with members of Chef and the Chef community.
- If you have a question that is about process, best practice, or opinion, this mailing list is probably your best bet. If you need to show code on the mailing list, using a service like PasteBin or GitHub Gist can make the email more readable.
- If you have a specific code example (i.e. why is this failing), StackOverflow/ServerFault are usually the best.

I realize there are definitely areas for overlap, so please use your best judgement.

Thanks,  
Seth

> On Aug 20, 2014, at 10:40 AM, Julian C. Dunn [jdunn@aquezada.com](mailto:jdunn@aquezada.com) wrote:
> 
> On Wed, Aug 20, 2014 at 8:16 AM, Carlos Camacho  
> \<[carlos.camacho@frontiersin.org](mailto:carlos.camacho@frontiersin.org) [mailto:carlos.camacho@frontiersin.org](mailto:carlos.camacho@frontiersin.org)\> wrote:
> 
> > Hello!
> > 
> > Im runing this simple recipe block to create a web app in IIS
> > 
> > powershell\_script "create\_site\_my\_site" do  
> > code "New-webapppool -name 'My\_Web\_App'; New-Website -Name  
> > 'My\_Web\_App' -applicationpool 'My\_Web\_App' -Port '80' -IPAddress \*  
> > -PhysicalPath 'c:\webs\My\_Web\_App' "  
> > action :run  
> > not\_if "get-website | where-object { $\_.name -eq 'My\_Web\_App' }"  
> > end
> > 
> > The problem here its that the NOT\_IF part its allways True
> > 
> > PS C:\Users\carlos\>  
> > PS C:\Users\carlos\> get-website | where-object { $_.name -eq 'asdfasdfasdf' }  
> > PS C:\Users\carlos\> echo $lastexitcode  
> > 1  
> > PS C:\Users\carlos\> get-website | where-object { $_.name -eq 'My\_Web\_App' }
> > 
> > Name ID State Physical Path Bindings
> > 
> > * * *
> > 
> > ```
> > --------
> > 
> > ```
> > 
> > My\_Web\_App 6 Stopped c:\webs\My\_Web\_App http \*:80:
> > 
> > ```
> > https *:443:
> > 
> > ```
> > 
> > PS C:\Users\carlos.camacho\> echo $lastexitcode  
> > 1
> > 
> > Now, my question is about how to return True or False in the NOT\_IF  
> > depending on my code return value ??
> 
> Use the convert\_boolean\_return attribute in the powershell\_script  
> resource to do the right thing. See the documentation for information  
> about what that does & why.
> 
> [powershell\_script Resource](http://docs.getchef.com/resource_powershell_script.html) [http://docs.getchef.com/resource\_powershell\_script.html](http://docs.getchef.com/resource_powershell_script.html)
> 
> I imagine that this will become the default when :guard\_interpreter  
> becomes :powershell by default for powershell\_script.
> 
> - Julian
> 
> --  
> [Julian C. Dunn \<[jdunn@aquezada.com](mailto:jdunn@aquezada.com) [mailto:jdunn@aquezada.com](mailto:jdunn@aquezada.com)\> \* Sorry, I'm ]  
> [WWW: [Julian Dunn's Blog - Commentary on media, technology, and everything in between.](http://www.aquezada.com/staff/julian) [http://www.aquezada.com/staff/julian](http://www.aquezada.com/staff/julian) \* only Web 1.0 ]  
> [gopher://sdf.org/1/users/keymaker/ gopher://sdf.org/1/users/keymaker/ \* compliant!]  
> [PGP: 91B3 7A9D 683C 7C16 715F 442C 6065 D533 FDC2 05B9]

---

<div class="post-metadata">

**Author:** ![Adam\_Edwards1](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/adam_edwards1/32/27_2.png) [@Adam\_Edwards1](https://discourse.chef.io/u/Adam_Edwards1)\
**Post date:** [August 20, 2014, 4:09pm UTC](https://discourse.chef.io/t/not-if-and-only-if-validation-problem/5654/4 "2014-08-20T16:09:17Z")

</div>

Carlos, lastexitcode does not give you the exit status of the last powershell cmdlet, it gives you the last exit status of a Windows process that you executed. Since get-website is a cmdlet, lastexitcode tells you nothing about its success or failure. For cmdlet status, use ?, which is  
$true if the last cmdlet succeeded, $false otherwise.

Powershell\_script actually has a feature to make your scenario easy – just  
provide a boolean expression to your guard, something like (get-website |  
where-object { $\_.name -eq ‘My\_Web\_App’ }).length \> 0, which will return  
$true if there’s a web site satisfying the query (assuming get-website  
returns a collection with a length property) and $false otherwise.

You can read more about this capability in this documentation from  
[http://docs.getchef.com/resource\_powershell\_script.html](http://docs.getchef.com/resource_powershell_script.html) – see the excerpt  
below:

convert\_boolean\_return

Use to return 0 if the last line of a command is evaluated to be true or to  
return 1 if the last line is evaluated to be false. Default value: false.

When the guard\_intrepreter common attribute is set to :powershell\_script, a  
string command will be evaluated as if this value were set to true. This is  
because the behavior of this attribute is similar to the value of the "$?"  
expression common in UNIX interpreters. For example, this:

powershell\_script ‘make\_safe\_backup’ do

guard\_interpreter :powershell\_script

code ‘cp ~/data/nodes.json ~/data/nodes.bak’

not\_if ‘test-path ~/data/nodes.bak’

end

is similar to:

bash ‘make\_safe\_backup’ do

code ‘cp ~/data/nodes.json ~/data/nodes.bak’

not\_if ‘test -e ~/data/nodes.bak’

end

_From:_ Seth Vargo [[mailto:sethvargo@getchef.com](mailto:sethvargo@getchef.com)]  
_Sent:_ Wednesday, August 20, 2014 7:55 AM  
_To:_ [chef@lists.opscode.com](mailto:chef@lists.opscode.com)  
_Subject:_ [chef] Re: Re: NOT\_IF and ONLY\_IF validation problem.

Hi Carlos,

You also posted this question on StackOverflow:

> <https://stackoverflow.com/questions/25406353/chef-not-if-and-only-if-validation-issue-in-windows-recipes>

  
.

I understand that you are trying to get the fastest answer possible, but  
posting the same question in multiple channels, you may discourage the  
chances of getting an answer. It gives a wry impression. Here’s what I  
would recommend:

- If you have a question and you need an answer NOW, IRC is the best bet.  
You will get real-time communication with members of Chef and the Chef  
community.

- If you have a question that is about process, best practice, or opinion,  
this mailing list is probably your best bet. If you need to show code on  
the mailing list, using a service like PasteBin or GitHub Gist can make the  
email more readable.

- If you have a specific code example (i.e. why is this failing),  
StackOverflow/ServerFault are usually the best.

I realize there are definitely areas for overlap, so please use your best  
judgement.

Thanks,

Seth

On Aug 20, 2014, at 10:40 AM, Julian C. Dunn [jdunn@aquezada.com](mailto:jdunn@aquezada.com) wrote:

On Wed, Aug 20, 2014 at 8:16 AM, Carlos Camacho  
[carlos.camacho@frontiersin.org](mailto:carlos.camacho@frontiersin.org) wrote:

Hello!

Im runing this simple recipe block to create a web app in IIS

powershell\_script “create\_site\_my\_site” do  
code "New-webapppool -name ‘My\_Web\_App’; New-Website -Name  
’My\_Web\_App’ -applicationpool ‘My\_Web\_App’ -Port ‘80’ -IPAddress \*  
-PhysicalPath ‘c:\webs\My\_Web\_App’ "  
action :run  
not\_if "get-website | where-object { $\_.name -eq ‘My\_Web\_App’ }"  
end

The problem here its that the NOT\_IF part its allways True

PS C:\Users\carlos\>  
PS C:\Users\carlos\> get-website | where-object { $_.name -eq ‘asdfasdfasdf’  
}  
PS C:\Users\carlos\> echo lastexitcode 1 PS C:\Users\carlos\> get-website | where-object {_ .name -eq ‘My\_Web\_App’ }

Name ID State Physical Path  
Bindings

* * *

```
           --------

```

My\_Web\_App 6 Stopped c:\webs\My\_Web\_App http \*:80:

```
           https *:443:

```

PS C:\Users\carlos.camacho\> echo $lastexitcode  
1

Now, my question is about how to return True or False in the NOT\_IF  
depending on my code return value ??

Use the convert\_boolean\_return attribute in the powershell\_script  
resource to do the right thing. See the documentation for information  
about what that does & why.

[http://docs.getchef.com/resource\_powershell\_script.html](http://docs.getchef.com/resource_powershell_script.html)

I imagine that this will become the default when :guard\_interpreter  
becomes :powershell by default for powershell\_script.

- Julian

–  
[Julian C. Dunn [jdunn@aquezada.com](mailto:jdunn@aquezada.com) \* Sorry, I’m ]  
[WWW: [http://www.aquezada.com/staff/julian](http://www.aquezada.com/staff/julian) \* only Web 1.0 ]  
[gopher://sdf.org/1/users/keymaker/ \* compliant!]  
[PGP: 91B3 7A9D 683C 7C16 715F 442C 6065 D533 FDC2 05B9]

---

<div class="post-metadata">

**Author:** ![Jeremy\_Mauro](https://avatars.discourse-cdn.com/v4/letter/j/a6a055/32.png) [@Jeremy\_Mauro](https://discourse.chef.io/u/Jeremy_Mauro)\
**Post date:** [August 20, 2014, 5:07pm UTC](https://discourse.chef.io/t/not-if-and-only-if-validation-problem/5654/5 "2014-08-20T17:07:56Z")

</div>

Hi everyone,

In the documentation it is not clearly in which version of chef the attribute ‘convert\_boolean\_return’ and ‘interpreter’ has been introduced, because we are using chef-11-10-4 and it doesn’t seem to be implemented in this version.  
Do you think it could be possible to have a better highlight of the version where the attributes were introduced, because it is not clear see where to search such information except from reading directly the chef-client code.

Thx

From: Adam Edwards [[mailto:adamed@getchef.com](mailto:adamed@getchef.com)]  
Sent: mercredi 20 août 2014 18:09  
To: [chef@lists.opscode.com](mailto:chef@lists.opscode.com)  
Subject: [chef] RE: Re: Re: NOT\_IF and ONLY\_IF validation problem.

Carlos, lastexitcode does not give you the exit status of the last powershell cmdlet, it gives you the last exit status of a Windows process that you executed. Since get-website is a cmdlet, lastexitcode tells you nothing about its success or failure. For cmdlet status, use ?, which is $true if the last cmdlet succeeded, $false otherwise.

Powershell\_script actually has a feature to make your scenario easy – just provide a boolean expression to your guard, something like (get-website | where-object { $\_.name -eq ‘My\_Web\_App’ }).length \> 0, which will return $true if there’s a web site satisfying the query (assuming get-website returns a collection with a length property) and $false otherwise.

You can read more about this capability in this documentation from [http://docs.getchef.com/resource\_powershell\_script.html](http://docs.getchef.com/resource_powershell_script.html) – see the excerpt below:

convert\_boolean\_return  
Use to return 0 if the last line of a command is evaluated to be true or to return 1 if the last line is evaluated to be false. Default value: false.

When the guard\_intrepreter common attribute is set to :powershell\_script, a string command will be evaluated as if this value were set to true. This is because the behavior of this attribute is similar to the value of the “$?” expression common in UNIX interpreters. For example, this:

powershell\_script ‘make\_safe\_backup’ do  
guard\_interpreter :powershell\_script  
code 'cp ~/data/nodes.json ~/data/nodes.bak’  
not\_if 'test-path ~/data/nodes.bak’  
end  
is similar to:

bash ‘make\_safe\_backup’ do  
code 'cp ~/data/nodes.json ~/data/nodes.bak’  
not\_if 'test -e ~/data/nodes.bak’  
end

From: Seth Vargo [[mailto:sethvargo@getchef.com](mailto:sethvargo@getchef.com)[mailto:sethvargo@getchef.com](mailto:sethvargo@getchef.com)]  
Sent: Wednesday, August 20, 2014 7:55 AM  
To: [chef@lists.opscode.com](mailto:chef@lists.opscode.com)[mailto:chef@lists.opscode.com](mailto:chef@lists.opscode.com)  
Subject: [chef] Re: Re: NOT\_IF and ONLY\_IF validation problem.

Hi Carlos,

You also posted this question on StackOverflow: [http://stackoverflow.com/questions/25406353/chef-not-if-and-only-if-validation-issue-in-windows-recipes](http://stackoverflow.com/questions/25406353/chef-not-if-and-only-if-validation-issue-in-windows-recipes).

I understand that you are trying to get the fastest answer possible, but posting the same question in multiple channels, you may discourage the chances of getting an answer. It gives a wry impression. Here’s what I would recommend:

- If you have a question and you need an answer NOW, IRC is the best bet. You will get real-time communication with members of Chef and the Chef community.
- If you have a question that is about process, best practice, or opinion, this mailing list is probably your best bet. If you need to show code on the mailing list, using a service like PasteBin or GitHub Gist can make the email more readable.
- If you have a specific code example (i.e. why is this failing), StackOverflow/ServerFault are usually the best.

I realize there are definitely areas for overlap, so please use your best judgement.

Thanks,  
Seth

On Aug 20, 2014, at 10:40 AM, Julian C. Dunn \<[jdunn@aquezada.com](mailto:jdunn@aquezada.com)[mailto:jdunn@aquezada.com](mailto:jdunn@aquezada.com)\> wrote:

On Wed, Aug 20, 2014 at 8:16 AM, Carlos Camacho  
\<[carlos.camacho@frontiersin.org](mailto:carlos.camacho@frontiersin.org)[mailto:carlos.camacho@frontiersin.org](mailto:carlos.camacho@frontiersin.org)\> wrote:  
Hello!

Im runing this simple recipe block to create a web app in IIS

powershell\_script “create\_site\_my\_site” do  
code "New-webapppool -name ‘My\_Web\_App’; New-Website -Name  
’My\_Web\_App’ -applicationpool ‘My\_Web\_App’ -Port ‘80’ -IPAddress \*  
-PhysicalPath ‘c:\webs\My\_Web\_App’ "  
action :run  
not\_if "get-website | where-object { $\_.name -eq ‘My\_Web\_App’ }"  
end

The problem here its that the NOT\_IF part its allways True

PS C:\Users\carlos\>  
PS C:\Users\carlos\> get-website | where-object { $_.name -eq ‘asdfasdfasdf’ }  
PS C:\Users\carlos\> echo lastexitcode 1 PS C:\Users\carlos\> get-website | where-object {_ .name -eq ‘My\_Web\_App’ }

Name ID State Physical Path Bindings

* * *

```
           --------

```

My\_Web\_App 6 Stopped c:\webs\My\_Web\_App http \*:80:

```
           https *:443:

```

PS C:\Users\carlos.camacho\> echo $lastexitcode  
1

Now, my question is about how to return True or False in the NOT\_IF  
depending on my code return value ??

Use the convert\_boolean\_return attribute in the powershell\_script  
resource to do the right thing. See the documentation for information  
about what that does & why.

[http://docs.getchef.com/resource\_powershell\_script.html](http://docs.getchef.com/resource_powershell_script.html)

I imagine that this will become the default when :guard\_interpreter  
becomes :powershell by default for powershell\_script.

- Julian

–  
[Julian C. Dunn \<[jdunn@aquezada.com](mailto:jdunn@aquezada.com)[mailto:jdunn@aquezada.com](mailto:jdunn@aquezada.com)\> \* Sorry, I’m ]  
[WWW: [http://www.aquezada.com/staff/julian](http://www.aquezada.com/staff/julian) \* only Web 1.0 ]  
[gopher://sdf.org/1/users/keymaker/ \* compliant!]  
[PGP: 91B3 7A9D 683C 7C16 715F 442C 6065 D533 FDC2 05B9]

---

<div class="post-metadata">

**Author:** ![Carlos\_Camacho](https://avatars.discourse-cdn.com/v4/letter/c/d26b3c/32.png) [@Carlos\_Camacho](https://discourse.chef.io/u/Carlos_Camacho)\
**Post date:** [August 21, 2014, 9:06am UTC](https://discourse.chef.io/t/not-if-and-only-if-validation-problem/5654/6 "2014-08-21T09:06:48Z")

</div>

Adam!

Thanks a lot xD

[![](https://us1.discourse-cdn.com/flex016/uploads/chef/original/2X/6/6af8e342b6c01310d1ead5a605718233040a64a8.jpeg "#ChefConf 2014: Adam Edwards, "PowerChef - Enhanced PowerShell Integration for Chef Recipes"") ](https://www.youtube.com/watch?v=BNV2jZUjKlc)

On Wed, Aug 20, 2014 at 7:07 PM, Jeremy Mauro [j.mauro@criteo.com](mailto:j.mauro@criteo.com) wrote:

> Hi everyone,
> 
> In the documentation it is not clearly in which version of chef the  
> attribute ‘convert\_boolean\_return’ and ‘interpreter’ has been introduced,  
> because we are using chef-11-10-4 and it doesn’t seem to be implemented in  
> this version.
> 
> Do you think it could be possible to have a better highlight of the version  
> where the attributes were introduced, because it is not clear see where to  
> search such information except from reading directly the chef-client code.
> 
> Thx
> 
> From: Adam Edwards [[mailto:adamed@getchef.com](mailto:adamed@getchef.com)]  
> Sent: mercredi 20 août 2014 18:09  
> To: [chef@lists.opscode.com](mailto:chef@lists.opscode.com)  
> Subject: [chef] RE: Re: Re: NOT\_IF and ONLY\_IF validation problem.
> 
> Carlos, $lastexitcode does not give you the exit status of the last  
> powershell cmdlet, it gives you the last exit status of a Windows process  
> that you executed. Since get-website is a cmdlet, lastexitcode tells you  
> nothing about its success or failure. For cmdlet status, use $?, which is  
> $true if the last cmdlet succeeded, $false otherwise.
> 
> Powershell\_script actually has a feature to make your scenario easy – just  
> provide a boolean expression to your guard, something like (get-website |  
> where-object { $\_.name -eq 'My\_Web\_App' }).length \> 0, which will return  
> $true if there’s a web site satisfying the query (assuming get-website  
> returns a collection with a length property) and $false otherwise.
> 
> You can read more about this capability in this documentation from  
> [powershell\_script Resource](http://docs.getchef.com/resource_powershell_script.html) -- see the excerpt  
> below:
> 
> convert\_boolean\_return
> 
> Use to return 0 if the last line of a command is evaluated to be true or to  
> return 1 if the last line is evaluated to be false. Default value: false.
> 
> When the guard\_intrepreter common attribute is set to :powershell\_script, a  
> string command will be evaluated as if this value were set to true. This is  
> because the behavior of this attribute is similar to the value of the "$?"  
> expression common in UNIX interpreters. For example, this:
> 
> powershell\_script 'make\_safe\_backup' do
> 
> guard\_interpreter :powershell\_script
> 
> code 'cp ~/data/nodes.json ~/data/nodes.bak'
> 
> not\_if 'test-path ~/data/nodes.bak'
> 
> end
> 
> is similar to:
> 
> bash 'make\_safe\_backup' do
> 
> code 'cp ~/data/nodes.json ~/data/nodes.bak'
> 
> not\_if 'test -e ~/data/nodes.bak'
> 
> end
> 
> From: Seth Vargo [[mailto:sethvargo@getchef.com](mailto:sethvargo@getchef.com)]  
> Sent: Wednesday, August 20, 2014 7:55 AM  
> To: [chef@lists.opscode.com](mailto:chef@lists.opscode.com)  
> Subject: [chef] Re: Re: NOT\_IF and ONLY\_IF validation problem.
> 
> Hi Carlos,
> 
> You also posted this question on StackOverflow:  
> [ruby - Chef NOT\_IF and ONLY\_IF validation issue in Windows recipes - Stack Overflow](http://stackoverflow.com/questions/25406353/chef-not-if-and-only-if-validation-issue-in-windows-recipes).
> 
> I understand that you are trying to get the fastest answer possible, but  
> posting the same question in multiple channels, you may discourage the  
> chances of getting an answer. It gives a wry impression. Here's what I would  
> recommend:
> 
> - If you have a question and you need an answer NOW, IRC is the best bet.  
> You will get real-time communication with members of Chef and the Chef  
> community.
> 
> - If you have a question that is about process, best practice, or opinion,  
> this mailing list is probably your best bet. If you need to show code on the  
> mailing list, using a service like PasteBin or GitHub Gist can make the  
> email more readable.
> 
> - If you have a specific code example (i.e. why is this failing),  
> StackOverflow/ServerFault are usually the best.
> 
> I realize there are definitely areas for overlap, so please use your best  
> judgement.
> 
> Thanks,
> 
> Seth
> 
> On Aug 20, 2014, at 10:40 AM, Julian C. Dunn [jdunn@aquezada.com](mailto:jdunn@aquezada.com) wrote:
> 
> On Wed, Aug 20, 2014 at 8:16 AM, Carlos Camacho  
> [carlos.camacho@frontiersin.org](mailto:carlos.camacho@frontiersin.org) wrote:
> 
> Hello!
> 
> Im runing this simple recipe block to create a web app in IIS
> 
> powershell\_script "create\_site\_my\_site" do  
> code "New-webapppool -name 'My\_Web\_App'; New-Website -Name  
> 'My\_Web\_App' -applicationpool 'My\_Web\_App' -Port '80' -IPAddress \*  
> -PhysicalPath 'c:\webs\My\_Web\_App' "  
> action :run  
> not\_if "get-website | where-object { $\_.name -eq 'My\_Web\_App' }"  
> end
> 
> The problem here its that the NOT\_IF part its allways True
> 
> PS C:\Users\carlos\>  
> PS C:\Users\carlos\> get-website | where-object { $_.name -eq 'asdfasdfasdf'  
> }  
> PS C:\Users\carlos\> echo $lastexitcode  
> 1  
> PS C:\Users\carlos\> get-website | where-object { $_.name -eq 'My\_Web\_App' }
> 
> Name ID State Physical Path  
> Bindings
> 
> * * *
> 
> ```
> --------
> 
> ```
> 
> My\_Web\_App 6 Stopped c:\webs\My\_Web\_App http \*:80:
> 
> ```
> https *:443:
> 
> ```
> 
> PS C:\Users\carlos.camacho\> echo $lastexitcode  
> 1
> 
> Now, my question is about how to return True or False in the NOT\_IF  
> depending on my code return value ??
> 
> Use the convert\_boolean\_return attribute in the powershell\_script  
> resource to do the right thing. See the documentation for information  
> about what that does & why.
> 
> [powershell\_script Resource](http://docs.getchef.com/resource_powershell_script.html)
> 
> I imagine that this will become the default when :guard\_interpreter  
> becomes :powershell by default for powershell\_script.
> 
> - Julian
> 
> --  
> [Julian C. Dunn [jdunn@aquezada.com](mailto:jdunn@aquezada.com) \* Sorry, I'm ]  
> [WWW: [Julian Dunn's Blog - Commentary on media, technology, and everything in between.](http://www.aquezada.com/staff/julian) \* only Web 1.0 ]  
> [gopher://sdf.org/1/users/keymaker/ \* compliant!]  
> [PGP: 91B3 7A9D 683C 7C16 715F 442C 6065 D533 FDC2 05B9]

---

<div class="post-metadata">

**Author:** ![Carlos\_Camacho](https://avatars.discourse-cdn.com/v4/letter/c/d26b3c/32.png) [@Carlos\_Camacho](https://discourse.chef.io/u/Carlos_Camacho)\
**Post date:** [August 22, 2014, 9:42am UTC](https://discourse.chef.io/t/not-if-and-only-if-validation-problem/5654/7 "2014-08-22T09:42:56Z")

</div>

Hello,

After a lot of tests I'm coming back 🙂

This is my recipe code:

powershell\_script "create\_binding\_"+z[:app\_name] do  
code "New-WebBinding -Name '"+z[:app\_name]+"' -IPAddress '\*' -Port  
'"+z[:https\_port]+"' -Protocol 'https'"  
action :run  
not\_if "(get-webbinding -Name '"+z[:app\_name]+"' -Port  
'"+z[:https\_port]+"').length -gt 0"  
guard\_interpreter :powershell\_script  
action :run  
end

Now the execution fails and this is the chef-client ourput.

powershell\_script("create\_binding\_Alhambra") do  
action [:run]  
retries 0  
retry\_delay 2  
guard\_interpreter :powershell\_script  
command ""powershell.exe" -NoLogo -NonInteractive -NoProfile  
-ExecutionPolicy RemoteSigned -InputFormat None -File  
"C:/Users/CARLOS~1.CAM/AppData/Local/Temp/2/chef-script20140822-2516-1w2ge1f.ps1""  
backup 5  
returns 0  
code "New-WebBinding -Name 'Alhambra' -IPAddress '\*' -Port '4435'  
-Protocol 'https'"  
interpreter "powershell.exe"  
cookbook\_name "frontiers\_custom"  
recipe\_name "populator"  
not\_if "(get-webbinding -Name 'Alhambra' -Port '4435').length -gt 0"  
end

In this case if I execute the guard "(get-webbinding -Name 'Alhambra'  
-Port '4435').length -gt 0"  
It returs True in the PS shell (The binding its created) but it keeps  
executing the comand and it fails.

This is the error message

## [2014-08-22T08:48:18+00:00] INFO: Running queued delayed notifications before re-raising exception Running handlers: [2014-08-22T08:48:18+00:00] ERROR: Running exception handlers Running handlers complete [2014-08-22T08:48:18+00:00] ERROR: Exception handlers complete [2014-08-22T08:48:18+00:00] FATAL: Stacktrace dumped to c:/chef/cache/chef-stacktrace.out Chef Client failed. 0 resources updated in 12.819917 seconds [2014-08-22T08:48:18+00:00] FATAL: Mixlib::ShellOut::ShellCommandFailed: powershell\_script[create\_binding\_Alhambra] (frontiers\_custom::populator line 112) had an error: Mixlib::ShellOut::ShellCommandFailed: Expected process to exit with [0] , but received '1' ---- Begin output of "powershell.exe" -NoLogo -NonInteractive -NoProfile -ExecutionPolicy RemoteSigned -InputFormat None -File "C:/Users/CARLOS~1.CAM/AppData/Local/Temp/2/chef-script20140822-2516-1w2ge1f.ps1"

## STDOUT: STDERR: Filename: Error: Cannot add duplicate collection entry of type 'binding' with combined key attributes 'protocol, bindingInformation' respectively set to 'https, \*:4435:' ---- End output of "powershell.exe" -NoLogo -NonInteractive -NoProfile -ExecutionPolicy RemoteSigned -InputFormat None -File "C:/Users/CARLOS~1.CAM/AppData/Local/Temp/2/chef-script20140822-2516-1w2ge1f.ps1"

Ran "powershell.exe" -NoLogo -NonInteractive -NoProfile  
-ExecutionPolicy RemoteSigned -InputFormat None -File "C:/Users/

This is because the guard its not working properly (Chef client  
11.14.6 and Chef server 11.1.4)..  
I also tested it using convert\_boolean\_return true but the result remains.

Adam, can you help me with this?

Thanks.

On Wed, Aug 20, 2014 at 6:09 PM, Adam Edwards [adamed@getchef.com](mailto:adamed@getchef.com) wrote:

> Carlos, $lastexitcode does not give you the exit status of the last  
> powershell cmdlet, it gives you the last exit status of a Windows process  
> that you executed. Since get-website is a cmdlet, lastexitcode tells you  
> nothing about its success or failure. For cmdlet status, use $?, which is  
> $true if the last cmdlet succeeded, $false otherwise.
> 
> Powershell\_script actually has a feature to make your scenario easy – just  
> provide a boolean expression to your guard, something like (get-website |  
> where-object { $\_.name -eq 'My\_Web\_App' }).length \> 0, which will return  
> $true if there’s a web site satisfying the query (assuming get-website  
> returns a collection with a length property) and $false otherwise.
> 
> You can read more about this capability in this documentation from  
> [http://docs.getchef.com/resource\_powershell\_script.html](http://docs.getchef.com/resource_powershell_script.html) -- see the excerpt  
> below:
> 
> convert\_boolean\_return
> 
> Use to return 0 if the last line of a command is evaluated to be true or to  
> return 1 if the last line is evaluated to be false. Default value: false.
> 
> When the guard\_intrepreter common attribute is set to :powershell\_script, a  
> string command will be evaluated as if this value were set to true. This is  
> because the behavior of this attribute is similar to the value of the "$?"  
> expression common in UNIX interpreters. For example, this:
> 
> powershell\_script 'make\_safe\_backup' do
> 
> guard\_interpreter :powershell\_script
> 
> code 'cp ~/data/nodes.json ~/data/nodes.bak'
> 
> not\_if 'test-path ~/data/nodes.bak'
> 
> end
> 
> is similar to:
> 
> bash 'make\_safe\_backup' do
> 
> code 'cp ~/data/nodes.json ~/data/nodes.bak'
> 
> not\_if 'test -e ~/data/nodes.bak'
> 
> end
> 
> From: Seth Vargo [[mailto:sethvargo@getchef.com](mailto:sethvargo@getchef.com)]  
> Sent: Wednesday, August 20, 2014 7:55 AM  
> To: [chef@lists.opscode.com](mailto:chef@lists.opscode.com)  
> Subject: [chef] Re: Re: NOT\_IF and ONLY\_IF validation problem.
> 
> Hi Carlos,
> 
> You also posted this question on StackOverflow:  
> [ruby - Chef NOT\_IF and ONLY\_IF validation issue in Windows recipes - Stack Overflow](http://stackoverflow.com/questions/25406353/chef-not-if-and-only-if-validation-issue-in-windows-recipes).
> 
> I understand that you are trying to get the fastest answer possible, but  
> posting the same question in multiple channels, you may discourage the  
> chances of getting an answer. It gives a wry impression. Here's what I would  
> recommend:
> 
> - If you have a question and you need an answer NOW, IRC is the best bet.  
> You will get real-time communication with members of Chef and the Chef  
> community.
> 
> - If you have a question that is about process, best practice, or opinion,  
> this mailing list is probably your best bet. If you need to show code on the  
> mailing list, using a service like PasteBin or GitHub Gist can make the  
> email more readable.
> 
> - If you have a specific code example (i.e. why is this failing),  
> StackOverflow/ServerFault are usually the best.
> 
> I realize there are definitely areas for overlap, so please use your best  
> judgement.
> 
> Thanks,
> 
> Seth
> 
> On Aug 20, 2014, at 10:40 AM, Julian C. Dunn [jdunn@aquezada.com](mailto:jdunn@aquezada.com) wrote:
> 
> On Wed, Aug 20, 2014 at 8:16 AM, Carlos Camacho  
> [carlos.camacho@frontiersin.org](mailto:carlos.camacho@frontiersin.org) wrote:
> 
> Hello!
> 
> Im runing this simple recipe block to create a web app in IIS
> 
> powershell\_script "create\_site\_my\_site" do  
> code "New-webapppool -name 'My\_Web\_App'; New-Website -Name  
> 'My\_Web\_App' -applicationpool 'My\_Web\_App' -Port '80' -IPAddress \*  
> -PhysicalPath 'c:\webs\My\_Web\_App' "  
> action :run  
> not\_if "get-website | where-object { $\_.name -eq 'My\_Web\_App' }"  
> end
> 
> The problem here its that the NOT\_IF part its allways True
> 
> PS C:\Users\carlos\>  
> PS C:\Users\carlos\> get-website | where-object { $_.name -eq 'asdfasdfasdf'  
> }  
> PS C:\Users\carlos\> echo $lastexitcode  
> 1  
> PS C:\Users\carlos\> get-website | where-object { $_.name -eq 'My\_Web\_App' }
> 
> Name ID State Physical Path  
> Bindings
> 
> * * *
> 
> ```
> --------
> 
> ```
> 
> My\_Web\_App 6 Stopped c:\webs\My\_Web\_App http \*:80:
> 
> ```
> https *:443:
> 
> ```
> 
> PS C:\Users\carlos.camacho\> echo $lastexitcode  
> 1
> 
> Now, my question is about how to return True or False in the NOT\_IF  
> depending on my code return value ??
> 
> Use the convert\_boolean\_return attribute in the powershell\_script  
> resource to do the right thing. See the documentation for information  
> about what that does & why.
> 
> [http://docs.getchef.com/resource\_powershell\_script.html](http://docs.getchef.com/resource_powershell_script.html)
> 
> I imagine that this will become the default when :guard\_interpreter  
> becomes :powershell by default for powershell\_script.
> 
> - Julian
> 
> --  
> [Julian C. Dunn [jdunn@aquezada.com](mailto:jdunn@aquezada.com) \* Sorry, I'm ]  
> [WWW: [http://www.aquezada.com/staff/julian](http://www.aquezada.com/staff/julian) \* only Web 1.0 ]  
> [gopher://sdf.org/1/users/keymaker/ \* compliant!]  
> [PGP: 91B3 7A9D 683C 7C16 715F 442C 6065 D533 FDC2 05B9]
