如何仅更改连接字符串?

How to alter connection string only?

显然分析服务器中的 databases/cubes 包含 "script connection as > alter" 选项,如下所示:

我想使用这样的 powershell 脚本:

    $hashtable = @{}
Import-Csv "CSV_file" | ForEach-Object {
    $hashtable += @{$($_.Server) = ($_.Cube -split '\s*,\s*') }
}

Import-Module SqlServer

foreach($server in $hashtable.Keys){ 

   $Analysis_Server = New-Object Microsoft.AnalysisServices.Server  
   $Analysis_Server.connect("$server") 

   foreach($CUBE in $hashtable[$server]) {

      ####### Setting connection property for $Cube #######
"  
    <Alter ObjectExpansion="ExpandFull" xmlns="http://schemas.microsoft.com/analysisservices/2003/engine">
    <Object>
        <DatabaseID>$CUBE</DatabaseID>
    </Object>
    <ObjectDefinition>
        <DataSource xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ddl2="http://schemas.microsoft.com/analysisservices/2003/engine/2" xmlns:ddl2_2="http://schemas.microsoft.com/analysisservices/2003/engine/2/2" xmlns:ddl100_100="http://schemas.microsoft.com/analysisservices/2008/engine/100/100" xmlns:ddl200="http://schemas.microsoft.com/analysisservices/2010/engine/200" xmlns:ddl200_200="http://schemas.microsoft.com/analysisservices/2010/engine/200/200" xmlns:ddl300="http://schemas.microsoft.com/analysisservices/2011/engine/300" xmlns:ddl300_300="http://schemas.microsoft.com/analysisservices/2011/engine/300/300" xmlns:ddl400="http://schemas.microsoft.com/analysisservices/2012/engine/400" xmlns:ddl400_400="http://schemas.microsoft.com/analysisservices/2012/engine/400/400" xmlns:ddl500="http://schemas.microsoft.com/analysisservices/2013/engine/500" xmlns:ddl500_500="http://schemas.microsoft.com/analysisservices/2013/engine/500/500" xsi:type="RelationalDataSource">
            <ConnectionString>Connection Timeout=60;User Id=someID;Password=pass;Data Source=td.domain.com;Persist Security Info=True;Session Character Set=UTF8</ConnectionString>
        </DataSource>
    </ObjectDefinition>
    </Alter>
    "

    }
}

输入 csv 文件

Server,Cube

server1.domain.com,Database1

并循环遍历服务器中的 databases/cubes 以更改其数据源 <ConnectionString>

但是我确定该脚本中缺少某些内容,但问题是如果我 运行 在 SSMS 中 XML,它会抱怨 <Name> 元素 <DataSource> 是必需的。我知道需要完整的 XML,但是从我试图完成的脚本角度来看它不会工作,因为我不能为服务器中的其他数据库使用相同的数据源。它们都可能不同,因此必须从 powershell 脚本的 XML 查询中删除 ID、名称等。

我收到这个错误

The object definition supplied for the ALTER statement is of a different type that the object reference to be altered.


注意:我没有使用分析服务器属性的原因是因为我在这里尝试的分析属性方法

无效,已上报给 Microsoft 已损坏 属性、

所以 - 基于@thom schumacher 从你的另一个问题中得到的结果 ()

$Analysis_Server = New-Object Microsoft.AnalysisServices.Server
$Analysis_Server.connect("AX2012R2A")

#Getting current state
PS C:\Users\Administrator> $Analysis_Server.Databases | ForEach-Object {$_.datasources | ForEach-Object {$_.ConnectionSt
ring}}
Provider=SQLNCLI11.1;Data Source=AX2012R2A;Integrated Security=SSPI;Initial Catalog=DatabaseName_4
Provider=SQLNCLI11.1;Data Source=AX2012R2A;Integrated Security=SSPI;Initial Catalog=DatabaseName_2
Provider=SQLNCLI11.1;Data Source=AX2012R2A;Integrated Security=SSPI;Initial Catalog=DatabaseName_1
Provider=SQLNCLI11.1;Data Source=AX2012R2A;Integrated Security=SSPI;Initial Catalog=DatabaseName_6
Provider=SQLNCLI11.1;Data Source=AX2012R2A;Integrated Security=SSPI;Initial Catalog=DatabaseName_3
Provider=SQLNCLI11.1;Data Source=AX2012R2A;Integrated Security=SSPI;Initial Catalog=DatabaseName_5

#Setting the new connection string in variable
$connectionString = "Provider=SQLNCLI11.1;Data Source=AX2012R2A;Integrated Security=SSPI;Initial Catalog=DatabaseName_40"

#Assigning the variable to one of the databases
$Analysis_Server.Databases[0].datasources[0].ConnectionString = $connectionString

#Remember to update the new configuration back to the database
$Analysis_Server.Databases[0].datasources[0].Update()

#Getting the new configuration
PS C:\Users\Administrator> $Analysis_Server.Databases | ForEach-Object {$_.datasources | ForEach-Object {$_.ConnectionSt
ring}}
Provider=SQLNCLI11.1;Data Source=AX2012R2A;Integrated Security=SSPI;Initial Catalog=DatabaseName_40
Provider=SQLNCLI11.1;Data Source=AX2012R2A;Integrated Security=SSPI;Initial Catalog=DatabaseName_2
Provider=SQLNCLI11.1;Data Source=AX2012R2A;Integrated Security=SSPI;Initial Catalog=DatabaseName_1
Provider=SQLNCLI11.1;Data Source=AX2012R2A;Integrated Security=SSPI;Initial Catalog=DatabaseName_6
Provider=SQLNCLI11.1;Data Source=AX2012R2A;Integrated Security=SSPI;Initial Catalog=DatabaseName_3
Provider=SQLNCLI11.1;Data Source=AX2012R2A;Integrated Security=SSPI;Initial Catalog=DatabaseName_5

请注意每个列表中的第一个条目。从 DatabaseName_4DatabaseName_40

这就是您如何使用 PowerShell 和 SMO 仅更改连接字符串的详细信息。

我们一起了解到 SQL Server 2016 或更高版本在 1200 或更高的兼容级别下工作。每当您在 1200 级或更高级别工作时,您都无法使用经典的 AMO 对象来处理不同的属性。

微软其实说的很清楚:Programming with Analysis Management Objects (AMO)

如果您正在为 1200 或更高兼容级别的表格模型编程,请使用表格对象模型 (TOM)。 TOM 是分析服务管理对象 (AMO) 客户端库的扩展。

了解 TOM 就像当年了解 AMO 一样。你必须从这样的地方开始:Tabular Object Model (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].Update([Microsoft.AnalysisServices.UpdateOptions]::ExpandFull)