簡體   English   中英

從表格模型中提取 DMV 結果的 Powershell 腳本

[英]Powershell Script to pull DMV results from Tabular Model

嘗試構建 PowerShell 腳本以連接到 Analysis Services表格模型並提取 DMV 查詢的輸出(例如:SELECT * FROM $System.DBSchema_Tables)

在下面嘗試過,但它失敗了,似乎連接字符串或我嘗試連接的方式有問題:

$connectionString = "server=TabularServerName;database='ModelName';trusted_connection=true;";
$CubeQuery = "SELECT * FROM $System.DBSchema_Tables";

#SQL Connection - connection to SQL server
$sqlConnection = new-object System.Data.SqlClient.SqlConnection;
$sqlConnection.ConnectionString = $connectionString;

#SQL Command - set up the SQL call
$sqlCommand = New-Object System.Data.SqlClient.SqlCommand;
$sqlCommand.Connection = $sqlConnection;
$sqlCommand.CommandText = $CubeQuery;

#SQL Adapter - get the results using the SQL Command
$sqlAdapter = new-object System.Data.SqlClient.SqlDataAdapter 
$sqlAdapter.SelectCommand = $sqlCommand
$dataSet = new-object System.Data.Dataset
$recordCount = $sqlAdapter.Fill($dataSet)

你不只是使用 SQLPS 模塊或DBA 工具模塊是什么?

當然,您還可以利用其他模塊:

Find-Module -Name '*sql*' | Format-Table -AutoSize
Find-Package -Name '*sql*' | Format-Table -AutoSize

這是我傳遞給其他人使用 SQL 的東西。

安裝 SQL Server PowerShell 模塊

https://docs.microsoft.com/en-us/sql/powershell/download-sql-server-ps-module?view=sql-server-ver15

https://docs.microsoft.com/en-us/powershell/module/sqlps/?view=sqlserver-ps

https://docs.microsoft.com/en-us/sql/powershell/sql-server-powershell?view=sql-server-ver15

然后看:

將 PowerShell 連接到 SQL Server

作為概述,以下是我將在本文中介紹的選項列表:

  • SQL Server PowerShell (SQLPS)
  • SQL Server 管理對象 (SMO)
  • .NET (System.Data.SqlClient)

SQLPS

SQL Server PowerShell SQLPS 是一個首次隨 SQL Server 2008 一起發布的實用程序,您可能會看到它以各種方式被引用。 它作為 (1) 實用程序和 (2) 作為 PS 模塊存在。 該實用程序和模塊隨 SQL Server 2008 及更高版本的 SQL Server 管理工具一起安裝。 有幾種使用此實用程序連接到 SQL Server 的方法,每種方法都有其優點和缺點。

執行程序

這是一個實用程序,您應該能夠通過在運行提示(開始 > 運行)中鍵入它來打開它。 第二個選項,在 SQL Server Management Studio (SSMS) 中,右鍵單擊對象資源管理器下的節點,然后選擇“啟動 PowerShell”。 SQLPS 實用程序的主要訪問點是使用提供程序“SQLSERVER:\\”來像文件目錄一樣瀏覽 SQL Server。 這樣,基於您從中打開 SQLPS 的節點,您將位於提供程序的該路徑中。 在您所在的每個“文件夾”下,提供程序提供要讀取或設置的屬性,以及一些用於管理的方法。

Get-ChildItem SQLSERVER:\SQL\LOCALHOST\SQL12\Databases | foreach { $_.RecoveryModel = "SIMPLE"; $_.Alter() }

SQLPS 模塊

將 SQLPS 模塊導入 PS 會話提供與使用該實用程序相同的訪問權限,但允許您在操作系統的 PS 版本中進行操作。 在 SQL Server 2008 和 2008 R2 中,您將 SQLPS 作為管理單元 (Add-PSSnapin) 加載,然后在 SQL Server 2012 及更高版本中導入 (Import-Module)。

# Loading SMO
Add-Type -AssemblyName "Microsoft.SqlServer.Smo,Version=11.0.0.0,Culture=neutral,PublicKeyToken=89845dcd8080cc91"

# Connecting with SMO
$srv = New-Object Microsoft.SqlServer.Management.Smo.Server “localhost\sql12”
$srv.Databases | select name

# .NET Framework
# Create a connection
$sqlConn = New-Object System.Data.SqlClient.SqlConnection
$sqlConn.ConnectionString = “Server=localhost\sql12;Integrated Security=true;Initial Catalog=master”
$sqlConn.Open()

# Create your command (the T-SQL that will be executed)
$sqlcmd = $sqlConn.CreateCommand()
<# or #>
$sqlcmd = New-Object System.Data.SqlClient.SqlCommand
$sqlcmd.Connection = $sqlConn
$query = “SELECT name, database_id FROM sys.databases”
$sqlcmd.CommandText = $query

# Create your data adapter (if you want to retrieve data)
$adp = New-Object System.Data.SqlClient.SqlDataAdapter $sqlcmd

# Create your dataset (the adapter fills this object)
$data = New-Object System.Data.DataSet
$adp.Fill($data) | Out-Null

# Retrieving Your Data
$data.Tables
<# or #>
$data.Tables[0]

最后:

使用 POWERSHELL 從 2016 年表格立方體中獲取所有度量

因此,這是將從多維數據集獲取度量的 PowerShell 腳本(更改前三個變量以適應您的環境):

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.AnalysisServices.Tabular");

$tab = "YourSSASserver";
$dbId = "ID_or_DB";
$saveas = "C:\YourFolder\{0}.dax" -f $tab.Replace('\', '_');


$as = New-Object Microsoft.AnalysisServices.Tabular.Server;
$as.Connect($tab);
$db = $as.Databases[$dbId];
# in case you want to search by the name of the cube/db:
# $as.Databases.GetByName("DB Name");

$out = "";

foreach($t in $db.Model.Tables) {
  foreach($M in $t.Measures) {
    $out += "// Measure defined in table [" + $t.Name + "] //" + "`n";
    $out += $M.Name + ":=" + $M.Expression + "`n";
  }
}

$as.Disconnect();
$out = $out.Replace("`t","  "); # I prefer spaces over tabs :-)
$out.TrimEnd() | Out-File $saveas; 

以下是對我有用的

$connectionString =  $connectionString = “Provider=MSOLAP;Data Source=TabularServerName;” 
$CubeQuery = 'SELECT * FROM $System.DBSchema_Tables';

$connection = New-Object -TypeName System.Data.OleDb.OleDbConnection

$connection.ConnectionString = $connectionString
$sqlCommand = $connection.CreateCommand() 

$sqlCommand.CommandText = $CubeQuery;

#SQL Adapter - get the results using the SQL Command
$sqlAdapter = new-object System.Data.SqlClient.SqlDataAdapter 
$sqlAdapter.SelectCommand = $sqlCommand
$dataSet = new-object System.Data.Dataset
$recordCount = $sqlAdapter.Fill($dataSet)```

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM