1

我正在尝试使用以下脚本获取数据库数据源的连接字符串:

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

   $database = $Analysis_Server.Databases[7]
   $c = $database.DataSources[0].ConnectionString
   $c

什么都没有输出。

我试过这样调试:

$Analysis_Server.Databases

这将打印出服务器上的所有数据库

如果我索引它$Analysis_Server.Databases[],它会打印索引的任何数据库(所以在我的情况下,7,打印 database8)

显然,数据库属性正在工作。

-------------更新:--------------

这是数据库在服务器中的外观

server1_Databses

以下是以下每一行的作用:

   $Analysis_Server.Databases

这打印出来

数据库

人们可以注意到的一件事是,由于某种原因,它们不是按照它们在分析服务器上的顺序打印出来的,如图所示……我不知道为什么会这样

这是这个命令打印出来的:

Analysis_Server.Databases[0]

数据库8

现在由于 index 0 工作,我们应该能够 index[1], [2]等...

所以以下

Analysis_Server.Databases[1] 
Analysis_Server.Databases[2]

印刷:

数据库 1 和 3

现在到连接字符串:

$Analysis_Server.Databases[0].DataSources[0].ConnectionString

打印出来:

连接超时=60;用户 ID=someID;数据源=10.10.10.10;持久安全信息=True

它似乎是 Database8 的连接字符串

很酷,那么我们也应该能够做到这一点:

$Analysis_Server.Databases[1].DataSources[0].ConnectionString

但是,没有打印!似乎打印的唯一索引是Databases[0]

这是$Analysis_Server.Databases[0].DataSources[0]打印的:

数据源0

我们应该能够对所有数据库做同样的事情

$Analysis_Server.Databases[1].DataSources[0]

$Analysis_Server.Databases[2].DataSources[0] ETC..

但什么都没有打印出来!

4

3 回答 3

1

不是说我有你的答案,但我认为你可以使用另一种方法来获得你需要的东西,同时等待微软的支持。

我正在开发一个运行 SQL Server 2014 的 AX 2012 演示系统。它在同一个实例中具有多个 olap 数据库。我已经编辑了所有数据源以指向不同的数据库,以表明我们得到了正确的详细信息。

我在测试时安装了最新的 SQL Server PowerShell 模块。

Install-Module SqlServer -Force -Confirm:$false
Import-Module SqlServer

现在您应该有一个新的 PowerShell 提供程序,它使您能够像文件系统一样遍历 SQL Server

Get-PSProvider

Name                 Capabilities                                      Drives
----                 ------------                                      ------
Registry             ShouldProcess, Transactions                       {HKLM, HKCU}
Alias                ShouldProcess                                     {Alias}
Environment          ShouldProcess                                     {Env}
FileSystem           Filter, ShouldProcess, Credentials                {C, E, A, D...}
Function             ShouldProcess                                     {Function}
Variable             ShouldProcess                                     {Variable}
SqlServer            Credentials                                       {SQLSERVER}
Certificate          ShouldProcess                                     {Cert}
WSMan                Credentials                                       {WSMan}

有了它,您应该能够执行以下操作,这应该会产生正确的结果

#Connect to the SSAS part of the sql server 
PS C:\Users\Administrator> cd SQLSERVER:\SQLAS

#List all local instances
PS SQLSERVER:\SQLAS> dir

Host Name
---------
AX2012R2A
HTTP_DS

#Connect to the server (local) - you should be able to type in a valid network name
PS SQLSERVER:\SQLAS> cd AX2012R2A

#List all instances on that server
PS SQLSERVER:\SQLAS\AX2012R2A> dir

Instance Name
-------------
DEFAULT
POWERPIVOT
TABULAR

#Connect to the default instance on the server you are connected to
PS SQLSERVER:\SQLAS\AX2012R2A> cd default

#List all available collections / areas
PS SQLSERVER:\SQLAS\AX2012R2A\default> dir

