1

I am using powershell_script resource in chef to create a database in sqlserver 2012 here.

I have used database name as test1 hardcoded in script. Now I want to provide the database name from the attribute file.

How can I get the value referenced from attribute file to the script.

powershell_script "create database" do
  code <<-EOH
    [System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO') | out-null

    $serverName = "localhost"

    $server = new-object ('Microsoft.SqlServer.Management.Smo.Server') $serverName

    #Create a new database
    $db = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Database -argumentlist $server, "test1"
    $db.Create()

    #Reference the database and display the date when it was created. 
    $db = $server.Databases["Test_SMO_Database"]
    $db.CreateDate
  EOH
end

4 Answers 4

2

now modified script look like this

powershell_script "create database" do
  code <<-EOH
  [System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO') | out-null
  $serverName = "localhost"
  $server = new-object ('Microsoft.SqlServer.Management.Smo.Server') $serverName
  #Create a new database
  $db = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Database -argumentlist $server, "#{node['sqlserver']['dbname']}"
  $db.Create()
  EOH
end

Attributes/default.rb

default['sqlserver']['dbname'] = 'testing database'

now i can create database by using value in attribute file.

Thanks for you help IsabelHM

Sign up to request clarification or add additional context in comments.

Comments

1

Spent a little bit of time figuring out the syntax to define an array as a node attribute and then pass it successfully to powershell_script. Apparently defining it as string is what works.

Attribute:

default['cookbook-name']['attribute-name'] = "'value1', 'value2', 'value3'"

And recipe:

powershell_script 'my script' do
  code <<-EOH
  $array = #{node['cookbook-name']['attribute-name']}
  ...
  EOH
  action :run
  guard_interpreter :powershell_script
  not_if "..."
end

Comments

0

Try using attribute environment to pass variable (in this example: dbname) and then call it in the script as $dbname

There are two ways to setup the attributes.

Method 1

In attributes/default.rb, add this line

default['yourCookbookNameHere']['dbname'] = 'test1'

In recipes/default.rb

powershell_script "create database" do
  environment ({'dbname' => "#{node['yourCookbookNameHere']['dbname']}"})
  code <<-EOH
    $db = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Database -argumentlist $server, $dbname
  EOH
end

Method 2

In your recipe, set it as local variable like this

localDbName = 'test1'

powershell_script "create database" do
  environment ({'dbname' => localDbName})
  code <<-EOH
    $db = New-Object -TypeName Microsoft.SqlServer.Management.Smo.Database -argumentlist $server, $dbname
  EOH
end

8 Comments

modified the script as mentioned it is able to get the value passed in attribute file but still its failing with following error. Compiled Resource: ------------------ # Declared in c:/chef/cache/cookbooks/sqlserver/recipes/default.rb:1:in `from_file' powershell_script("create database") do action "run" retries 0 retry_delay 2 default_guard_interpreter :powershell_script command "create database" backup 5 environment {"dbname"=>"testing database"} returns 0
What was the error? Could you post the attribute in your default.rb as well?
default.rb has the following value , correct me if its wrong if node['platform_family'] == 'windows' default['cookbook']['dbname'] = 'testing database' end
@bbsys Do you want to get the param from the attribute default.rb file or a local variable in the recipe default.rb?
i want it from the attribute default,rb
|
0

The current answers here using the environment property of powershell_script are incorrect in how they reference variables within the code block. Variables specified in the environment property are available as Environment Variables, not script variables, and have to be referenced as an environment variable like below:

powershell_script 'Passed-In Variable Example' do
  environment({ myVar: 'myvalue' })
  code <<-EOH
    Write-Output $env:myVar
  EOH
end

Basically, any of the other answers using the environment property to pass values in to the code block are almost correct, just make sure you prepend the variable name with $env: within the Powershell code.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.