问题描述
我正在调用dbatools Install-DbaInstance
函数,参数之一是Feature
。我将变量初始化为“ Engine”。如果为$bolSSIS -eq $true
,我想将“ IntegrationServices”添加到变量中。如果为$bolSSAS -eq $true
,我想在变量中添加“ analysisservices”。
下面的代码并不完整,但是我相信足以解释我要尝试做的事情:
$bolSSIS = $true
$bolSSAS = $false
$InstallFeatures = "Engine"
if ($bolInstallFeatureSSIS -eq $true) { $InstallFeatures += ",IntegrationServices" }
if ($bolInstallFeatureSSAS -eq $true) { $InstallFeatures += ",analysisservices" }
Install-DbaInstance -Feature $InstallFeatures
上面的代码返回错误:
Cannot validate argument on parameter 'Feature'. The argument "Engine,IntegrationServices" does not belong to the set "Default,All,Engine,Tools,Replication,FullText,DataQuality,polyBase,MachineLearning,analysisservices,IntegrationServices,{others removed for brevity} " specified by the validateset attribute. Supply an argument that is in the set and then try the command again.
这是我的问题:如何设置$InstallFeatures
?
我已经尝试过字符串,数组,哈希和其他变量类型。
FWIW,如果$InstallFeatures
仅设置为“默认”,则Install-DbaInstance -Feature $InstallFeatures
命令有效,并且不返回错误。
解决方法
如果将$ InstallFeatures声明为数组,则添加更多字符串会将其添加为数组元素,而不是串联。
例如
$boolSSIS = $true
$boolSSAS = $false
$InstallFeatures = @("Engine")
if ($boolSSIS) { $InstallFeatures += "IntegrationServices" }
if ($boolSSAS) { $InstallFeatures += "AnalysisServices" }
,
参数Feature
被定义为字符串数组([string[]]$Feature
)。您正在发送单个字符串,该字符串应为数组。
无需更改脚本的其余部分,您就可以
Install-DbaInstance -Feature ($InstallFeatures -split ',')