显然,分析服务器中的数据库/多维数据集包含“脚本连接为>更改”选项,如下所示:
我想使用这样的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文件
服务器,多维数据集
server1.domain.com,Database1
并循环访问服务器中的数据库/多维数据集以更改其数据源<ConnectionString>
但是我确定该脚本中缺少某些内容,但是问题是,如果我在SSMS中运行该XML,它将抱怨<Name>
的{{1}}元素是必需的。我知道需要完整的XML,但是从我试图完成的脚本角度来看,它将无法正常工作,因为我无法为服务器中的其他数据库使用相同的DataSource。它们可能都不同,因此必须在Powershell脚本中从XML查询中删除ID,名称等。
我收到此错误
为ALTER语句提供的对象定义与要更改的对象引用的类型不同。
注意 :之所以没有使用分析服务器属性,是因为我在这里尝试使用分析属性方法ConnectionString property not printing connection string
无法正常工作,并且已作为损坏的属性升级到Microsoft,
答案 0 :(得分:1)
所以-基于您的其他问题(located here)中@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_4 变为 DatabaseName_40
这就是使用PowerShell和SMO仅更改连接字符串的详细信息的方式。
答案 1 :(得分:1)
我们一起了解到,SQL Server 2016或更高版本的兼容性级别为1200或更高。每当您在1200或更高级别上工作时,就无法使用经典的AMO对象来处理不同的属性。
Microsoft实际上非常清楚地指出了这一点:Programming with Analysis Management Objects (AMO)
如果要为1200或更高兼容性级别的表格模型进行编程,请使用表格对象模型(TOM)。 TOM是Analysis Services管理对象(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)