意外的令牌“ UpdateOptions.ExpandFull”

时间:2018-11-11 17:34:26

标签: sql-server powershell azure-analysis-services

基于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'或   声明。

如果我定期进行Update():

$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'。

2 个答案:

答案 0 :(得分:1)

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

您直接使用.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)

答案 1 :(得分:1)

您的问题主要是语法问题:

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

上面是.NET 方法调用,PowerShell在expression mode中对其进行了解析,这导致UpdateOptions.ExpandFull报告语法错误。

在表达方式下:

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

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

也就是说,您必须:

  • 两者之一:使用{em> full 类型名称[Microsoft.AnalysisServices.UpdateOptions]::ExpandFull,与Mötz' helpful answer中一样。

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

PowerShell提供了更方便的替代方法,但是:您只需将枚​​举值的符号名称指定为字符串 -'ExpandFull'-PowerShell将自动为您执行转换:

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

您甚至不需要知道枚举的类型名称即可工作(尽管,如果您确实知道它,可以在支持语言的编辑器(如Visual Studio Code)中使用它来为您提供IntelliSense。