# How to pass a attribute value to ps1 file?

**URL:** <https://discourse.chef.io/t/how-to-pass-a-attribute-value-to-ps1-file/11444>\
**Category:** Chef Infra (archive)\
**Created:** [September 5, 2017, 7:40am UTC](https://discourse.chef.io/t/how-to-pass-a-attribute-value-to-ps1-file/11444 "2017-09-05T07:40:29Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ankur760](https://avatars.discourse-cdn.com/v4/letter/a/5fc32e/32.png) [@Ankur760](https://discourse.chef.io/u/Ankur760)\
**Post date:** [September 5, 2017, 7:40am UTC](https://discourse.chef.io/t/how-to-pass-a-attribute-value-to-ps1-file/11444/1 "2017-09-05T07:40:29Z")

</div>

i want to pass a variable $instance = #{node[‘t1’][‘message’]}.  
i want it to pass through code property of powershell\_script resource

can i do like this ???

powershell\_script ‘run remote script’ do  
code '. C:\t1\files\default\shell\_cmd.ps1 “$instance”'  
end  
please help soon

i just want to pass a attribute value to a command in ps1 script…  
need urgent help:astonished:😲😲

---

<div class="post-metadata">

**Author:** ![stuartpreston](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/stuartpreston/32/1771_2.png) [@stuartpreston](https://discourse.chef.io/u/stuartpreston)\
**Post date:** [September 5, 2017, 8:30am UTC](https://discourse.chef.io/t/how-to-pass-a-attribute-value-to-ps1-file/11444/2 "2017-09-05T08:30:43Z")

</div>

Hi @Ankur760 You can probably achieve this with the following:

```
powershell_script 'run remote script' do
  code ". C:\\t1\\files\\default\\shell_cmd.ps1 \"#{node['t1']['message']}\""
end

```

Note the backslashes to make the following character literal, and double quotes around the line to allow the interpolation of ruby code inside the string. I would also consider whether the contents of shell\_cmd.ps1 would be better served from within your Chef recipe, to make future changes easier/more readable, but you may have accounted for that elsewhere.

Check our the powershell\_script guide for more examples: [https://docs.chef.io/resource\_powershell\_script.html](https://docs.chef.io/resource_powershell_script.html)

It is also worth mentioning our Chef Community Slack #windows channel - you can sign yourself up at [http://community-slack.chef.io/](http://community-slack.chef.io/) where you may be able to get faster help in future.

---

<div class="post-metadata">

**Author:** ![Ankur760](https://avatars.discourse-cdn.com/v4/letter/a/5fc32e/32.png) [@Ankur760](https://discourse.chef.io/u/Ankur760)\
**Post date:** [September 5, 2017, 8:38am UTC](https://discourse.chef.io/t/how-to-pass-a-attribute-value-to-ps1-file/11444/3 "2017-09-05T08:38:13Z")

</div>

thats not working giving following error. help me please

FATAL: NoMethodError: undefined method `[]' for nil:NilClass FATAL: NoMethodError: undefined method`[]’ for nil:NilClass

---

<div class="post-metadata">

**Author:** ![Ankur760](https://avatars.discourse-cdn.com/v4/letter/a/5fc32e/32.png) [@Ankur760](https://discourse.chef.io/u/Ankur760)\
**Post date:** [September 5, 2017, 8:45am UTC](https://discourse.chef.io/t/how-to-pass-a-attribute-value-to-ps1-file/11444/4 "2017-09-05T08:45:20Z")

</div>

its urgent if anyone can help

---

<div class="post-metadata">

**Author:** ![Ankur760](https://avatars.discourse-cdn.com/v4/letter/a/5fc32e/32.png) [@Ankur760](https://discourse.chef.io/u/Ankur760)\
**Post date:** [September 5, 2017, 8:50am UTC](https://discourse.chef.io/t/how-to-pass-a-attribute-value-to-ps1-file/11444/5 "2017-09-05T08:50:30Z")

</div>

> [@Ankur760](#):
>
> thats not working giving following error. help me please
> 
> FATAL: NoMethodError: undefined method ' for nil:NilClass FATAL: NoMethodError: undefined method’ for nil:NilClass

thats not working giving following error. help me please

FATAL: NoMethodError: undefined method ' for nil:NilClass FATAL: NoMethodError: undefined method’ for nil:NilClass

---

<div class="post-metadata">

**Author:** ![stuartpreston](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/stuartpreston/32/1771_2.png) [@stuartpreston](https://discourse.chef.io/u/stuartpreston)\
**Post date:** [September 5, 2017, 8:54am UTC](https://discourse.chef.io/t/how-to-pass-a-attribute-value-to-ps1-file/11444/6 "2017-09-05T08:54:58Z")

</div>

Where is your attribute set? Looks like there is no [‘t1’] level in your attribute hierarchy. Have you set a default attribute in your cookbook? i.e. in `attributes\default.rb`

`default['t1']['message'] = 'localinstancename'`

---

<div class="post-metadata">

**Author:** ![Ankur760](https://avatars.discourse-cdn.com/v4/letter/a/5fc32e/32.png) [@Ankur760](https://discourse.chef.io/u/Ankur760)\
**Post date:** [September 5, 2017, 12:36pm UTC](https://discourse.chef.io/t/how-to-pass-a-attribute-value-to-ps1-file/11444/7 "2017-09-05T12:36:39Z")

</div>

yes right i have set a default attribute in my attribute\default.rb  
still its not working. kindly help

default[‘t1’][‘message’] = ‘server\msssql’

---

<div class="post-metadata">

**Author:** ![stuartpreston](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/stuartpreston/32/1771_2.png) [@stuartpreston](https://discourse.chef.io/u/stuartpreston)\
**Post date:** [September 5, 2017, 12:55pm UTC](https://discourse.chef.io/t/how-to-pass-a-attribute-value-to-ps1-file/11444/8 "2017-09-05T12:55:36Z")

</div>

> [@Ankur760](#):
>
> default attribute in my attribute\default.rb

Just to check you put this file in the attribute **s** folder in your cookbook? Can you post your recipe code, the command line you are using to trigger it and the error message seen again please?

---

<div class="post-metadata">

**Author:** ![Ankur760](https://avatars.discourse-cdn.com/v4/letter/a/5fc32e/32.png) [@Ankur760](https://discourse.chef.io/u/Ankur760)\
**Post date:** [September 5, 2017, 2:30pm UTC](https://discourse.chef.io/t/how-to-pass-a-attribute-value-to-ps1-file/11444/9 "2017-09-05T14:30:35Z")

</div>

yes i have put it in my attribute folder  
this is my recipe code

powershell\_script ‘script\_name’ do  
code". C:\t1\files\default\shell\_cmd.ps1 “#{node[‘t1’][‘message’]}”"  
EOH  
end

this the only code i am using in ps1 file

param([string]$instance)  
Invoke-sqlcmd -ServerInstance “$instance” -InputFile “C:\t1\files\default\create.sql”

---

<div class="post-metadata">

**Author:** ![Ankur760](https://avatars.discourse-cdn.com/v4/letter/a/5fc32e/32.png) [@Ankur760](https://discourse.chef.io/u/Ankur760)\
**Post date:** [September 5, 2017, 2:34pm UTC](https://discourse.chef.io/t/how-to-pass-a-attribute-value-to-ps1-file/11444/10 "2017-09-05T14:34:33Z")

</div>

NoMethodError: undefined method `[]' for nil:NilClass NoMethodError: undefined method`[]’ for nil:NilClass

this is the error

---

<div class="post-metadata">

**Author:** ![stuartpreston](https://sea2.discourse-cdn.com/flex016/user_avatar/discourse.chef.io/stuartpreston/32/1771_2.png) [@stuartpreston](https://discourse.chef.io/u/stuartpreston)\
**Post date:** [September 5, 2017, 3:01pm UTC](https://discourse.chef.io/t/how-to-pass-a-attribute-value-to-ps1-file/11444/11 "2017-09-05T15:01:53Z")

</div>

Ignoring the smart quotes in your post (and the EOH which would cause an error in your Chef run if executed). You don’t mention what command line you are using? Is it local mode Chef (i.e. `chef-client -z -r recipe[yourcookbook::yourrecipe]`) or is this a real Chef node pointing to a server configured to a specific environment?

It really looks like you need to spend some more time running through the tutorials at [http://learn.chef.io](http://learn.chef.io) to make sure you understand the fundamentals of Chef before trying to solve this.

I would start to troubleshoot this by figuring out why your node attribute does not appear. Why not try replacing your powershell\_script block with:

```auto
log node['t1']['message']

```

If you get the same error then you can turn your attention to why your default attribute is not registering properly. Please triple-check the content and location (within your cookbook, not the repo) of your attributes\default.rb file once more…

If we can’t get any further I’ll be happy to jump on a call or analyze a cookbook you’ve zipped and sent me at stuart(at)[chef.io](http://chef.io)

---

<div class="post-metadata">

**Author:** ![Ankur760](https://avatars.discourse-cdn.com/v4/letter/a/5fc32e/32.png) [@Ankur760](https://discourse.chef.io/u/Ankur760)\
**Post date:** [September 5, 2017, 3:04pm UTC](https://discourse.chef.io/t/how-to-pass-a-attribute-value-to-ps1-file/11444/12 "2017-09-05T15:04:04Z")

</div>

> [@stuartpreston](#):
>
> If you get the same error then you can turn your attention to why your default attribute is not registering properly. Please triple-check the content and location (within your cookbook, not the repo)

yes actually i am running on a local mode

---

<div class="post-metadata">

**Author:** ![Ankur760](https://avatars.discourse-cdn.com/v4/letter/a/5fc32e/32.png) [@Ankur760](https://discourse.chef.io/u/Ankur760)\
**Post date:** [September 5, 2017, 3:07pm UTC](https://discourse.chef.io/t/how-to-pass-a-attribute-value-to-ps1-file/11444/13 "2017-09-05T15:07:15Z")

</div>

can you simply tell me how to pass value to a .sql file or replace some values with attribute value can you please write the a resource block and changes that i have to make in sql file

---

<div class="post-metadata">

**Author:** ![Ankur760](https://avatars.discourse-cdn.com/v4/letter/a/5fc32e/32.png) [@Ankur760](https://discourse.chef.io/u/Ankur760)\
**Post date:** [September 5, 2017, 3:08pm UTC](https://discourse.chef.io/t/how-to-pass-a-attribute-value-to-ps1-file/11444/14 "2017-09-05T15:08:53Z")

</div>

actually by mistake That EOH is placed because i am using hit and trial.  
for somehow code to work

---

<div class="post-metadata">

**Author:** ![Ankur760](https://avatars.discourse-cdn.com/v4/letter/a/5fc32e/32.png) [@Ankur760](https://discourse.chef.io/u/Ankur760)\
**Post date:** [September 7, 2017, 10:53am UTC](https://discourse.chef.io/t/how-to-pass-a-attribute-value-to-ps1-file/11444/16 "2017-09-07T10:53:42Z")

</div>

thanks its working now…  
but if i want to pass multiple values…
