更改表格数据的连接数据源路径

时间:2018-06-27 22:08:14

标签: sql-server powershell ssas-tabular

我正在尝试更改MSSQL 2017的连接数据源路径。 我的代码:

$ServerName="localhost\olap17"
$loadInfo = [Reflection.Assembly]::LoadWithPartialName("Microsoft.AnalysisServices")

$server = New-Object Microsoft.AnalysisServices.Server
$server.connect($ServerName)
if ($server.name -eq $null) {
 Write-Output ("Server '{0}' not found" -f $ServerName)
 break
}
foreach ($dbase in $server.Databases )
{
Write-Output ( "Database: {0}; Status: {1}; Size: {2}MB; Data Sources: {3} " -f $dbase.Name, $dbase.State, ($dbase.EstimatedSize/1024/1024).ToString("#,##0"), $dbase.Model.DataSources.Count )
foreach ($dsource in $dbase.Model.DataSources)
{
    $dsource.ConnectionDetails.Address.Path = $dsource.ConnectionDetails.Address.Path.Replace('OLDSERVER', 'NEWSERVER')
    echo ">>> Datasource: " $dsource.ConnectionDetails.ToString()
}
}

输出似乎正确,但是当我检查“连接”属性时,看到的路径不变:

Database: Billing Cycle Delivery; Status: Unprocessed; Size: 0MB; Data Sources: 5 
Datasource: 
{
"protocol": "file",
"address": {
"path": "\\\\NEWSERVER\\Telco\\Models\\Billing Cycle Delivery\\Data Sources\\Billing.xlsx"
},
"authentication": null,
"query": null
}

看起来像新属性未传递回服务器... 我的问题在哪里?我在互联网上找不到任何有用的信息:(

任何想法表示赞赏!

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。需要:

$dbase.Update(1)

这是将更改推送到服务器所必需的。

我的最终脚本,如果有人需要的话:

$changeSource = 'OLDSERVER'
$changeTarget = 'NEWSERVER'
$ServerName="localhost\olap17"
$loadInfo = [Reflection.Assembly]::LoadWithPartialName("Microsoft.AnalysisServices")
$server = New-Object Microsoft.AnalysisServices.Server
$server.connect($ServerName)
if ($server.name -eq $null) {
 Write-Output ("Server '{0}' not found" -f $ServerName)
 break
}
foreach ($dbase in $server.Databases )
{
 Write-Output ( "Database: {0}; Status: {1}; Size: {2}MB; Data Sources: {3} " -f $dbase.Name, $dbase.State, ($dbase.EstimatedSize/1024/1024).ToString("#,##0"), $dbase.Model.DataSources.Count )
 foreach ($dsource in $dbase.Model.DataSources)
 {
    $dsource.ConnectionDetails.Address.Path = $dsource.ConnectionDetails.Address.Path.Replace($changeSource, $changeTarget)
    echo ">>> Datasource name: " $dsource.Name " Connection: " $dsource.ConnectionDetails.ToString()
    $dbase.Update(1)
}
}
$server.Disconnect($ServerName)
echo "Connection closed"
echo "------------- Confirmation --------------"
$server.connect($ServerName)
foreach ($dbase in $server.Databases )
{
 Write-Output ( "Database: {0}; Status: {1}; Size: {2}MB; Data Sources: {3} " -f $dbase.Name, $dbase.State, ($dbase.EstimatedSize/1024/1024).ToString("#,##0"), $dbase.Model.DataSources.Count )
 foreach ($newds in $dbase.Model.DataSources)
 {
    echo ">>> Datasource name: " $newds.Name " Connection: " $newds.ConnectionDetails.ToString()
 }
}
$server.Disconnect($ServerName)

以下MS文章对我有帮助:https://docs.microsoft.com/en-us/dotnet/api/microsoft.analysisservices.database?redirectedfrom=MSDN&view=sqlserver-2016