How to pass a attribute value to ps1 file?

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::astonished::astonished:

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

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

thats not working giving following error. help me please

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

its urgent if anyone can help

thats not working giving following error. help me please

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

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'

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

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

Just to check you put this file in the attributes 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?

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”

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

this is the error

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 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:

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

yes actually i am running on a local mode

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

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

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