I am using powershell_script
resource in chef to create a database in sqlserver 2012 here.
我在chef中使用powershell_script资源在sqlserver 2012中创建数据库。
I have used database name as test1
hardcoded in script. Now I want to provide the database name from the attribute file.
我在脚本中使用了数据库名称作为test1硬编码。现在我想从属性文件中提供数据库名称。
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 个解决方案
#1
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
谢谢你帮助IsabelHM
#2
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.
花了一点时间搞清楚将数组定义为节点属性的语法,然后将其成功传递给powershell_script。显然将其定义为字符串是有效的。
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
#3
Try using attribute environment to pass variable (in this example: dbname
) and then call it in the script as $dbname
尝试使用属性环境传递变量(在此示例中:dbname),然后在脚本中将其作为$ dbname调用
There are two ways to setup the attributes.
有两种方法可以设置属性。
Method 1
In attributes/default.rb, add this line
在attributes / default.rb中,添加此行
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
#4
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的environment属性的当前答案在它们如何引用代码块中的变量方面是不正确的。环境属性中指定的变量可用作环境变量,而不是脚本变量,必须作为环境变量引用,如下所示:
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.
基本上,使用environment属性将值传递到代码块的任何其他答案几乎都是正确的,只需确保在Powershell代码中使用$ env:添加变量名称。
#1
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
谢谢你帮助IsabelHM
#2
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.
花了一点时间搞清楚将数组定义为节点属性的语法,然后将其成功传递给powershell_script。显然将其定义为字符串是有效的。
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
#3
Try using attribute environment to pass variable (in this example: dbname
) and then call it in the script as $dbname
尝试使用属性环境传递变量(在此示例中:dbname),然后在脚本中将其作为$ dbname调用
There are two ways to setup the attributes.
有两种方法可以设置属性。
Method 1
In attributes/default.rb, add this line
在attributes / default.rb中,添加此行
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
#4
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的environment属性的当前答案在它们如何引用代码块中的变量方面是不正确的。环境属性中指定的变量可用作环境变量,而不是脚本变量,必须作为环境变量引用,如下所示:
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.
基本上,使用environment属性将值传递到代码块的任何其他答案几乎都是正确的,只需确保在Powershell代码中使用$ env:添加变量名称。