如何在powershell脚本中使用属性值

如何在powershell脚本中使用属性值,powershell,chef-infra,chef-recipe,powershell-4.0,Powershell,Chef Infra,Chef Recipe,Powershell 4.0,我正在使用chef中的powershell\u脚本资源在SQLServer2012中创建一个数据库 我在脚本中使用了数据库名作为test1硬编码。现在我想从属性文件中提供数据库名称 如何获取从属性文件引用到脚本的值 powershell_script "create database" do code <<-EOH [System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SqlServer.SMO') |

我正在使用chef中的
powershell\u脚本
资源在SQLServer2012中创建一个数据库

我在脚本中使用了数据库名作为
test1
硬编码。现在我想从属性文件中提供数据库名称

如何获取从属性文件引用到脚本的值

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
powershell\u脚本“创建数据库”执行

code尝试使用attributeenvironment传递变量(在本例中为:
dbname
),然后在脚本中将其称为
$dbname

有两种方法可以设置属性

方法1

在attributes/default.rb中,添加此行

default['yourCookbookNameHere']['dbname'] = 'test1'
在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
powershell\u脚本“创建数据库”执行
环境({'dbname'=>“#{node['yourcookbookname']['dbname']}”)

代码现在修改的脚本如下所示

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
现在我可以使用属性文件中的值来创建数据库


感谢您的帮助,IsabelHM花了一点时间找出语法,将数组定义为节点属性,然后将其成功传递给powershell\u脚本。显然,将其定义为字符串是有效的

属性:

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

powershell_script 'my script' do
  code <<-EOH
  $array = #{node['cookbook-name']['attribute-name']}
  ...
  EOH
  action :run
  guard_interpreter :powershell_script
  not_if "..."
end
powershell\u脚本“我的脚本”执行

code此处使用
powershell\u脚本
environment
属性的当前答案在引用代码块内变量的方式上不正确。
环境
属性中指定的变量可用作环境变量,而不是脚本变量,并且必须作为环境变量引用,如下所示:

powershell_script 'Passed-In Variable Example' do
  environment({ myVar: 'myvalue' })
  code <<-EOH
    Write-Output $env:myVar
  EOH
end
powershell\u脚本“传入变量示例”do
环境({myVar:'myvalue'})

代码如前所述修改了脚本,它能够获得在属性文件中传递的值,但仍然失败,出现以下错误。已编译资源:---------------在c:/chef/cache/cookbooks/sqlserver/recipes/default.rb:1:in`from_file'powershell_脚本(“创建数据库”)中执行操作“运行”重试0重试延迟2默认\u保护\u解释器:powershell_脚本命令“创建数据库”备份5环境{“dbname”=>“测试数据库”}返回0错误是什么?您可以在default.rb中也发布该属性吗?default.rb具有以下值,如果它的if节点错误,请更正我['platform_family']='windows'默认值['cookbook']['dbname']='testing database'end@bbsys您想从属性default.rb文件或配方default.rb中的局部变量获取参数吗?我想从属性default.rb获取参数
powershell_script 'my script' do
  code <<-EOH
  $array = #{node['cookbook-name']['attribute-name']}
  ...
  EOH
  action :run
  guard_interpreter :powershell_script
  not_if "..."
end
powershell_script 'Passed-In Variable Example' do
  environment({ myVar: 'myvalue' })
  code <<-EOH
    Write-Output $env:myVar
  EOH
end