2

基于https://docs.microsoft.com/en-us/bi-reference/tom/add-a-data-source-to-tabular-model-analysis-services-amo-tom

我正在尝试更新数据库连接字符串更改:

Import-Module SqlServer

$newConnectionString = "Connection Timeout=60;User Id=SOME_NEW_ID;Data Source=10.10.19.10;Persist Security Info=True;Session Character Set=UTF8"

 $svr = new-Object Microsoft.AnalysisServices.Tabular.Server
$svr.Connect("server1.domain.com")

$svr.databases[1].model.datasources[0].ConnectionString = $newConnectionString
$svr.Databases[1].model.datasources[0].Update(UpdateOptions.ExpandFull)

但我收到错误:

表达式或语句中出现意外的标记“UpdateOptions.ExpandFull”。

如果我定期更新():

$svr.Databases[1].model.datasources[0].Update()

我明白了:

方法调用失败,因为 [Microsoft.AnalysisServices.Tabular.ProviderDataSource] 不包含名为“更新”的方法

如果我尝试SaveChanges()

$svr.Databases[1].Model.SaveChanges()

我收到此错误:

检索成员“SaveChanges”时发生以下异常:“遇到默认值的无效类型。”

如果我尝试ExpandFull

$svr.Databases[1].model.datasources[0].Update(ExpandFull)

我明白了

表达式或语句中出现意外的标记“ExpandFull”。

4

2 回答 2

2

错误是您为更新指定枚举的方式。

您正在直接使用 .NET 类型,并且您必须了解/知道给定枚举所属的位置。在这种情况下,UpdateOptions 位于 Microsoft.AnalysisServices 命名空间中

更新

Import-Module SqlServer

$newConnectionString = "Connection Timeout=60;User Id=SOME_NEW_ID;Data Source=10.10.19.10;Persist Security Info=True;Session Character Set=UTF8"

$svr = new-Object Microsoft.AnalysisServices.Tabular.Server
$svr.Connect("server1.domain.com")

$svr.databases[1].model.datasources[0].ConnectionString = $newConnectionString
$svr.Databases[1].Update([Microsoft.AnalysisServices.UpdateOptions]::ExpandFull)
于 2018-11-11T18:08:50.267 回答
2

您的问题主要是语法问题

$svr.Databases[1].model.datasources[0].Update(UpdateOptions.ExpandFull)

以上是.NET方法调用,PowerShell以表达式模式解析,导致UpdateOptions.ExpandFull报语法错误。

在表达模式下:

  • 对必须包含在 中的类型的引用;例如,UpdateOptions[...][UpdateOptions]

  • 对该类型的静态成员的引用必须通过::操作符进行引用;例如[UpdateOptions]::ExpandFull

也就是说,您必须:

  • 要么:使用完整的类型名称,[Microsoft.AnalysisServices.UpdateOptions]::ExpandFullMötz 的有用答案

  • 或者,在 PSv5+ 中:通过using namespace Microsoft.AnalysisServices在脚本开头放置一个语句,您可以使[UpdateOptions]::ExpandFull工作更简洁。

然而, PowerShell 提供了一种更方便的替代方法:您可以简单地将枚举值的符号名称指定为字符串-'ExpandFull'并且 PowerShell 会自动为您执行转换:

$svr.Databases[1].model.datasources[0].Update('ExpandFull')

您甚至不需要知道枚举的类型名称就可以工作(不过,如果您知道,在 Visual Studio Code 等语言感知编辑器中使用它可以为您提供 IntelliSense)。

于 2018-11-11T22:17:28.023 回答