DMV 查询语法不正确异常

DMV Query syntax incorrect exception

我正在编写一个简单的查询函数,以便在我将来开始编写更多查询时使我的脚本高效和简单。

Function Query($Query) {
    $connectionString = "Provider=msolap;Data Source=$Server;Initial Catalog=$Database;"

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

    $command = New-Object System.Data.OleDb.OleDbCommand
    $command.Connection = $connection 
    $command.CommandText = $Query 

    $connection.Open()

    $adapter = New-Object System.Data.OleDb.OleDbDataAdapter #$command
    $adapter.SelectCommand = $command 

    $dataset = New-Object System.Data.DataSet
    [void] $adapter.Fill($dataSet)

    $connection.Close()

    $dataSet.Tables[0]
}

查询

SELECT [SESSION_ID], [SESSION_SPID] 
FROM $SYSTEM.DISCOVER_SESSIONS";

我收到这个错误:

Exception calling "Fill" with "1" argument(s): "Query (1, 41) The syntax for '.' is incorrect. (SELECT [SESSION_ID],[SESSION_SPID] FROM .DISCOVER_SESSIONS)."
+ [void] $adapter.Fill($dataSet)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : OleDbException

我错过了什么吗?我遵循了每个 MSDocs 的语法:

https://docs.microsoft.com/en-us/sql/analysis-services/instances/use-dynamic-management-views-dmvs-to-monitor-analysis-services?view=sql-server-2017

而且我实际上在没有创建查询函数的情况下尝试成功并显示了结果。以某种方式将其作为查询函数不起作用...

使用单引号字符串:

Query 'SELECT [SESSION_ID],[SESSION_SPID] FROM $SYSTEM.DISCOVER_SESSIONS';

在 Powershell 中,双引号字符串进行字符串插值,即字符串中的 $SYSTEM 被替换为变量 SYSTEM 的值,或者(在本例中)当SYSTEM 未定义。