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

Change Connection datasource path for Tabular databse

我正在尝试更改 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
}

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

任何想法表示赞赏!

我找到了解决办法。需要:

$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