2
例如,假设我有一个脚本资源:如何使资源水平的变量
configuration ChocolateyServer {
node localhost {
Script ChocolateyPackages {
SetScript = {
$tools = 'googlechrome,notepadplusplus,7zip,microsoftwse,octopusdeploy.tentacle,sqlserver-cmdlineutils'
foreach($tool in $tools.Split(',')) {
if(($packages | ? { $_.ToString().ToLower().StartsWith($tool) }).Count -eq 0) {
choco install $tool -y
}
}
}
TestScript = {
$tools = 'googlechrome,notepadplusplus,7zip,microsoftwse,octopusdeploy.tentacle,sqlserver-cmdlineutils'
$packages = (choco list -lr).ToLower()
foreach($tool in $tools.Split(',')) {
if(($packages | ? { $_.ToString().ToLower().StartsWith($tool) }).Count -eq 0) {
return $false
}
}
return $true
}
GetScript = { return @{ Result = "nothing" } }
}
}
}
我想拉$tools
出来的这两个独立的功能,以避免代码重复。我很满意能够将$tools
添加到我的脚本资源中,并在这两个函数之间共享它,而不必定义两次。
但是,试图做到这一点给我一个错误:
An attribute name for resource 'Script' was found that is not valid. An attribute name must be a simple string, and cannot contain variables or expressions. Replace '$tools' with a simple string.
我如何共享代码?