#Connect to the databases area
PS SQLSERVER:\SQLAS\AX2012R2A\default> cd databases

#List all databases that are available
PS SQLSERVER:\SQLAS\AX2012R2A\default\databases> dir

Name                                     State                Read-Write Mode
----                                     -----                ---------------
Demand Forecast Accuracy initial         Processed            ReadWrite
Demand Forecast Accuracy ps              Processed            ReadWrite
Demand Forecast initial                  Processed            ReadWrite
Demand Forecast ps                       Processed            ReadWrite
Dynamics AX initial                      Processed            ReadWrite
Dynamics AX ps                           Processed            ReadWrite

#Getting the same result with Get-ChildItem
PS SQLSERVER:\SQLAS\AX2012R2A\default\databases> Get-ChildItem

Name                                     State                Read-Write Mode
----                                     -----                ---------------
Demand Forecast Accuracy initial         Processed            ReadWrite
Demand Forecast Accuracy ps              Processed            ReadWrite
Demand Forecast initial                  Processed            ReadWrite
Demand Forecast ps                       Processed            ReadWrite
Dynamics AX initial                      Processed            ReadWrite
Dynamics AX ps                           Processed            ReadWrite

#Traverse all databases and show their connection string details
PS SQLSERVER:\SQLAS\AX2012R2A\default\databases> Get-ChildItem | ForEach-Object {$_.Datasources}

Name                      Isolation     Max Connections Connection String
----                      ---------     --------------- -----------------
Dynamics Database         ReadCommitted              10 Provider=SQLNCLI11.1;Data
                                                        Source=AX2012R2A;Integrated
                                                        Security=SSPI;Initial
                                                        Catalog=DatabaseName_1
Dynamics Database         ReadCommitted              10 Provider=SQLNCLI11.1;Data
                                                        Source=AX2012R2A;Integrated
                                                        Security=SSPI;Initial
                                                        Catalog=DatabaseName_2
Dynamics Database         ReadCommitted              10 Provider=SQLNCLI11.1;Data
                                                        Source=AX2012R2A;Integrated
                                                        Security=SSPI;Initial
                                                        Catalog=MicrosoftDynamicsAX
Dynamics Database         ReadCommitted              10 Provider=SQLNCLI11.1;Data
                                                        Source=AX2012R2A;Integrated
                                                        Security=SSPI;Initial
                                                        Catalog=DatabaseName_4
Dynamics Database         ReadCommitted              10 Provider=SQLNCLI11.1;Data
                                                        Source=AX2012R2A;Integrated
                                                        Security=SSPI;Initial
                                                        Catalog=DatabaseName_5
Dynamics Database         ReadCommitted              10 Provider=SQLNCLI11.1;Data
                                                        Source=AX2012R2A;Integrated
                                                        Security=SSPI;Initial
                                                        Catalog=DatabaseName_6

PS SQLSERVER:\sqlas\ax2012r2A\default\databases> Get-ChildItem | ForEach-Object {$_.Datasources | ForEach-Object {$_.Connectionstring}}
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_2
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_4
Provider=SQLNCLI11.1;Data Source=AX2012R2A;Integrated Security=SSPI;Initial Catalog=DatabaseName_5
Provider=SQLNCLI11.1;Data Source=AX2012R2A;Integrated Security=SSPI;Initial Catalog=DatabaseName_6

我会说这应该可以让您继续前进,因为您现在可以相信您拥有正确的连接字符串详细信息。我与您的其他问题有关-这应该为您提供要生成的 alter 脚本的所有详细信息,因此您可以按照 SQL Server 对您的期望提供整个 alter 语句。

于 2018-11-10T10:53:42.357 回答
1

我只是多玩了一些东西,以确保我理解您所面临的问题。在同一个盒子上,仍然加载了最新的可用 SqlServer PowerShell 模块——我得到了这些结果。

PS C:\Users\Administrator> $Analysis_Server = New-Object Microsoft.AnalysisServices.Server
PS C:\Users\Administrator> $Analysis_Server.connect("AX2012R2A")
PS C:\Users\Administrator> $Analysis_Server.Databases

Name                                     State                Read-Write Mode
----                                     -----                ---------------
Demand Forecast ps                       Processed            ReadWrite
Demand Forecast Accuracy ps              Processed            ReadWrite
Demand Forecast Accuracy initial         Processed            ReadWrite
Dynamics AX ps                           Processed            ReadWrite
Demand Forecast initial                  Processed            ReadWrite
Dynamics AX initial                      Processed            ReadWrite

现在我遍历每个数据库及其数据源,以显示连接字符串

PS C:\Users\Administrator> $Analysis_Server.Databases | ForEach-Object {$_.datasources}

Name                      Isolation     Max Connections Connection String
----                      ---------     --------------- -----------------
Dynamics Database         ReadCommitted              10 Provider=SQLNCLI11.1;Data
                                                        Source=AX2012R2A;Integrated
                                                        Security=SSPI;Initial
                                                        Catalog=DatabaseName_4
Dynamics Database         ReadCommitted              10 Provider=SQLNCLI11.1;Data
                                                        Source=AX2012R2A;Integrated
                                                        Security=SSPI;Initial
                                                        Catalog=DatabaseName_2
Dynamics Database         ReadCommitted              10 Provider=SQLNCLI11.1;Data
                                                        Source=AX2012R2A;Integrated
                                                        Security=SSPI;Initial
                                                        Catalog=DatabaseName_1
Dynamics Database         ReadCommitted              10 Provider=SQLNCLI11.1;Data
                                                        Source=AX2012R2A;Integrated
                                                        Security=SSPI;Initial
                                                        Catalog=DatabaseName_6
Dynamics Database         ReadCommitted              10 Provider=SQLNCLI11.1;Data
                                                        Source=AX2012R2A;Integrated
                                                        Security=SSPI;Initial
                                                        Catalog=DatabaseName_3
Dynamics Database         ReadCommitted              10 Provider=SQLNCLI11.1;Data
                                                        Source=AX2012R2A;Integrated
                                                        Security=SSPI;Initial
                                                        Catalog=DatabaseName_5

以及只为您提供连接字符串而不是其他任何东西的单线

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

您能否花点时间尝试安装最新的 SqlServer PowerShell 模块,看看这对您和您面临的问题是否有任何影响?

于 2018-11-10T11:15:45.073 回答
0

在这里的帖子中找到了这个:http ://www.mrtsql.com/2011/03/powershell-updating-analysis-services.html

看看该帖子中的此功能是否可以帮助您。

function UpdateDataSources()
{
   # Lets get our server name
   $SSASServerName="$env:ComputerName\" + $SSASInstanceName
   $MyConnection = New-Object("Microsoft.AnalysisServices.Server")
   $MyConnection.Connect($SSASServerName)
   # lets return the number of data sources
   [int]$DataSourcecount=$MyConnection.Databases[$DatabaseName].DataSources.count
   for ($count=0; $count -ne $DataSourcecount;++$count)
   {
     $MyCS=$MyConnection.Databases[$DatabaseName].DataSources[$count].ConnectionString
    $NewCS=setNewValue -MyCS $MyCS -Pattern "Data Source=" -ReplaceWith $DataSourceServer

     if ($PW.Length -ne 0)
     {
        $NewCS=setNewValue -MyCS $NewCS -Pattern "Password=" -ReplaceWith $PW
     }
     if ($UserName.length -ne 0)
     {
        $NewCS=setNewValue -MyCS $NewCS -Pattern "User ID=" -ReplaceWith $UserName
     }
     $MyConnection.Databases[$DatabaseName].DataSources[$count].ConnectionString=$NewCS
     # write the change back to SSAS
     $MyConnection.Databases[$DatabaseName].DataSources[$count].update()

     write-output $NewCS
     }
}
于 2018-10-30T17:58:43.633 回答